Skip to content

Commit

Permalink
Fix segfault, handle symbol conversion correctly (#319)
Browse files Browse the repository at this point in the history
Symbol::Description() normally returns a string, but when the symbol
does not have a description, it returns undefined.

The String::Utf8Value constructor requires that a v8::Context is active
for the ToString operation it executes. ToString is a no-op for strings
(hence no crash) but not for undefined.

Add a Context::Scope to fix that.

Fixes: #318
  • Loading branch information
bnoordhuis committed Sep 21, 2024
1 parent 5a03255 commit 8c13559
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
6 changes: 4 additions & 2 deletions ext/mini_racer_extension/mini_racer_extension.cc
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,7 @@ static VALUE convert_v8_to_ruby(Isolate* isolate, Local<Context> context,

Isolate::Scope isolate_scope(isolate);
HandleScope scope(isolate);
Context::Scope context_scope(context);

StackCounter stackCounter(isolate);

Expand Down Expand Up @@ -672,8 +673,9 @@ static VALUE convert_v8_to_ruby(Isolate* isolate, Local<Context> context,
}

if (value->IsSymbol()) {
v8::String::Utf8Value symbol_name(isolate,
Local<Symbol>::Cast(value)->Description(isolate));
Local<Symbol> symbol = Local<Symbol>::Cast(value);
Local<Value> description = symbol->Description(isolate);
v8::String::Utf8Value symbol_name(isolate, description);

VALUE str_symbol = rb_utf8_str_new(*symbol_name, symbol_name.length());

Expand Down
1 change: 1 addition & 0 deletions test/mini_racer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -975,6 +975,7 @@ def test_pipe_leak
def test_symbol_support
context = MiniRacer::Context.new()
assert_equal :foo, context.eval("Symbol('foo')")
assert_equal :undefined, context.eval("Symbol()") # should not crash
end

def test_cyclical_object_js
Expand Down

0 comments on commit 8c13559

Please sign in to comment.