Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions ext/bigdecimal/bigdecimal.c
Original file line number Diff line number Diff line change
Expand Up @@ -2846,6 +2846,8 @@ rb_cstr_convert_to_BigDecimal(const char *c_str, int raise_exception)
static inline VALUE
rb_str_convert_to_BigDecimal(VALUE val, int raise_exception)
{
StringValue(val);
rb_must_asciicompat(val);
if (!raise_exception && memchr(RSTRING_PTR(val), '\0', RSTRING_LEN(val))) return Qnil;
const char *c_str = StringValueCStr(val);
VALUE bd = rb_cstr_convert_to_BigDecimal(c_str, raise_exception);
Expand Down Expand Up @@ -3012,6 +3014,12 @@ f_BigDecimal(int argc, VALUE *argv, VALUE self)
static VALUE
BigDecimal_s_interpret_loosely(VALUE klass, VALUE str)
{
StringValue(str);
rb_must_asciicompat(str);
/* Like String#to_f, ignore everything after an embedded NUL */
const char *p = RSTRING_PTR(str);
const char *nul = memchr(p, '\0', RSTRING_LEN(str));
if (nul) str = rb_str_subseq(str, 0, nul - p);
char const *c_str = StringValueCStr(str);
NULLABLE_BDVALUE v = CreateFromString(c_str, klass, false, true);
RB_GC_GUARD(str);
Expand Down
6 changes: 6 additions & 0 deletions test/bigdecimal/test_bigdecimal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@ def test_BigDecimal_issue_192
assert_equal(BigDecimal(int), big, "[ruby/bigdecimal#192]")
end

def test_BigDecimal_with_ascii_incompatible_string
str = "123".encode("UTF-16LE")
assert_raise(Encoding::CompatibilityError) { BigDecimal(str) }
assert_raise(Encoding::CompatibilityError) { BigDecimal(str, exception: false) }
end

def test_BigDecimal_with_invalid_string
[
'', '.', 'e1', 'd1', '.e', '.d', '1.e', '1.d', '.1e', '.1d',
Expand Down
3 changes: 3 additions & 0 deletions test/bigdecimal/test_bigdecimal_util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ def test_String_to_d
assert_equal(BigDecimal('1.2'), "1.2.3".to_d)
assert_equal(BigDecimal('1'), "1.".to_d)
assert_equal(BigDecimal('1'), "1e".to_d)
assert_equal(BigDecimal('1'), "1\0 2".to_d)
assert_equal(BigDecimal('0'), "\0 1".to_d)
assert_raise(Encoding::CompatibilityError) { "1".encode("UTF-16LE").to_d }

assert("2.5".to_d.frozen?)
end
Expand Down