zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit 56f2e5c5bc3267fa6c54d8fbc2295c5fa2a21571 (tree)
parent 2eaef84ebe968224b0cf25206abf12ea1c5e0f5a
Author: Andrew Kelley <andrew@ziglang.org>
Date:   Mon, 11 Jul 2022 14:51:22 -0700

Sema: fix double-free on compile errors

when instantiating a generic function and an error occurs in the
function prototype.

Diffstat:
Msrc/Sema.zig | 4+++-
1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/src/Sema.zig b/src/Sema.zig @@ -7096,6 +7096,7 @@ fn funcCommon( if (param.ty.tag() == .generic_poison) is_generic = true; } + var destroy_fn_on_error = false; const new_func: *Module.Fn = new_func: { if (!has_body) break :new_func undefined; if (sema.comptime_args_fn_inst == func_inst) { @@ -7103,9 +7104,10 @@ fn funcCommon( sema.preallocated_new_func = null; // take ownership break :new_func new_func; } + destroy_fn_on_error = true; break :new_func try sema.gpa.create(Module.Fn); }; - errdefer if (has_body) sema.gpa.destroy(new_func); + errdefer if (destroy_fn_on_error) sema.gpa.destroy(new_func); var maybe_inferred_error_set_node: ?*Module.Fn.InferredErrorSetListNode = null; errdefer if (maybe_inferred_error_set_node) |node| sema.gpa.destroy(node);