From cbc5ca770bd427ffff29c748f9da60b83b621a00 Mon Sep 17 00:00:00 2001 From: IAS <93499805+I-A-S@users.noreply.github.com> Date: Tue, 31 Oct 2023 01:06:38 +0530 Subject: fix: Macro segmentation faults (#6756) * Fix for seg faults when using macros * Update src/js_ast.zig to reflect review suggestions Co-authored-by: Jarred Sumner * add test for checking non-zero exitcodes under macros. regression, issue 3830 --------- Co-authored-by: Jarred Sumner --- src/js_ast.zig | 15 +++++++++++---- src/js_parser.zig | 2 +- 2 files changed, 12 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/js_ast.zig b/src/js_ast.zig index 3b17240c0..8b745742b 100644 --- a/src/js_ast.zig +++ b/src/js_ast.zig @@ -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| { arg.unprotect(); } @@ -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; } diff --git a/src/js_parser.zig b/src/js_parser.zig index fce928a6e..f569f515f 100644 --- a/src/js_parser.zig +++ b/src/js_parser.zig @@ -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; }; -- cgit v1.2.3