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

ruby-changes:35916

From: akr <ko1@a...>
Date: Fri, 17 Oct 2014 18:49:39 +0900 (JST)
Subject: [ruby-changes:35916] akr:r47997 (trunk): * Avoid undefined behaviors found by gcc -fsanitize=undefined.

akr	2014-10-17 18:49:28 +0900 (Fri, 17 Oct 2014)

  New Revision: 47997

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

  Log:
    * Avoid undefined behaviors found by gcc -fsanitize=undefined.
      gcc (Debian 4.9.1-16) 4.9.1
    
    * string.c (rb_str_sum): Avoid undefined behavior.

  Modified files:
    trunk/ChangeLog
    trunk/string.c
    trunk/test/ruby/test_string.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 47996)
+++ ChangeLog	(revision 47997)
@@ -1,3 +1,10 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Fri Oct 17 17:50:10 2014  Tanaka Akira  <akr@f...>
+
+	* Avoid undefined behaviors found by gcc -fsanitize=undefined.
+	  gcc (Debian 4.9.1-16) 4.9.1
+
+	* string.c (rb_str_sum): Avoid undefined behavior.
+
 Fri Oct 17 17:43:50 2014  Tanaka Akira  <akr@f...>
 
 	* Avoid undefined behaviors found by gcc -fsanitize=undefined.
Index: string.c
===================================================================
--- string.c	(revision 47996)
+++ string.c	(revision 47997)
@@ -7571,7 +7571,7 @@ rb_str_ord(VALUE s) https://github.com/ruby/ruby/blob/trunk/string.c#L7571
  *
  *  Returns a basic <em>n</em>-bit checksum of the characters in <i>str</i>,
  *  where <em>n</em> is the optional <code>Fixnum</code> parameter, defaulting
- *  to 16. The result is simply the sum of the binary value of each character in
+ *  to 16. The result is simply the sum of the binary value of each byte in
  *  <i>str</i> modulo <code>2**n - 1</code>. This is not a particularly good
  *  checksum.
  */
@@ -7592,6 +7592,8 @@ rb_str_sum(int argc, VALUE *argv, VALUE https://github.com/ruby/ruby/blob/trunk/string.c#L7592
     else {
 	rb_scan_args(argc, argv, "01", &vbits);
 	bits = NUM2INT(vbits);
+        if (bits < 0)
+            bits = 0;
     }
     ptr = p = RSTRING_PTR(str);
     len = RSTRING_LEN(str);
Index: test/ruby/test_string.rb
===================================================================
--- test/ruby/test_string.rb	(revision 47996)
+++ test/ruby/test_string.rb	(revision 47997)
@@ -1584,6 +1584,8 @@ class TestString < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_string.rb#L1584
     assert_equal(16, n.sum(17))
     n[0] = 2.chr
     assert_not_equal(15, n.sum)
+    assert_equal(17, n.sum(0))
+    assert_equal(17, n.sum(-1))
   end
 
   def check_sum(str, bits=16)
@@ -1598,7 +1600,7 @@ class TestString < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_string.rb#L1600
     assert_equal(294, "abc".sum)
     check_sum("abc")
     check_sum("\x80")
-    0.upto(70) {|bits|
+    -3.upto(70) {|bits|
       check_sum("xyz", bits)
     }
   end

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

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