On Mon, Mar 01, 2004 at 11:38:56AM +0300 Vadim O. Ustiansky wrote:

> Dear Tassilo!

Hi!

> Thank you for your answer, but that is slightly not what I was asking for.
> Sorry if my first post was not enough clear.
> 
> If, as you said, you store a pointer to the C struct somewhere in an SV, the
> raw memory address becomes available at the perl level. The perl programmer
> may simply change this value and your C code will not notice it. This just
> opens a door at perl level to all headaches known to C pointers.

So you have to take some precautions against people changing the address
of the pointer. You could for instance mark the underlying SV readonly
with SvREADONLY_on(). 

There are perhaps other possible ways. If you look at a thusly blessed
reference:

    perl -Mblib -MDevice::CDROM -MDevel::Peek -e 
'Dump(Device::CDROM->new("/dev/cdrom"))'
    SV = RV(0x814ca54) at 0x8137ec0
      REFCNT = 1
      FLAGS = (TEMP,ROK)
      RV = 0x8137ff8
      SV = PVMG(0x81dc0a0) at 0x8137ff8
        REFCNT = 1
        FLAGS = (OBJECT,IOK,pIOK)
        IV = 136312992
        NV = 0
        PV = 0
        STASH = 0x81a1e20   "Device::CDROM"

You can see the SV in question (the PVMG thingy) has the IOK flag set
indicating that this SV should be treated as an ordinary integer. I
thought it would be enough to, say, call SvNOK_only() on the referenced
SV to protect the IV slot. But some tests show that perl still allows to
assign an integer to it and that the IOK flags are back in.

Just mark it readonly and it should be fine.

> So my question was is this avoidable. From your answer that this approach
> is a common practice I may conclude that it is up to perl programmer to
> respect the privacy of such data.

This is always the case, yes.

But now that I see that this

    perl -blib -MDevice::CDROM -MDevel::Peek -e '
        $s = Device::CDROM->new("/dev/cdrom");
        $ss = 1;
    '

segfaults horribly in the DESTROY method, I might also mark my object
readonly.

Tassilo
-- 
$_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus})!JAPH!qq(rehtona{tsuJbus#;
$_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval

Reply via email to