From e60d66711185fa2e3164f5af92b9786e04c4fa19 Mon Sep 17 00:00:00 2001 From: Jacob Young Date: Wed, 21 Feb 2024 16:21:14 +0100 Subject: Module: fix `@embedFile` of files containing zero bytes If an adapted string key with embedded nulls was put in a hash map with `std.hash_map.StringIndexAdapter`, then an incorrect hash would be entered for that entry such that it is possible that when looking for the exact key that matches the prefix of the original key up to the first null would sometimes match this entry due to hash collisions and sometimes not if performed later after a grow + rehash, causing the same key to exist with two different indices breaking every string equality comparison ever, for example claiming that a container type doesn't contain a field because the field name string in the struct and the string representing the identifier to lookup might be equal strings but have different string indices. This could maybe be fixed by changing `std.hash_map.StringIndexAdapter.hash` to only hash up to the first null, therefore ensuring that the entry's hash is correct and that all future lookups will be consistent, but I don't trust anything so instead I assert that there are no embedded nulls. --- src/InternPool.zig | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'src/InternPool.zig') diff --git a/src/InternPool.zig b/src/InternPool.zig index 379a4f76c6..19be12c129 100644 --- a/src/InternPool.zig +++ b/src/InternPool.zig @@ -7985,7 +7985,8 @@ pub fn getTrailingAggregate( ) Allocator.Error!Index { try ip.items.ensureUnusedCapacity(gpa, 1); try ip.extra.ensureUnusedCapacity(gpa, @typeInfo(Bytes).Struct.fields.len); - const str: String = @enumFromInt(@intFromEnum(try getOrPutTrailingString(ip, gpa, len))); + + const str: String = @enumFromInt(ip.string_bytes.items.len - len); const adapter: KeyAdapter = .{ .intern_pool = ip }; const gop = try ip.map.getOrPutAdapted(gpa, Key{ .aggregate = .{ .ty = ty, -- cgit v1.2.3