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

zig build: Handle empty hashes in build.zig.zon
This commit is contained in:
Andrew Kelley 2025-06-06 22:48:09 -04:00 committed by GitHub
commit e96d86064e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 10 additions and 3 deletions

View file

@ -66,10 +66,11 @@ pub const Hash = struct {
pub fn toSlice(ph: *const Hash) []const u8 {
var end: usize = ph.bytes.len;
while (true) {
while (end > 0) {
end -= 1;
if (ph.bytes[end] != 0) return ph.bytes[0 .. end + 1];
}
return ph.bytes[0..0];
}
pub fn eql(a: *const Hash, b: *const Hash) bool {
@ -195,6 +196,11 @@ test Hash {
try std.testing.expectEqualStrings("nasm-2.16.1-3-vrr-ygAAoADH9XG3tOdvPNuHen_d-XeHndOG-nNXmved", result.toSlice());
}
test "empty hash" {
const hash = Hash.fromSlice("");
try std.testing.expectEqualStrings("", hash.toSlice());
}
test {
_ = Fetch;
}

View file

@ -568,14 +568,14 @@ fn runResource(
const actual_hex = Package.multiHashHexDigest(f.computed_hash.digest);
if (!std.mem.eql(u8, declared_hash.toSlice(), &actual_hex)) {
return f.fail(hash_tok, try eb.printString(
"hash mismatch: manifest declares {s} but the fetched package has {s}",
"hash mismatch: manifest declares '{s}' but the fetched package has '{s}'",
.{ declared_hash.toSlice(), actual_hex },
));
}
} else {
if (!computed_package_hash.eql(&declared_hash)) {
return f.fail(hash_tok, try eb.printString(
"hash mismatch: manifest declares {s} but the fetched package has {s}",
"hash mismatch: manifest declares '{s}' but the fetched package has '{s}'",
.{ declared_hash.toSlice(), computed_package_hash.toSlice() },
));
}
@ -726,6 +726,7 @@ fn queueJobsForDeps(f: *Fetch) RunError!void {
.hash = h: {
const h = dep.hash orelse break :h null;
const pkg_hash: Package.Hash = .fromSlice(h);
if (h.len == 0) break :h pkg_hash;
const gop = f.job_queue.table.getOrPutAssumeCapacity(pkg_hash);
if (gop.found_existing) {
if (!dep.lazy) {