Skip to content

Commit

Permalink
Fix set_global() use after free.
Browse files Browse the repository at this point in the history
  • Loading branch information
theduke committed Mar 15, 2021
1 parent 05998ab commit e759be0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -748,6 +748,9 @@ impl<'a> OwnedObjectRef<'a> {
}
}

// Set a property on an object.
// NOTE: this method takes ownership of the `JSValue`, so it must not be
// freed later.
unsafe fn set_property_raw(&self, name: &str, value: q::JSValue) -> Result<(), ExecutionError> {
let cname = make_cstring(name)?;
let ret = q::JS_SetPropertyStr(
Expand All @@ -765,7 +768,12 @@ impl<'a> OwnedObjectRef<'a> {

pub fn set_property(&self, name: &str, value: JsValue) -> Result<(), ExecutionError> {
let qval = self.value.context.serialize_value(value)?;
unsafe { self.set_property_raw(name, qval.value) }
unsafe {
self.set_property_raw(name, qval.value)?;
// set_property_raw takes ownership, so we must prevent a free.
std::mem::forget(qval);
}
Ok(())
}
}

Expand Down
7 changes: 7 additions & 0 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -605,3 +605,10 @@ fn test_console() {
]
);
}

#[test]
fn test_global_setter() {
let ctx = Context::new().unwrap();
ctx.set_global("a", "a").unwrap();
ctx.eval("a + 1").unwrap();
}

0 comments on commit e759be0

Please sign in to comment.