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

ruby-changes:47944

From: nobu <ko1@a...>
Date: Fri, 29 Sep 2017 17:00:52 +0900 (JST)
Subject: [ruby-changes:47944] nobu:r60059 (trunk): pack.c: unpack "M" may be ASCII only

nobu	2017-09-29 17:00:47 +0900 (Fri, 29 Sep 2017)

  New Revision: 60059

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

  Log:
    pack.c: unpack "M" may be ASCII only
    
    * pack.c (pack_unpack_internal): set ASCII only properly on "M",
      may be ASCII only.  [ruby-core:83055] [Bug #13949]

  Modified files:
    trunk/pack.c
    trunk/test/ruby/test_pack.rb
Index: pack.c
===================================================================
--- pack.c	(revision 60058)
+++ pack.c	(revision 60059)
@@ -1597,6 +1597,7 @@ pack_unpack_internal(VALUE str, VALUE fm https://github.com/ruby/ruby/blob/trunk/pack.c#L1597
 	    {
 		VALUE buf = infected_str_new(0, send - s, str);
 		char *ptr = RSTRING_PTR(buf), *ss = s;
+		int csum = 0;
 		int c1, c2;
 
 		while (s < send) {
@@ -1608,18 +1609,19 @@ pack_unpack_internal(VALUE str, VALUE fm https://github.com/ruby/ruby/blob/trunk/pack.c#L1609
 			    if ((c1 = hex2num(*s)) == -1) break;
 			    if (++s == send) break;
 			    if ((c2 = hex2num(*s)) == -1) break;
-			    *ptr++ = castchar(c1 << 4 | c2);
+			    csum |= *ptr++ = castchar(c1 << 4 | c2);
 			}
 		    }
 		    else {
-			*ptr++ = *s;
+			csum |= *ptr++ = *s;
 		    }
 		    s++;
 		    ss = s;
 		}
 		rb_str_set_len(buf, ptr - RSTRING_PTR(buf));
 		rb_str_buf_cat(buf, ss, send-ss);
-		ENCODING_CODERANGE_SET(buf, rb_ascii8bit_encindex(), ENC_CODERANGE_VALID);
+		csum = ISASCII(csum) ? ENC_CODERANGE_7BIT : ENC_CODERANGE_VALID;
+		ENCODING_CODERANGE_SET(buf, rb_ascii8bit_encindex(), csum);
 		UNPACK_PUSH(buf);
 	    }
 	    break;
Index: test/ruby/test_pack.rb
===================================================================
--- test/ruby/test_pack.rb	(revision 60058)
+++ test/ruby/test_pack.rb	(revision 60059)
@@ -686,6 +686,11 @@ EXPECTED https://github.com/ruby/ruby/blob/trunk/test/ruby/test_pack.rb#L686
     assert_equal(["pre=hoge"], "pre=hoge".unpack("M"))
     assert_equal(["pre==31after"], "pre==31after".unpack("M"))
     assert_equal(["pre===31after"], "pre===31after".unpack("M"))
+
+    bug = '[ruby-core:83055] [Bug #13949]'
+    s = "abcdef".unpack1("M")
+    assert_equal(Encoding::ASCII_8BIT, s.encoding)
+    assert_predicate(s, :ascii_only?, bug)
   end
 
   def test_pack_unpack_P2

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

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