authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-05-23 23:15:58-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-05-23 23:15:58-04:00
log8a3cd82b854164758eef17440d86105472e9e7a9
tree06e8d67b26cd18a99b5bd4fd4b3d2d6daf5630ab
parent2952604d5d18078ed768e12010f4463176620e9a

translate-c: fix a use-after-free bug


1 files changed, 3 insertions(+), 2 deletions(-)

src-self-hosted/translate_c.zig+3-2
...@@ -135,12 +135,13 @@ const Scope = struct {...@@ -135,12 +135,13 @@ const Scope = struct {
135 /// Given the desired name, return a name that does not shadow anything from outer scopes.135 /// Given the desired name, return a name that does not shadow anything from outer scopes.
136 /// Inserts the returned name into the scope.136 /// Inserts the returned name into the scope.
137 fn makeMangledName(scope: *Block, c: *Context, name: []const u8) ![]const u8 {137 fn makeMangledName(scope: *Block, c: *Context, name: []const u8) ![]const u8 {
138 var proposed_name = name;138 const name_copy = try c.arena.dupe(u8, name);
139 var proposed_name = name_copy;
139 while (scope.contains(proposed_name)) {140 while (scope.contains(proposed_name)) {
140 scope.mangle_count += 1;141 scope.mangle_count += 1;
141 proposed_name = try std.fmt.allocPrint(c.arena, "{}_{}", .{ name, scope.mangle_count });142 proposed_name = try std.fmt.allocPrint(c.arena, "{}_{}", .{ name, scope.mangle_count });
142 }143 }
143 try scope.variables.append(.{ .name = name, .alias = proposed_name });144 try scope.variables.append(.{ .name = name_copy, .alias = proposed_name });
144 return proposed_name;145 return proposed_name;
145 }146 }
146147