Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Macro segmentation faults #6756

Merged
merged 7 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 11 additions & 4 deletions src/js_ast.zig
Original file line number Diff line number Diff line change
Expand Up @@ -7278,8 +7278,9 @@ pub const Macro = struct {

exception_holder = Zig.ZigException.Holder.init();
var js_args: []JSC.JSValue = &.{};
var js_processed_args_len: usize = 0;
defer {
for (js_args[0 .. js_args.len - @as(usize, @intFromBool(!javascript_object.isEmpty()))]) |arg| {
for (js_args[0 .. js_processed_args_len - @as(usize, @intFromBool(!javascript_object.isEmpty()))]) |arg| {
I-A-S marked this conversation as resolved.
Show resolved Hide resolved
arg.unprotect();
}

Expand All @@ -7292,12 +7293,18 @@ pub const Macro = struct {
.e_call => |call| {
const call_args: []Expr = call.args.slice();
js_args = try allocator.alloc(JSC.JSValue, call_args.len + @as(usize, @intFromBool(!javascript_object.isEmpty())));
js_processed_args_len = js_args.len;

for (call_args, js_args[0..call_args.len]) |in, *out| {
const value = try in.toJS(
for (0.., call_args, js_args[0..call_args.len]) |i, in, *out| {
const value = in.toJS(
allocator,
globalObject,
);
) catch |e| {
// Keeping a separate variable instead of modifying js_args.len
// due to allocator.free call in defer
js_processed_args_len = i;
return e;
};
value.protect();
out.* = value;
}
Expand Down
2 changes: 1 addition & 1 deletion src/js_parser.zig
Original file line number Diff line number Diff line change
Expand Up @@ -16389,7 +16389,7 @@ fn NewParser_(
p.log.addError(p.source, expr.loc, "macro threw exception") catch unreachable;
}
} else {
p.log.addErrorFmt(p.source, expr.loc, p.allocator, "{s} error in macro", .{@errorName(err)}) catch unreachable;
p.log.addErrorFmt(p.source, expr.loc, p.allocator, "\"{s}\" error in macro", .{@errorName(err)}) catch unreachable;
}
return expr;
};
Expand Down