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

Re: [perl #21347] segfault in UNIVERSAL::AUTOLOAD

Thread Previous | Thread Next
Benjamin Goldberg
February 26, 2003 20:26
Re: [perl #21347] segfault in UNIVERSAL::AUTOLOAD
Message ID:
Hugo wrote:
> Rafael Garcia-Suarez wrote:
> :Enache Adrian wrote:
> :> Perl blesses the reference qr// returns into a hypothetical
> :> "Regexp" package (see pp_qr() - pp_hot.c:1140) . It seems that the
> :> only use for this trick is that
> :>      $a = qr/ ... /;
> :>      ref $a
> :> returns 'Regexp'. This is used by a lot a Perl modules/scripts.
> :>
> :> When the time comes for the qr// to be freed, the code inside
> :> sv_clear() tries to call its DESTROY method - since it is an
> :> object. (sv.c:5261)
> :> DESTROY doesn't exist in the 'Regexp' stash: if the user defined
> :> a UNIVERSAL::AUTOLOAD sub, that will be called instead.
> :> If that AUTOLOAD uses qr//'s, the recursion happens.
> :
> :Thanks for that explanation.
> :
> :> I have 2 ideas to fix that:
> :>
> :> 1) Completely drop the 'Regexp' stash trick. Let $a = qr/../ be
> :> a simple reference. Hack sv_reftype to return "Regexp" if the SV
> :> its argument points to is a 'r'-magical variable.
> :
> :You can try this to see what breaks. From what I can see, some
> :adjustements may be needed in Data::Dumper. However I think this is a
> :cleaner solution than :
> :
> :> 2) Put a dummy DESTROY method in the 'Regexp' stash.
> Hmm, I'd have thought the "cleaner" argument applies the other way
> round: surely it is cleaner to let it be an object than to hack the
> pretence of it?

An extention of that idea would be to move the compiled regex out of the
magic, and into the SV, so that if one does:
   $a = qr/..../;
   $b = bless \"$$a", 'Regexp';
Then $b is effectively the same compiled regexp as $a was.

In some ways, this would be the cleanest change, but it has the severe
drawback of allowing the user to take arbitrary strings, and tell perl
that they're compiled regexen, potentially leading to a segfault.

(This could be avoided by having perl attatch a *tiny* bit of magic,
indicating that a blessed Regexp object was created by qr//; if that
magic is absent, it would vet the compiled regex for correctness, and if
all is not well, croak; otherwise, add the magic to avoid doing that
check again in the future, and run the regex.  This magic would be
transparent to the user.)

> It seems like it might be helpful at some point to be able to add
> other Regexp:: methods - for introspection, for example, or even
> as a window to enable or disable particular types of optimisation.
> I'd even be tempted to document it, though that may be too radical
> a step.

$;=qq qJ,krleahciPhueerarsintoitq;sub __{0 &&
my$__;s ee substr$;,$,&&++$__%$,--,1,qq;;;ee;
$__>2&&&__}$,=22+$;=~y yiy y;__ while$;;print

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