authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-06-06 22:48:09-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2025-06-06 22:48:09-04:00
loge96d86064eb81977f254fa8f36481b7d150cb3b6
treedb68c1089fcd0d78b790924b3f58136fcbf77645
parent38266c50351fc64fd4f513d684d6f2c1aaad7f9a
parentbc3ce4b9715fc487eddfa1819a252a8fc82a369e
signaturebadge-check Signed by PGP key B5690EEEBB952194

Merge pull request #24090 from fardragon/handle-empty-hash

zig build: Handle empty hashes in build.zig.zon

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

src/Package.zig+7-1
......@@ -66,10 +66,11 @@ pub const Hash = struct {
6666
6767 pub fn toSlice(ph: *const Hash) []const u8 {
6868 var end: usize = ph.bytes.len;
69 while (true) {
69 while (end > 0) {
7070 end -= 1;
7171 if (ph.bytes[end] != 0) return ph.bytes[0 .. end + 1];
7272 }
73 return ph.bytes[0..0];
7374 }
7475
7576 pub fn eql(a: *const Hash, b: *const Hash) bool {
......@@ -195,6 +196,11 @@ test Hash {
195196 try std.testing.expectEqualStrings("nasm-2.16.1-3-vrr-ygAAoADH9XG3tOdvPNuHen_d-XeHndOG-nNXmved", result.toSlice());
196197}
197198
199test "empty hash" {
200 const hash = Hash.fromSlice("");
201 try std.testing.expectEqualStrings("", hash.toSlice());
202}
203
198204test {
199205 _ = Fetch;
200206}
src/Package/Fetch.zig+3-2
......@@ -568,14 +568,14 @@ fn runResource(
568568 const actual_hex = Package.multiHashHexDigest(f.computed_hash.digest);
569569 if (!std.mem.eql(u8, declared_hash.toSlice(), &actual_hex)) {
570570 return f.fail(hash_tok, try eb.printString(
571 "hash mismatch: manifest declares {s} but the fetched package has {s}",
571 "hash mismatch: manifest declares '{s}' but the fetched package has '{s}'",
572572 .{ declared_hash.toSlice(), actual_hex },
573573 ));
574574 }
575575 } else {
576576 if (!computed_package_hash.eql(&declared_hash)) {
577577 return f.fail(hash_tok, try eb.printString(
578 "hash mismatch: manifest declares {s} but the fetched package has {s}",
578 "hash mismatch: manifest declares '{s}' but the fetched package has '{s}'",
579579 .{ declared_hash.toSlice(), computed_package_hash.toSlice() },
580580 ));
581581 }
......@@ -726,6 +726,7 @@ fn queueJobsForDeps(f: *Fetch) RunError!void {
726726 .hash = h: {
727727 const h = dep.hash orelse break :h null;
728728 const pkg_hash: Package.Hash = .fromSlice(h);
729 if (h.len == 0) break :h pkg_hash;
729730 const gop = f.job_queue.table.getOrPutAssumeCapacity(pkg_hash);
730731 if (gop.found_existing) {
731732 if (!dep.lazy) {