ruby-changes:21693
From: kosaki <ko1@a...>
Date: Mon, 14 Nov 2011 12:52:08 +0900 (JST)
Subject: [ruby-changes:21693] kosaki:r33742 (trunk): * bignum.c (rb_big2ull): fix off-by-twice bug of NUM2ULL.
kosaki 2011-11-14 12:51:56 +0900 (Mon, 14 Nov 2011) New Revision: 33742 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=33742 Log: * bignum.c (rb_big2ull): fix off-by-twice bug of NUM2ULL. * test/-ext-/num2int/test_num2int.rb (class TestNum2int): fix a testcase too. Modified files: trunk/ChangeLog trunk/bignum.c trunk/test/-ext-/num2int/test_num2int.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 33741) +++ ChangeLog (revision 33742) @@ -1,3 +1,9 @@ +Sun Nov 13 10:23:48 2011 KOSAKI Motohiro <kosaki.motohiro@g...> + + * bignum.c (rb_big2ull): fix off-by-twice bug of NUM2ULL. + * test/-ext-/num2int/test_num2int.rb (class TestNum2int): + fix a testcase too. + Sun Nov 13 10:22:44 2011 KOSAKI Motohiro <kosaki.motohiro@g...> * test/-ext-/num2int/test_num2int.rb (class TestNum2int): Index: bignum.c =================================================================== --- bignum.c (revision 33741) +++ bignum.c (revision 33742) @@ -1257,8 +1257,14 @@ { unsigned LONG_LONG num = big2ull(x, "unsigned long long"); - if (!RBIGNUM_SIGN(x)) - return (VALUE)(-(SIGNED_VALUE)num); + if (!RBIGNUM_SIGN(x)) { + VALUE v = (VALUE)(-(SIGNED_VALUE)num); + + /* FIXNUM_MIN-1 .. LLONG_MIN mapped into 0xbfffffffffffffff .. LONG_MAX+1 */ + if (v <= LLONG_MAX) + rb_raise(rb_eRangeError, "bignum out of range of unsigned long long"); + return v; + } return num; } Index: test/-ext-/num2int/test_num2int.rb =================================================================== --- test/-ext-/num2int/test_num2int.rb (revision 33741) +++ test/-ext-/num2int/test_num2int.rb (revision 33742) @@ -162,22 +162,13 @@ assert_output(ULLONG_MAX.to_s) do Num2int.print_num2ull(-1) end - assert_output((LLONG_MAX+2).to_s) do - Num2int.print_num2ull(LLONG_MIN+1) + assert_output((LLONG_MAX+1).to_s) do + Num2int.print_num2ull(LLONG_MIN) end - - # maybe bug - assert_output((LLONG_MAX).to_s) do + assert_raise(RangeError) do Num2int.print_num2ull(LLONG_MIN-1) end - # maybe bug - assert_output(1.to_s) do - Num2int.print_num2ull(LLONG_MIN*2+1) - end assert_raise(RangeError) do - Num2int.print_num2ull(LLONG_MIN*2) - end - assert_raise(RangeError) do Num2int.print_num2ull(ULLONG_MAX+1) end assert_output((ULLONG_HALF+FIXNUM_MAX+1).to_s) do -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/