Skip to content

Commit 03c692e

Browse files
[cli] Fix the UAF with by-value receivers and --weak-refs (#2448)
Fixes #2447
1 parent de2a5d7 commit 03c692e

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

crates/cli-support/src/js/binding.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -124,15 +124,14 @@ impl<'a, 'b> Builder<'a, 'b> {
124124
drop(params.next());
125125
if js.cx.config.debug {
126126
js.prelude(
127-
"if (this.ptr == 0) throw new Error('Attempt to use a moved value');\n",
127+
"if (this.ptr == 0) throw new Error('Attempt to use a moved value');",
128128
);
129129
}
130130
if consumes_self {
131-
js.prelude("var ptr = this.ptr;");
132-
js.prelude("this.ptr = 0;");
133-
js.args.push("ptr".to_string());
131+
js.prelude("const ptr = this.__destroy_into_raw();");
132+
js.args.push("ptr".into());
134133
} else {
135-
js.args.push("this.ptr".to_string());
134+
js.args.push("this.ptr".into());
136135
}
137136
}
138137
None => {}

crates/cli-support/src/js/mod.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -850,10 +850,15 @@ impl<'a> Context<'a> {
850850

851851
dst.push_str(&format!(
852852
"
853-
free() {{
853+
__destroy_into_raw() {{
854854
const ptr = this.ptr;
855855
this.ptr = 0;
856856
{}
857+
return ptr;
858+
}}
859+
860+
free() {{
861+
const ptr = this.__destroy_into_raw();
857862
wasm.{}(ptr);
858863
}}
859864
",

0 commit comments

Comments
 (0)