From b65b41861f590ae14ec932e323337923ce12ca18 Mon Sep 17 00:00:00 2001 From: rhe Date: Mon, 26 Dec 2016 06:32:00 +0000 Subject: 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 --- pack.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) (limited to 'pack.c') 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>= 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 (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 (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> 4) & 15]; } + UNPACK_PUSH(bitstr); } break; -- cgit v1.2.3