Data-UUID icon indicating copy to clipboard operation
Data-UUID copied to clipboard

Data::UUID->new->from_string(4) doesn't always throw an error.

Open ikegami opened this issue 11 years ago • 3 comments

$ perl -MData::UUID -e'
    my $x = "12345678901234567890123456789012";
    print Data::UUID->new->from_string($x), "\n";
    $x = 4;
    print Data::UUID->new->from_string($x), "\n";
    print "done\n";
'
xV4▒V4x▒4Vx▒
xV4▒V4x▒4Vx▒
done

That's wrong. If I change C<< $x = 4; >> to C<< undef $x; $x = 4; >>, an error is correctly reported.

xV4▒V4x▒4Vx▒
from_string(4) failed...

ikegami avatar Mar 11 '14 16:03 ikegami

Better test case:

$ perl -Mlib=lib,thirdParty/lib/perl5 -MData::UUID -e'
    my $x = "3\0" x 16;
    print Data::UUID->new->from_string($x), "\n";
    print "done\n";
'
done

ikegami avatar Mar 11 '14 17:03 ikegami

Fix:

 void
 from_string(self,str) 
    uuid_context_t *self;
-   char           *str;
+   SV             *str_sv;
 ALIAS:
    Data::UUID::from_hexstring = F_HEX
    Data::UUID::from_b64string = F_B64
 PREINIT:
+   STRLEN         len;
+   char          *str = SvPV(str_sv, len);
    perl_uuid_t    uuid;
    char          *from, *to;
    int            c;
    unsigned int   i;
    unsigned char  buf[4];
 PPCODE:
    switch(ix) {
    case F_BIN:
    case F_STR:
    case F_HEX:
+      if (len != sizeof(perl_uuid_t)*2)
+         croak("from_string(%s) failed...\n", str);

Untested

ikegami avatar Mar 11 '14 17:03 ikegami

Oops, that "fix" only fixes the first code sample, but not the second. Unfortunately, I've already spent too much time on this today.

ikegami avatar Mar 11 '14 19:03 ikegami