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 crash on invalid date object #317

Merged
merged 1 commit into from
Sep 21, 2024
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
30 changes: 26 additions & 4 deletions ext/mini_racer_extension/mini_racer_extension.cc
Original file line number Diff line number Diff line change
Expand Up @@ -586,10 +586,6 @@ static VALUE convert_v8_to_ruby(Isolate* isolate, Local<Context> context,
return INT2FIX(value->Int32Value(context).ToChecked());
}

if (value->IsNumber()) {
return rb_float_new(value->NumberValue(context).ToChecked());
}

if (value->IsTrue()) {
return Qtrue;
}
Expand All @@ -598,6 +594,25 @@ static VALUE convert_v8_to_ruby(Isolate* isolate, Local<Context> context,
return Qfalse;
}

struct State {
Isolate* isolate;
Local<Value> value;
Local<Context> context;
} state = {isolate, value, context};

// calls to rb_*() functions can raise exceptions and longjmp,
// and therefore must be inside this protected block
auto can_raise = [](VALUE arg) -> VALUE {
State* state =
reinterpret_cast<State*>(static_cast<uintptr_t>(RB_NUM2ULL(arg)));
Isolate* isolate = state->isolate;
Local<Value> value = state->value;
Local<Context> context = state->context;

if (value->IsNumber()) {
return rb_float_new(value->NumberValue(context).ToChecked());
}

if (value->IsArray()) {
VALUE rb_array = rb_ary_new();
Local<Array> arr = Local<Array>::Cast(value);
Expand Down Expand Up @@ -673,6 +688,13 @@ static VALUE convert_v8_to_ruby(Isolate* isolate, Local<Context> context,
Local<String> rstr = rstr_maybe.ToLocalChecked();
return rb_utf8_str_new(*String::Utf8Value(isolate, rstr), rstr->Utf8Length(isolate));
}
};

// this is kind of slow because RB_ULL2NUM allocs when the pointer
// doesn't fit in a fixnum but yolo'ing and reinterpret_casting the
// pointer to a VALUE is probably not very sound
VALUE arg = RB_ULL2NUM(reinterpret_cast<uintptr_t>(&state));
return rb_protect(can_raise, arg, nullptr);
}

static VALUE convert_v8_to_ruby(Isolate* isolate,
Expand Down
10 changes: 10 additions & 0 deletions test/mini_racer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,16 @@ def test_return_hash
)
end

def test_date_nan
# NoMethodError: undefined method `source_location' for "<internal:core>
# core/float.rb:114:in `to_i'":Thread::Backtrace::Location
if RUBY_ENGINE == "truffleruby"
skip "TruffleRuby bug"
end
context = MiniRacer::Context.new
context.eval("new Date(NaN)") # should not crash process
end

def test_return_date
context = MiniRacer::Context.new
test_time = Time.new
Expand Down
Loading