Skip to content

Commit b1147f1

Browse files
Fix use-after-free in JS::Object#[]= for to_js'ed assigned value
1 parent 08a9b68 commit b1147f1

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

ext/js/js-core.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,11 +182,13 @@ static VALUE _rb_js_obj_aref(VALUE obj, VALUE key) {
182182
*/
183183
static VALUE _rb_js_obj_aset(VALUE obj, VALUE key, VALUE val) {
184184
struct jsvalue *p = check_jsvalue(obj);
185-
struct jsvalue *v = check_jsvalue(_rb_js_try_convert(rb_mJS, val));
185+
VALUE rv = _rb_js_try_convert(rb_mJS, val);
186+
struct jsvalue *v = check_jsvalue(rv);
186187
rb_js_abi_host_string_t key_abi_str;
187188
key = rb_obj_as_string(key);
188189
rstring_to_abi_string(key, &key_abi_str);
189190
rb_js_abi_host_reflect_set(p->abi, &key_abi_str, v->abi);
191+
RB_GC_GUARD(rv);
190192
return val;
191193
}
192194

packages/npm-packages/ruby-wasm-wasi/test/unit/test_object.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,4 +203,10 @@ def test_member_set
203203
object["foo"] = 42
204204
assert_equal 42.to_s, object["foo"].to_s
205205
end
206+
207+
def test_member_set_with_stress_gc
208+
GC.stress = true
209+
JS.global[:tmp] = "1"
210+
GC.stress = false
211+
end
206212
end

0 commit comments

Comments
 (0)