develooper Front page | perl.perl5.porters | Postings from March 2003

Re: odd (or not so odd?) segmentation fault in 5.8.0

Thread Previous | Thread Next
March 1, 2003 23:25
Re: odd (or not so odd?) segmentation fault in 5.8.0
Message ID:
On Mon, 17 Feb 2003 22:22:16 +0200, wrote:
>On Mon, Feb 17, 2003 at 07:23:58PM +0000, Dave Mitchell wrote:
>> What happens is as follows:
>>     sub P::FETCH { $a+1 }
>>     $a = 1;
>>     tie $a, P;
>>     $x = $a;
>> is executed as follows:
>> 1) the plain SV associated with $a gets the value 1
>> 2) The $a gets magic attached to it
>> 3) an attempt is made to get the value of $a
>>   3a) $a's magic is temporarily disabled, then FETCH is called
>>   3b) FETCH accesses the current value of (plain) $a, which is 1, and
>>       returns that plus 1
>>   3c) the returned value from FETCH is assigned to $a's SV, ie SvNVX($a) = 2
>Where ( please give me a source reference ) does this happen ?
>This certainly looks like a bug.

Which part looks like a bug?  This is just how get/set magic works
(excluding overloading, which is a horse of a different colour).  The
fields of the SV are used as kind of a buffer.  The get magic is
called and the results placed in the SV, from whence they are accessed
just as if there was no magic.  They can be changed, just as if there
were no magic, and then the set magic is called, which reads the new
value from the SV itself.

I think the source reference you want is the sv_setsv in mg.c's
S_magic_methpack.  (The scheme I describe is implemented in different
places for different types of magic.)

>> Whether this is the correct or best possible behaviour is another matter -
>> personally I think it is reasonable given the complexities of tie.
>No. That isn't reasonable.
>    print $a, $a, $a, $a
>should get the same value for each $a.
>(note that FETCH doesn't modify in any way the value of the $a scalar -
>neither the magic nor the regular).

The tie "owns" the fields of the $a scalar, and the implementation is
that they are set to the value FETCHed.  Because $a temporarily isn't
magic *within* the FETCH call, you are getting direct access to them,
in effect, saying FETCH should each time return one more than the last
value FETCHed.

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About