Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix "Invalid float" parse error #897

Merged
merged 3 commits into from
Sep 1, 2023
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
6 changes: 5 additions & 1 deletion ext/oj/parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -875,7 +875,11 @@ oj_num_as_value(NumInfo ni) {
double d = strtod(ni->str, &end);

if ((long)ni->len != (long)(end - ni->str)) {
rb_raise(ni->pi->err_class, "Invalid float");
if (Qnil == ni->pi->err_class) {
rb_raise(oj_parse_error_class, "Invalid float");
} else {
rb_raise(ni->pi->err_class, "Invalid float");
}
}
rnum = rb_float_new(d);
}
Expand Down
10 changes: 10 additions & 0 deletions test/test_strict.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,16 @@ def test_float
dump_and_load(-2.48e100 * 1.0e10, false)
end

def test_invalid_float
begin
Oj.load("64ecb72d29191067f91ff95b")
rescue Oj::ParseError => e
assert(e.message == "Invalid float")
return
end
assert(false, "*** expected an exception")
end

def test_nan_dump
assert_equal('null', Oj.dump(0/0.0, :nan => :null))
assert_equal('3.3e14159265358979323846', Oj.dump(0/0.0, :nan => :huge))
Expand Down