Skip to content

fix(opts): reject negative -c/-t/-n/--test-time; guard Hits/sec from /0 - #436

Merged
fcostaoliveira merged 3 commits into
redis:masterfrom
filipecosta90:fix/cli-negative-guards-and-hits-sec-div0
Jun 2, 2026
Merged

fcostaoliveira merged 3 commits into
redis:masterfrom
filipecosta90:fix/cli-negative-guards-and-hits-sec-div0

Conversation

@fcostaoliveira

@fcostaoliveira fcostaoliveira commented Jun 2, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Two small hygiene fixes flagged in the 2.4 release-readiness review.

1. Reject negative -c/-t/-n/--test-time (memtier_benchmark.cpp)

The merged phase-2 sweep (#428/#429) added optarg_is_negative + errno=ERANGE guards to --pipeline, --data-size, --run-count etc., but missed the four older count flags. -c -1 → strtoul wraps → (unsigned int)UINT_MAX = 4294967295 → ~4 billion clients spawned. Same crash class.

Adds the existing pattern to all four cases. Preserves the -n allkeys sentinel.

2. Guard Hits/sec from divide-by-zero (run_stats.cpp:1008-1009)

The legacy GET path's summarize() was the lone holdout missing a test_duration_usec > 0 guard. Sub-microsecond runs (or Ctrl-C at startup) produced +Inf / nan in the JSON. Adds the guard; matches the pattern every other 2.4 hits/sec computation already uses.

Test plan

  • New parser tests assert exit-2 + descriptive error for -c -1, -c 0, -t -1, -t 0, -n -2, -n 0, --test-time -1, --test-time 0
  • Existing test_json_output_integrity.py catches any regression in the legacy GET stats path
  • CI green

Note

Low Risk
Localized input validation and defensive math in stats reporting; no auth, networking protocol, or persistence changes.

Overview
Hardens CLI parsing for -c, -t, -n, and --test-time so negative or zero values fail at startup with a clear stderr message instead of strtoul wrapping to huge counts and OOM (same pattern as earlier fixes for --pipeline / --data-size / --run-count). A new tests/test_cli_validation_count_flags.py exercises those rejections without Redis.

In run_stats::summarize, all per-second rates that divide by test_duration_usec (hits/misses, ops, KB/s, MOVED/ASK, connection errors) now return 0.0 when duration is zero, avoiding +Inf/nan in JSON on sub-microsecond runs or immediate interrupt.

Reviewed by Cursor Bugbot for commit 8e8bd23. Bugbot is set up for automated code reviews on this repo. Configure here.

fcostaoliveira and others added 2 commits June 2, 2026 09:24
Same crash class as merged phases redis#428/redis#429 (--pipeline, --data-size,
--run-count): strtoul("-1") wraps to UINT_MAX and -c -1 then tries to
spawn ~4B clients. The four count flags missed the phase-2 sweep.

Also adds a > 0 guard to run_stats.cpp summarize() for the legacy GET
Hits/sec / Misses/sec computation (the only summarize site without one);
sub-microsecond runs were emitting +Inf / nan into the JSON.

Refs: 2.4 review findings redis#24 + redis#19

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
…ize error wording

Reviewers R3+R4 flagged that the prior commit only guarded m_hits_sec
and m_misses_sec in summarize(); six sibling rate computations
(m_ops_sec, m_bytes_sec, m_bytes_sec_rx, m_bytes_sec_tx, m_moved_sec,
m_ask_sec, m_connection_errors_sec) still divided by test_duration_usec
with no guard. All now use the (test_duration_usec > 0 ? ... : 0.0)
ternary, consistent with sites at lines 1300, 1311, 1317, 1344, 1354,
1360, 1627, 1629.

R5 also flagged that the four new error messages mixed "must be a
positive integer" and "must be greater than zero". Standardized to
match the phase-2 convention.

Refs: 2.4 review of PR redis#436 (R3/R4/R5).

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
@fcostaoliveira

Copy link
Copy Markdown
Collaborator Author

R3+R4+R5 findings addressed in 2b95d26: /0 guard extended to all 8 summarize() rate computations; error wording standardized to phase-2 convention.

Resolve conflicts after redis#432/redis#434/redis#435 landed on master.

Conflict in memtier_benchmark.cpp: adopt master's "must be a positive
integer" wording (standardized by redis#432) for the four negative-value
guard fprintf() calls added by this branch; both sides trigger on the
same code path and the test assertions only check the prefix, so either
wording is functionally correct.

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

Reviewed by Cursor Bugbot for commit 8e8bd23. Configure here.

Comment thread run_stats.cpp
result.m_wait_cmd.summarize(totals.m_wait_cmd, test_duration_usec);
result.m_ar_commands.summarize(totals.m_ar_commands, test_duration_usec);

// Guard against zero-duration runs (sub-microsecond or Ctrl-C at startup) which would produce +Inf/nan in the JSON.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Divide-by-zero guard incomplete in summarize calls

Medium Severity

The new divide-by-zero guard at line 1007 only protects the inline rate computations below it, but totals_cmd::summarize() (called at lines 1002–1005) still divides by test_duration_usec six times without any guard. In a zero-duration run, these calls produce +Inf/NaN for per-command m_ops_sec, m_bytes_sec, m_bytes_sec_rx, m_bytes_sec_tx, m_moved_sec, and m_ask_sec — the same class of bug the PR intends to fix.

Additional Locations (1)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 8e8bd23. Configure here.

@fcostaoliveira
fcostaoliveira merged commit 20688d3 into redis:master Jun 2, 2026
89 of 90 checks passed
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