Exception when compare integer against big integer
rexcheung21 opened this issue · comments
Rex Cheung commented
Following code in mRuby 3.2 and master branch causes ArgumentError exception. mRuby 3.1 is not affected since it does not have built-in bigint.
a=64
b=2**64
puts "a=#{a.class}"
puts "b=#{b.class}"
puts "num #{a < b}"
a=Integer
b=Integer
trace (most recent call last):
[1] ../../lgtv_2024_mruby32/test.rb:5
../../lgtv_2024_mruby32/test.rb:5:in <: comparison of Integer with Integer failed (ArgumentError)
There's no problem if the statement a < b
changed to b > a
.
The function (cmpnum()
in src/numeric.c) handles comparison handles the case the first parameter (v1) is big integer, but not the second one. A quick and dirty fix appears to solve the issue:
diff --git a/src/numeric.c b/src/numeric.c
index 4ad523f22..c32f0024d 100644
--- a/src/numeric.c
+++ b/src/numeric.c
@@ -2031,6 +2031,10 @@ cmpnum(mrb_state *mrb, mrb_value v1, mrb_value v2)
if (mrb_bigint_p(v1)) {
return mrb_bint_cmp(mrb, v1, v2);
}
+ if (mrb_bigint_p(v2)) {
+ mrb_int ret = mrb_bint_cmp(mrb, v2, v1);
+ return (-2 != ret)? -ret : ret;
+ }
#endif
#ifdef MRB_NO_FLOAT