From: Ton Hospel Date: Sat, 29 Jan 2005 13:24:55 +0000 (+0000) Subject: Re: encoding neutral unpack X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=d6d3e8bddad8c105fc1972d4d9a8298ad3f73f11;p=p5sagit%2Fp5-mst-13.2.git Re: encoding neutral unpack From: perl5-porters[at]ton.iguana.be (Ton Hospel) Message-ID: Ensure that with the C format, unpack checksums don't get overflowed. (plus a regression test) p4raw-id: //depot/perl@23922 --- diff --git a/pp_pack.c b/pp_pack.c index cb3dd89..97e0a06 100644 --- a/pp_pack.c +++ b/pp_pack.c @@ -1183,7 +1183,10 @@ S_unpack_rec(pTHX_ register tempsym_t* symptr, register char *s, char *strbeg, c uchar_checksum: while (len-- > 0) { auint = *s++ & 255; - cuv += auint; + if (checksum > bits_in_uv) + cdouble += (NV)auint; + else + cuv += auint; } } else { @@ -1703,7 +1706,7 @@ S_unpack_rec(pTHX_ register tempsym_t* symptr, register char *s, char *strbeg, c if (checksum) { if (strchr("fFdD", TYPE_NO_MODIFIERS(datumtype)) || (checksum > bits_in_uv && - strchr("csSiIlLnNUvVqQjJ", TYPE_NO_MODIFIERS(datumtype))) ) { + strchr("cCsSiIlLnNUvVqQjJ", TYPE_NO_MODIFIERS(datumtype))) ) { NV trouble; adouble = (NV) (1 << (checksum & 15)); diff --git a/t/op/pack.t b/t/op/pack.t index d30ae94..3fc4cfd 100755 --- a/t/op/pack.t +++ b/t/op/pack.t @@ -12,7 +12,7 @@ my $no_endianness = $] > 5.009 ? '' : my $no_signedness = $] > 5.009 ? '' : "Signed/unsigned pack modifiers not available on this perl"; -plan tests => 13856; +plan tests => 13857; use strict; use warnings; @@ -1498,4 +1498,7 @@ is(unpack('c'), 65, "one-arg unpack (change #18751)"); # defaulting to $_ my (@x) = unpack("b10a", "abcd"); my (@y) = unpack("%b10a", "abcd"); is($x[1], $y[1], "checksum advance ok"); + + # verify that the checksum is not overflowed with C0 + is(unpack("C0%128U", "abcd"), unpack("U0%128U", "abcd"), "checksum not overflowed"); }