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

ruby-changes:35918

From: akr <ko1@a...>
Date: Fri, 17 Oct 2014 23:28:44 +0900 (JST)
Subject: [ruby-changes:35918] akr:r47999 (trunk): * pack.c (pack_unpack): Add casts for char references for 'u'.

akr	2014-10-17 23:28:34 +0900 (Fri, 17 Oct 2014)

  New Revision: 47999

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

  Log:
    * pack.c (pack_unpack): Add casts for char references for 'u'.
      Fix line ending recognition algorithm.

  Modified files:
    trunk/ChangeLog
    trunk/pack.c
    trunk/test/ruby/test_pack.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 47998)
+++ ChangeLog	(revision 47999)
@@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Fri Oct 17 22:47:11 2014  Tanaka Akira  <akr@f...>
+
+	* pack.c (pack_unpack): Add casts for char references for 'u'.
+	  Fix line ending recognition algorithm.
+
 Fri Oct 17 21:49:52 2014  Tanaka Akira  <akr@f...>
 
 	* pack.c (pack_unpack): Add casts for char references for 'b' and 'h'.
Index: pack.c
===================================================================
--- pack.c	(revision 47998)
+++ pack.c	(revision 47999)
@@ -1612,12 +1612,12 @@ pack_unpack(VALUE str, VALUE fmt) https://github.com/ruby/ruby/blob/trunk/pack.c#L1612
 		char *ptr = RSTRING_PTR(buf);
 		long total = 0;
 
-		while (s < send && *s > ' ' && *s < 'a') {
+		while (s < send && (unsigned char)*s > ' ' && (unsigned char)*s < 'a') {
 		    long a,b,c,d;
-		    char hunk[4];
+		    char hunk[3];
+
+		    len = ((unsigned char)*s++ - ' ') & 077;
 
-		    hunk[3] = '\0';
-		    len = (*s++ - ' ') & 077;
 		    total += len;
 		    if (total > RSTRING_LEN(buf)) {
 			len -= total - RSTRING_LEN(buf);
@@ -1627,20 +1627,20 @@ pack_unpack(VALUE str, VALUE fmt) https://github.com/ruby/ruby/blob/trunk/pack.c#L1627
 		    while (len > 0) {
 			long mlen = len > 3 ? 3 : len;
 
-			if (s < send && *s >= ' ')
-			    a = (*s++ - ' ') & 077;
+			if (s < send && (unsigned char)*s >= ' ' && (unsigned char)*s < 'a')
+			    a = ((unsigned char)*s++ - ' ') & 077;
 			else
 			    a = 0;
-			if (s < send && *s >= ' ')
-			    b = (*s++ - ' ') & 077;
+			if (s < send && (unsigned char)*s >= ' ' && (unsigned char)*s < 'a')
+			    b = ((unsigned char)*s++ - ' ') & 077;
 			else
 			    b = 0;
-			if (s < send && *s >= ' ')
-			    c = (*s++ - ' ') & 077;
+			if (s < send && (unsigned char)*s >= ' ' && (unsigned char)*s < 'a')
+			    c = ((unsigned char)*s++ - ' ') & 077;
 			else
 			    c = 0;
-			if (s < send && *s >= ' ')
-			    d = (*s++ - ' ') & 077;
+			if (s < send && (unsigned char)*s >= ' ' && (unsigned char)*s < 'a')
+			    d = ((unsigned char)*s++ - ' ') & 077;
 			else
 			    d = 0;
 			hunk[0] = (char)(a << 2 | b >> 4);
@@ -1650,10 +1650,10 @@ pack_unpack(VALUE str, VALUE fmt) https://github.com/ruby/ruby/blob/trunk/pack.c#L1650
 			ptr += mlen;
 			len -= mlen;
 		    }
-		    if (*s == '\r') s++;
-		    if (*s == '\n') s++;
-		    else if (s < send && (s+1 == send || s[1] == '\n'))
-			s += 2;	/* possible checksum byte */
+		    if (s < send && (unsigned char)*s != '\r' && *s != '\n')
+			s++;	/* possible checksum byte */
+		    if (s < send && *s == '\r') s++;
+		    if (s < send && *s == '\n') s++;
 		}
 
 		rb_str_set_len(buf, total);
Index: test/ruby/test_pack.rb
===================================================================
--- test/ruby/test_pack.rb	(revision 47998)
+++ test/ruby/test_pack.rb	(revision 47999)
@@ -526,6 +526,11 @@ EXPECTED https://github.com/ruby/ruby/blob/trunk/test/ruby/test_pack.rb#L526
     assert_equal(["a"*46], "M86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A\n!80``\n".unpack("u"))
     assert_equal(["abcdefghi"], "&86)C9&5F\n#9VAI\n".unpack("u"))
 
+    assert_equal(["abcdef"], "#86)C\n#9&5F\n".unpack("u"))
+    assert_equal(["abcdef"], "#86)CX\n#9&5FX\n".unpack("u")) # X is a (dummy) checksum.
+    assert_equal(["abcdef"], "#86)C\r\n#9&5F\r\n".unpack("u"))
+    assert_equal(["abcdef"], "#86)CX\r\n#9&5FX\r\n".unpack("u")) # X is a (dummy) checksum.
+
     assert_equal(["\x00"], "\"\n".unpack("u"))
     assert_equal(["\x00"], "! \r \n".unpack("u"))
   end

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

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