diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2021-04-29 20:34:33 -0700 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2021-04-29 20:34:33 -0700 |
| commit | 8ab4a003c8f22a3fd8f84bdb2fc27d8c20d3bf2f (patch) | |
| tree | 28db2ba140f4017ef0f314d47a52c8dd517301a9 /src/Module.zig | |
| parent | 474ade88b58b6fd7239049ec445129eabafa3cbd (diff) | |
| download | zig-8ab4a003c8f22a3fd8f84bdb2fc27d8c20d3bf2f.tar.gz zig-8ab4a003c8f22a3fd8f84bdb2fc27d8c20d3bf2f.zip | |
stage2: properly free Decl name
Two problems solved:
* The Decl name may be allocated with gpa or it may be a reference to
the ZIR string table.
* The main update() function was freeing the ZIR when we still had
Decl objects referencing it.
Diffstat (limited to 'src/Module.zig')
| -rw-r--r-- | src/Module.zig | 13 |
1 files changed, 12 insertions, 1 deletions
diff --git a/src/Module.zig b/src/Module.zig index a212fb44e4..76a740bdc0 100644 --- a/src/Module.zig +++ b/src/Module.zig @@ -263,9 +263,20 @@ pub const Decl = struct { false, ); + pub fn clearName(decl: *Decl, gpa: *Allocator) void { + // name could be allocated in the ZIR or it could be owned by gpa. + const file = decl.namespace.file_scope; + const string_table_start = @ptrToInt(file.zir.string_bytes.ptr); + const string_table_end = string_table_start + file.zir.string_bytes.len; + if (@ptrToInt(decl.name) < string_table_start or @ptrToInt(decl.name) >= string_table_end) { + gpa.free(mem.spanZ(decl.name)); + } + decl.name = undefined; + } + pub fn destroy(decl: *Decl, module: *Module) void { const gpa = module.gpa; - gpa.free(mem.spanZ(decl.name)); + decl.clearName(gpa); if (decl.has_tv) { if (decl.val.castTag(.function)) |payload| { const func = payload.data; |
