From 8a3cd82b854164758eef17440d86105472e9e7a9 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Sat, 23 May 2020 23:15:58 -0400 Subject: [PATCH] translate-c: fix a use-after-free bug --- src-self-hosted/translate_c.zig | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src-self-hosted/translate_c.zig b/src-self-hosted/translate_c.zig index 22b338642..94122f465 100644 --- a/src-self-hosted/translate_c.zig +++ b/src-self-hosted/translate_c.zig @@ -135,12 +135,13 @@ const Scope = struct { /// Given the desired name, return a name that does not shadow anything from outer scopes. /// Inserts the returned name into the scope. fn makeMangledName(scope: *Block, c: *Context, name: []const u8) ![]const u8 { - var proposed_name = name; + const name_copy = try c.arena.dupe(u8, name); + var proposed_name = name_copy; while (scope.contains(proposed_name)) { scope.mangle_count += 1; proposed_name = try std.fmt.allocPrint(c.arena, "{}_{}", .{ name, scope.mangle_count }); } - try scope.variables.append(.{ .name = name, .alias = proposed_name }); + try scope.variables.append(.{ .name = name_copy, .alias = proposed_name }); return proposed_name; }