Skip to content

fuzz: prevent pcs slice panic and null-base memcpy; ensure capacity before first append #24869

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

jwstolk
Copy link

@jwstolk jwstolk commented Aug 15, 2025

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.

…efore first append

- std/Build.Fuzz.addEntryPoint: guard pcs.len==0 and make logs bounds-safe.
- lib/fuzzer.zig:
  - appendSliceAssumeCapacity: early-return on zero-length; copy to [old_len .. old_len+len].
  - start(): ensureTotalCapacity before first append when corpus is empty (TODO: revisit invariant).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant