ruby-changes:63204
From: Peter <ko1@a...>
Date: Wed, 30 Sep 2020 00:32:17 +0900 (JST)
Subject: [ruby-changes:63204] f7bd9f0750 (master): Fix unsigned int overflow in error message for chr
https://git.ruby-lang.org/ruby.git/commit/?id=f7bd9f0750 From f7bd9f075030e5fa876320c1624a80685a636e82 Mon Sep 17 00:00:00 2001 From: Peter Zhu <peter@p...> Date: Mon, 28 Sep 2020 17:35:27 -0400 Subject: Fix unsigned int overflow in error message for chr The error message has an integer overflow because it treats an unsigned int as a signed int. Before: ``` > 3_000_000_000.chr -1294967296 out of char range (RangeError) ``` After: ``` > 3_000_000_000.chr 3000000000 out of char range (RangeError) ``` Redmine ticket: https://bugs.ruby-lang.org/issues/17186 diff --git a/numeric.c b/numeric.c index 387e46f..6d4eb86 100644 --- a/numeric.c +++ b/numeric.c @@ -3417,7 +3417,7 @@ int_chr(int argc, VALUE *argv, VALUE num) https://github.com/ruby/ruby/blob/trunk/numeric.c#L3417 if (0xff < i) { enc = rb_default_internal_encoding(); if (!enc) { - rb_raise(rb_eRangeError, "%d out of char range", i); + rb_raise(rb_eRangeError, "%u out of char range", i); } goto decode; } diff --git a/test/ruby/test_integer.rb b/test/ruby/test_integer.rb index a111698..2755987 100644 --- a/test/ruby/test_integer.rb +++ b/test/ruby/test_integer.rb @@ -260,6 +260,7 @@ class TestInteger < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_integer.rb#L260 assert_equal("a", "a".ord.chr) assert_raise(RangeError) { (-1).chr } assert_raise(RangeError) { 0x100.chr } + assert_raise_with_message(RangeError, "3000000000 out of char range") { 3_000_000_000.chr } end def test_upto -- cgit v0.10.2 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/