fuzz: prevent pcs slice panic and null-base memcpy; ensure capacity before first append #24869
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes two crash paths in the new fuzz runner:
std/Build.Fuzz.addEntryPoint: guard pcs.len == 0 (avoid pcs[1..] slice panic) and make logs bounds-safe.
lib/fuzzer.zig:
appendSliceAssumeCapacity: early-return on zero-length and copy into [old_len .. old_len + items.len] to avoid forming a slice off a null base.
start(): ensure capacity before the first append when the corpus is empty (Web UI/coverage path triggers this).
minimal reproduction using zig master: zig init
// src/main.zig
const std = @import("std");
test "fuzz minimal" {
const Ctx = struct { fn testOne(_: @this(), input: []const u8) !void { _ = input; } };
try std.testing.fuzz(Ctx{}, Ctx.testOne, .{});
}
Run: zig build test --fuzz --webui=[::1]:45891.
Before: either a pcs slice panic or a segfault at the fuzzer memcpy (null-base slice).
After: fuzzing runs indefinitely (Ctrl-C to stop).
Note: the ensureTotalCapacity(len) is defensive; a higher-level invariant likely intended the assume-capacity precondition to hold. A TODO comment is left to flag follow-up.