Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

When numeric conversion overloading goes awry.

by dmitri (Curate)
on Jan 24, 2006 at 19:21 UTC ( #525293=perlquestion: print w/ replies, xml ) Need Help??
dmitri has asked for the wisdom of the Perl Monks concerning the following question:

I have a server written in Perl that does some tasks that clients ask it to. Requests and responses are passed using soap, namely SOAP::Lite. Responses can be successful or unsuccessful. The latter are of two types: error codes plus honest-to-God exceptions (well, as close as Perl comes to them, anyway) of type Error or Exception::Class. The problem occurs when SOAP::Serializer tries to serialize objects of type Error. Error.pm has the following code (shortened for our purposes):
use overload ("0+" => 'value'); sub value { my $self = shift; exists $self->{'-value'} ? $self->{'-value'} : undef; }
Note that this subroutine may return undef. Now, SOAP::Serializer calls subroutine gen_id on references, which for normal objects is just the value of memory address:
sub gen_id { sprintf "%U", $_[1] }
Now, the problem is that when objects of type Error do not have a value (returns undef), this produces a segfault in perl 5.6.1. This problem can be demonstrated with the following short script:
package My; use overload ('0+' => 'num_conv'); sub new { my $self = shift; bless {}, ref($self) || $self; } sub num_conv { undef } package main; my $var = My->new; print int($var);
We see that when numeric conversion overloading function returns undef to built-in Perl functions that invoke it, that crashes perl. My question is, how do I approach this problem? Do I try to
  • find out whether I can modify Error.pm (drop numeric conversion overload insanity) and figuring out the implications of such change;
  • modify gen_id routine to turn off numeric conversion overloading for referenced packages that are passed to it;
  • other?
Your input is greatly appreciated. Thank you.

Comment on When numeric conversion overloading goes awry.
Select or Download Code
Re: When numeric conversion overloading goes awry.
by dragonchild (Archbishop) on Jan 24, 2006 at 19:34 UTC
    Submit the following patch to the maintainer of Error.pm:
    -exists $self->{'-value'} ? $self->{'-value'} : undef; +exists $self->{'-value'} ? $self->{'-value'} : 0;
    The bug is that 0+ should always return something that is a number. Undef, as you've discovered, isn't a number.

    My criteria for good software:
    1. Does it work?
    2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?
      That will technically work. Semantically, however, this may lead to problems -- I think SOAP::Serializer relies on this technique to generate unique IDs for objects. If there are two such Error objects, SOAP::Serializer may get confused. I will file a bug in Error.pm's CPAN page, however, as yours is a valid point.
        Then that's a bug in Soap::Serializer. UIDs are a well-studied space with UUID and Data::UUID simply being two modules I've personally used. That should be filed, as well.

        My criteria for good software:
        1. Does it work?
        2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?
Re: When numeric conversion overloading goes awry.
by Anonymous Monk on Jan 24, 2006 at 19:48 UTC
Re: When numeric conversion overloading goes awry.
by chromatic (Archbishop) on Jan 24, 2006 at 23:03 UTC
      Oh, this is interesting. I will try this out...

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://525293]
Approved by Corion
Front-paged by bart
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (9)
As of 2014-11-29 01:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (200 votes), past polls