Skip to content

Commit 57741b4

Browse files
JoeMattclaude
andcommitted
fix(bench): validate num_frames > 0 and warmup_frames >= 0
Copilot review on PR #129 caught that the per-frame stats path unconditionally allocates / sorts / divides by num_frames, which would OOB-access sorted[num_frames - 1] and divide by zero if a caller passed 0 or a negative number. Both come from atoi() which returns 0 on garbage input, so this was reachable. Validate both at parse time and exit with a clear error before allocating anything. Co-Authored-By: Claude Opus 4.7 <[email protected]>
1 parent b8cc8b4 commit 57741b4

1 file changed

Lines changed: 11 additions & 0 deletions

File tree

test/tools/test_benchmark.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,17 @@ int main(int argc, char **argv)
256256
num_frames = atoi(argv[i]);
257257
}
258258

259+
if (num_frames <= 0)
260+
{
261+
fprintf(stderr, "ERROR: num_frames must be a positive integer (got %d)\n", num_frames);
262+
return 1;
263+
}
264+
if (warmup_frames < 0)
265+
{
266+
fprintf(stderr, "ERROR: --warmup must be >= 0 (got %d)\n", warmup_frames);
267+
return 1;
268+
}
269+
259270
#ifdef __APPLE__
260271
/* Initialize timebase for mach_absolute_time conversion */
261272
mach_timebase_info(&timebase_info);

0 commit comments

Comments
 (0)