Skip to content

Commit 223a268

Browse files
committed
fix: dangling pointer
1 parent a27eb18 commit 223a268

1 file changed

Lines changed: 26 additions & 3 deletions

File tree

crates/jsshaker/src/module.rs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,6 @@ impl<'a> Analyzer<'a> {
201201
return;
202202
}
203203
module.initializing = true;
204-
let program = unsafe { &*module.program.get() };
205204
let variable_scope = module.variable_scope;
206205
let callee = module.callee;
207206

@@ -219,7 +218,19 @@ impl<'a> Analyzer<'a> {
219218
false,
220219
));
221220

222-
for node in &program.body {
221+
// First pass: handle imports and exports
222+
// Use index-based iteration because recursive calls to exec_module can cause
223+
// self.modules to reallocate, invalidating any borrowed references to program.
224+
let body_len = {
225+
let module = &self.modules.modules[module_id];
226+
let program = unsafe { &*module.program.get() };
227+
program.body.len()
228+
};
229+
230+
for i in 0..body_len {
231+
let module = &self.modules.modules[module_id];
232+
let program = unsafe { &*module.program.get() };
233+
let node = &program.body[i];
223234
match node {
224235
Statement::ImportDeclaration(node) => {
225236
self.init_import_declaration(node);
@@ -239,7 +250,19 @@ impl<'a> Analyzer<'a> {
239250
_ => {}
240251
}
241252
}
242-
for node in &program.body {
253+
254+
// Second pass: initialize statements
255+
// Re-access body length and iterate by index
256+
let body_len = {
257+
let module = &self.modules.modules[module_id];
258+
let program = unsafe { &*module.program.get() };
259+
program.body.len()
260+
};
261+
262+
for i in 0..body_len {
263+
let module = &self.modules.modules[module_id];
264+
let program = unsafe { &*module.program.get() };
265+
let node = &program.body[i];
243266
self.init_statement(node);
244267
}
245268

0 commit comments

Comments
 (0)