[前][次][番号順一覧][スレッド一覧]

ruby-changes:45114

From: rhe <ko1@a...>
Date: Mon, 26 Dec 2016 15:32:05 +0900 (JST)
Subject: [ruby-changes:45114] rhe:r57187 (trunk): pack.c: avoid returning uninitialized String

rhe	2016-12-26 15:32:00 +0900 (Mon, 26 Dec 2016)

  New Revision: 57187

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=57187

  Log:
    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]

  Modified files:
    trunk/pack.c
    trunk/test/ruby/test_pack.rb
Index: test/ruby/test_pack.rb
===================================================================
--- test/ruby/test_pack.rb	(revision 57186)
+++ test/ruby/test_pack.rb	(revision 57187)
@@ -838,10 +838,18 @@ EXPECTED https://github.com/ruby/ruby/blob/trunk/test/ruby/test_pack.rb#L838
     assert_equal addr, [buf].pack('p')
   end
 
+  def test_unpack_with_block
+    ret = []; "ABCD".unpack("CCCC") {|v| ret << v }
+    assert_equal [65, 66, 67, 68], ret
+    ret = []; "A".unpack("B*") {|v| ret << v }
+    assert_equal ["01000001"], ret
+  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")
+    assert_equal "01000001", "A".unpack1("B*")
   end
 end
Index: pack.c
===================================================================
--- pack.c	(revision 57186)
+++ pack.c	(revision 57187)
@@ -1194,13 +1194,14 @@ pack_unpack_internal(VALUE str, VALUE fm https://github.com/ruby/ruby/blob/trunk/pack.c#L1194
 		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 fm https://github.com/ruby/ruby/blob/trunk/pack.c#L1215
 		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 fm https://github.com/ruby/ruby/blob/trunk/pack.c#L1236
 		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 fm https://github.com/ruby/ruby/blob/trunk/pack.c#L1245
 			bits = (unsigned char)*s++;
 		    *t++ = hexdigits[bits & 15];
 		}
+		UNPACK_PUSH(bitstr);
 	    }
 	    break;
 
@@ -1256,7 +1259,7 @@ pack_unpack_internal(VALUE str, VALUE fm https://github.com/ruby/ruby/blob/trunk/pack.c#L1259
 		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 fm https://github.com/ruby/ruby/blob/trunk/pack.c#L1268
 			bits = (unsigned char)*s++;
 		    *t++ = hexdigits[(bits >> 4) & 15];
 		}
+		UNPACK_PUSH(bitstr);
 	    }
 	    break;
 

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]