diff options
author | rhe <rhe@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2016-12-26 06:32:00 +0000 |
---|---|---|
committer | rhe <rhe@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2016-12-26 06:32:00 +0000 |
commit | b65b41861f590ae14ec932e323337923ce12ca18 () | |
tree | 2431210a9f9abadfcbb12c9638d73145d7e2ee9c | |
parent | 256d8c9ecffbcd8f4fe7562b866fcd55f1d445e7 (diff) |
pack.c: avoid returning uninitialized String
Fix unpacking with 'b', 'B', 'h' and 'H' format. Do not return an uninitialized String to Ruby before filling the content bytes. Fixes r11175 ("pack.c (pack_unpack): execute block if given with unpacked value instead of creating an array", 2006-10-15). [ruby-core:78841] [Bug #13075] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@57187 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r-- | pack.c | 12 | ||||
-rw-r--r-- | test/ruby/test_pack.rb | 8 |
2 files changed, 16 insertions, 4 deletions
@@ -1194,13 +1194,14 @@ pack_unpack_internal(VALUE str, VALUE fmt, int mode) if (p[-1] == '*' || len > (send - s) * 8) len = (send - s) * 8; bits = 0; - UNPACK_PUSH(bitstr = rb_usascii_str_new(0, len)); t = RSTRING_PTR(bitstr); for (i=0; i<len; i++) { if (i & 7) bits >>= 1; else bits = (unsigned char)*s++; *t++ = (bits & 1) ? '1' : '0'; } } break; @@ -1214,13 +1215,14 @@ pack_unpack_internal(VALUE str, VALUE fmt, int mode) if (p[-1] == '*' || len > (send - s) * 8) len = (send - s) * 8; bits = 0; - UNPACK_PUSH(bitstr = rb_usascii_str_new(0, len)); t = RSTRING_PTR(bitstr); for (i=0; i<len; i++) { if (i & 7) bits <<= 1; else bits = (unsigned char)*s++; *t++ = (bits & 128) ? '1' : '0'; } } break; @@ -1234,7 +1236,7 @@ pack_unpack_internal(VALUE str, VALUE fmt, int mode) if (p[-1] == '*' || len > (send - s) * 2) len = (send - s) * 2; bits = 0; - UNPACK_PUSH(bitstr = rb_usascii_str_new(0, len)); t = RSTRING_PTR(bitstr); for (i=0; i<len; i++) { if (i & 1) @@ -1243,6 +1245,7 @@ pack_unpack_internal(VALUE str, VALUE fmt, int mode) bits = (unsigned char)*s++; *t++ = hexdigits[bits & 15]; } } break; @@ -1256,7 +1259,7 @@ pack_unpack_internal(VALUE str, VALUE fmt, int mode) if (p[-1] == '*' || len > (send - s) * 2) len = (send - s) * 2; bits = 0; - UNPACK_PUSH(bitstr = rb_usascii_str_new(0, len)); t = RSTRING_PTR(bitstr); for (i=0; i<len; i++) { if (i & 1) @@ -1265,6 +1268,7 @@ pack_unpack_internal(VALUE str, VALUE fmt, int mode) bits = (unsigned char)*s++; *t++ = hexdigits[(bits >> 4) & 15]; } } break; @@ -838,10 +838,18 @@ EXPECTED assert_equal addr, [buf].pack('p') end def test_unpack1 assert_equal 65, "A".unpack1("C") assert_equal 68, "ABCD".unpack1("x3C") assert_equal 0x3042, "\u{3042 3044 3046}".unpack1("U*") assert_equal "hogefuga", "aG9nZWZ1Z2E=".unpack1("m") end end |