From 23080d743e466b244f5719d343d1a517d4dfadf5 Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Sat, 21 Sep 2024 22:31:26 +0200 Subject: [PATCH] Fix segfault, handle symbol conversion correctly 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: https://github.com/rubyjs/mini_racer/issues/318 --- ext/mini_racer_extension/mini_racer_extension.cc | 6 ++++-- test/mini_racer_test.rb | 1 + 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/ext/mini_racer_extension/mini_racer_extension.cc b/ext/mini_racer_extension/mini_racer_extension.cc index 4b611f9..3ebcf1b 100644 --- a/ext/mini_racer_extension/mini_racer_extension.cc +++ b/ext/mini_racer_extension/mini_racer_extension.cc @@ -565,6 +565,7 @@ static VALUE convert_v8_to_ruby(Isolate* isolate, Local context, Isolate::Scope isolate_scope(isolate); HandleScope scope(isolate); + Context::Scope context_scope(context); StackCounter stackCounter(isolate); @@ -672,8 +673,9 @@ static VALUE convert_v8_to_ruby(Isolate* isolate, Local context, } if (value->IsSymbol()) { - v8::String::Utf8Value symbol_name(isolate, - Local::Cast(value)->Description(isolate)); + Local symbol = Local::Cast(value); + Local description = symbol->Description(isolate); + v8::String::Utf8Value symbol_name(isolate, description); VALUE str_symbol = rb_utf8_str_new(*symbol_name, symbol_name.length()); diff --git a/test/mini_racer_test.rb b/test/mini_racer_test.rb index 134021a..0cfbad7 100644 --- a/test/mini_racer_test.rb +++ b/test/mini_racer_test.rb @@ -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