Skip to content

Commit 5d85778

Browse files
authored
Ignore ndigits passed to BigDecimal(string, ndigits) (#385)
Don't allocate huge memory when huge ndigits is passed
1 parent d8bacc6 commit 5d85778

2 files changed

Lines changed: 7 additions & 2 deletions

File tree

ext/bigdecimal/bigdecimal.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3224,7 +3224,7 @@ rb_convert_to_BigDecimal(VALUE val, size_t digs, int raise_exception)
32243224
return rb_convert_to_BigDecimal(rb_complex_real(val), digs, raise_exception);
32253225
}
32263226
else if (RB_TYPE_P(val, T_STRING)) {
3227-
return rb_str_convert_to_BigDecimal(val, digs, raise_exception);
3227+
return rb_str_convert_to_BigDecimal(val, 0, raise_exception);
32283228
}
32293229

32303230
/* TODO: chheck to_d */
@@ -3238,7 +3238,7 @@ rb_convert_to_BigDecimal(VALUE val, size_t digs, int raise_exception)
32383238
}
32393239
return Qnil;
32403240
}
3241-
return rb_str_convert_to_BigDecimal(str, digs, raise_exception);
3241+
return rb_str_convert_to_BigDecimal(str, 0, raise_exception);
32423242
}
32433243

32443244
/* call-seq:

test/bigdecimal/test_bigdecimal.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,11 @@ def test_BigDecimal
113113
end
114114
end
115115

116+
def test_BigDecimal_ignore_digits
117+
assert_equal(1, BigDecimal(1, LIMITS["FIXNUM_MAX"]))
118+
assert_equal(1, BigDecimal('1', LIMITS["FIXNUM_MAX"]))
119+
end
120+
116121
def test_BigDecimal_bug7522
117122
bd = BigDecimal("1.12", 1)
118123
assert_same(bd, BigDecimal(bd))

0 commit comments

Comments
 (0)