B::hash() should only work on byte sequences.
authorNicholas Clark <nick@ccl4.org>
Sun, 24 Oct 2010 15:00:23 +0000 (17:00 +0200)
committerNicholas Clark <nick@ccl4.org>
Sun, 24 Oct 2010 15:00:23 +0000 (17:00 +0200)
ext/B/B.xs
ext/B/t/b.t

index 28ad952..d71f587 100644 (file)
@@ -781,7 +781,7 @@ hash(sv)
     CODE:
        STRLEN len;
        U32 hash = 0;
-       const char *s = SvPV(sv, len);
+       const char *s = SvPVbyte(sv, len);
        PERL_HASH(hash, s, len);
        ST(0) = sv_2mortal(Perl_newSVpvf(aTHX_ "0x%"UVxf, (UV)hash));
 
index 19694a0..de306af 100644 (file)
@@ -163,6 +163,15 @@ is(B::opnumber("pp_null"), 0, "Testing opnumber with opname (pp_null)");
 
     like(B::hash("\0" x $_), qr/\A0x0+\z/, "Testing B::hash(\"0\" x $_)")
         for 0..19;
+
+    $hash = eval {B::hash(chr 256)};
+    is($hash, undef, "B::hash() refuses non-octets");
+    like($@, qr/^Wide character in subroutine entry/);
+
+    $hash = B::hash(chr 163);
+    my $str = chr(163) . chr 256;
+    chop $str;
+    is(B::hash($str), $hash, 'B::hash() with chr 128-256 is well-behaved');
 }
 {
     is(B::cstring(undef), '0', "Testing B::cstring(undef)");