Skip to content

Commit 7611d90

Browse files
committed
InternPool: remove slice from byte aggregate keys
This deletes a ton of lookups and avoids many UAF bugs. Closes ziglang#19485
1 parent 4cd9256 commit 7611d90

24 files changed

+1038
-952
lines changed

lib/std/zig/Zir.zig

+2-6
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,8 @@ pub const NullTerminatedString = enum(u32) {
106106

107107
/// Given an index into `string_bytes` returns the null-terminated string found there.
108108
pub fn nullTerminatedString(code: Zir, index: NullTerminatedString) [:0]const u8 {
109-
const start = @intFromEnum(index);
110-
var end: u32 = start;
111-
while (code.string_bytes[end] != 0) {
112-
end += 1;
113-
}
114-
return code.string_bytes[start..end :0];
109+
const slice = code.string_bytes[@intFromEnum(index)..];
110+
return slice[0..std.mem.indexOfScalar(u8, slice, 0).? :0];
115111
}
116112

117113
pub fn refSlice(code: Zir, start: usize, len: usize) []Inst.Ref {

src/Compilation.zig

+3-4
Original file line numberDiff line numberDiff line change
@@ -3159,7 +3159,7 @@ pub fn addModuleErrorMsg(mod: *Module, eb: *ErrorBundle.Wip, module_err_msg: Mod
31593159
const rt_file_path = try module_reference.src_loc.file_scope.fullPath(gpa);
31603160
defer gpa.free(rt_file_path);
31613161
ref_traces.appendAssumeCapacity(.{
3162-
.decl_name = try eb.addString(ip.stringToSlice(module_reference.decl)),
3162+
.decl_name = try eb.addString(module_reference.decl.toSlice(ip)),
31633163
.src_loc = try eb.addSourceLocation(.{
31643164
.src_path = try eb.addString(rt_file_path),
31653165
.span_start = span.start,
@@ -4074,8 +4074,7 @@ fn workerCheckEmbedFile(
40744074
fn detectEmbedFileUpdate(comp: *Compilation, embed_file: *Module.EmbedFile) !void {
40754075
const mod = comp.module.?;
40764076
const ip = &mod.intern_pool;
4077-
const sub_file_path = ip.stringToSlice(embed_file.sub_file_path);
4078-
var file = try embed_file.owner.root.openFile(sub_file_path, .{});
4077+
var file = try embed_file.owner.root.openFile(embed_file.sub_file_path.toSlice(ip), .{});
40794078
defer file.close();
40804079

40814080
const stat = try file.stat();
@@ -4444,7 +4443,7 @@ fn reportRetryableEmbedFileError(
44444443
const ip = &mod.intern_pool;
44454444
const err_msg = try Module.ErrorMsg.create(gpa, src_loc, "unable to load '{}{s}': {s}", .{
44464445
embed_file.owner.root,
4447-
ip.stringToSlice(embed_file.sub_file_path),
4446+
embed_file.sub_file_path.toSlice(ip),
44484447
@errorName(err),
44494448
});
44504449

0 commit comments

Comments
 (0)