Skip to content

Commit

Permalink
Fix "Invalid float" parse error (#897)
Browse files Browse the repository at this point in the history
* Add test that passes with v3.14.2 and fails from v3.15+

* Fix 'Invalid float' error

* clang-format -i ext/oj/parse.c
  • Loading branch information
jasonpenny authored Sep 1, 2023
1 parent b399b8a commit 1cacb87
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
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

0 comments on commit 1cacb87

Please sign in to comment.