aboutsummaryrefslogtreecommitdiffstats
path: root/string.c
diff options
context:
space:
mode:
authorakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2014-10-17 09:49:28 +0000
committerakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2014-10-17 09:49:28 +0000
commit0d4185f30e360b078d663acd8f4dfda95fe2aae9 (patch)
tree5df4391258496199530f6875163a3e98e2e84706 /string.c
parent1efb3c31b731e99627bbc0da13dfd3463bb67c67 (diff)
downloadruby-0d4185f30e360b078d663acd8f4dfda95fe2aae9.tar.gz
* 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. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@47997 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'string.c')
-rw-r--r--string.c4
1 files changed, 3 insertions, 1 deletions
diff --git a/string.c b/string.c
index c2501f99b1..b433a21446 100644
--- a/string.c
+++ b/string.c
@@ -7571,7 +7571,7 @@ rb_str_ord(VALUE s)
*
* 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 str)
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);