summaryrefslogtreecommitdiff
path: root/pack.c
diff options
context:
space:
mode:
authornaruse <naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-03-11 10:44:13 +0000
committernaruse <naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-03-11 10:44:13 +0000
commit7b560e31be52b880711204df6992c84185eac6cf (patch)
tree34abe45e11ed87102464e309d3a060ea52ed5cdf /pack.c
parent6c93dee7ed9eddf55431ae734502bad335f7e50a (diff)
merge revision(s) 57187,57234: [Backport #13075]
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] test/ruby/test_pack.rb: fix test case added by r57187 The test case for String#unpack added by r57187 is not properly testing because the String will be filled after the block invocation. [ruby-core:78841] [Bug #13075] Thanks to nagachika for pointing this out: http://d.hatena.ne.jp/nagachika/20161226/ruby_trunk_changes_57184_57194#r57187 git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_2_4@57833 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'pack.c')
-rw-r--r--pack.c12
1 files changed, 8 insertions, 4 deletions
diff --git a/pack.c b/pack.c
index a8339c7219..d275a286b0 100644
--- a/pack.c
+++ b/pack.c
@@ -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));
+ 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';
}
+ UNPACK_PUSH(bitstr);
}
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));
+ 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';
}
+ UNPACK_PUSH(bitstr);
}
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));
+ 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];
}
+ UNPACK_PUSH(bitstr);
}
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));
+ 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];
}
+ UNPACK_PUSH(bitstr);
}
break;