Skip to content

Out of Bounds read in valkey-benchmark - #4142

Merged
enjoy-binbin merged 1 commit into
valkey-io:unstablefrom
lightsigma96:valkey_benchmark_out_of_bound_access
Jul 14, 2026
Merged

enjoy-binbin merged 1 commit into
valkey-io:unstablefrom
lightsigma96:valkey_benchmark_out_of_bound_access

Conversation

@lightsigma96

Copy link
Copy Markdown
Contributor

Description

Running:

./valkey-benchmark -t get 10000

causes valkey-benchmark to segfault.

After option parsing, the remaining argument (10000) is interpreted as a repeat count for an arbitrary command sequence. Since no command follows the repeat count, start becomes equal to argc.

When start becomes equal to argc, the parser evaluates sds_args[i][0] before checking that i is still within bounds, resulting in an out-of-bounds read and undefined behaviour.

This change guards the repeat-count parsing by checking i < argc before accessing sds_args[i].

Expected Behaviour

The benchmark should reject the malformed input instead of crashing.

How to Reproduce

  1. Build Valkey:
make

or

make SANITIZER=address
  1. Run:
./valkey-benchmark -t get 10000

Testing

  • Reproduced the crash.
  • Verified the out-of-bounds read with AddressSanitizer & gdb.
  • Ran make test.

Further

  • This avoids reading out of bounds but still does not handle what happens after that.

The arbitrary command sequence parser uses ';' to separate commands and
iterates until i == argc.

If a repeat count is provided without a following command, start becomes
equal to argc. During the sentinel iteration, the parser evaluates
sds_args[i][0] before checking i == argc, resulting in an out-of-bounds
read and undefined behaviour.

Guard the repeat-count parsing by checking i < argc before accessing
sds_args[i].

Signed-off-by: lightsigma96 <[email protected]>
@coderabbitai

coderabbitai Bot commented Jul 10, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 1913c702-5018-40a2-9197-0e92b84a7d22

📥 Commits

Reviewing files that changed from the base of the PR and between d382452 and 6699825.

📒 Files selected for processing (1)
  • src/valkey-benchmark.c

📝 Walkthrough

Walkthrough

The benchmark command parser adds an i < argc guard before inspecting sds_args[i], preventing an out-of-bounds access on the loop’s sentinel iteration.

Changes

Benchmark command parsing

Layer / File(s) Summary
Numeric prefix bounds check
src/valkey-benchmark.c
The numeric command-prefix condition verifies that the argument index is below argc before accessing sds_args[i].

Estimated code review effort: 1 (Trivial) | ~2 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main fix: an out-of-bounds read in valkey-benchmark.
Description check ✅ Passed The description is directly related to the bug fix and reproduces the same crashing scenario.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@valkey-review-bot valkey-review-bot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The bounds check fixes the out-of-bounds read, but the malformed trailing-repeat-count case still falls through into an empty benchmark sequence instead of being rejected.

Comment thread src/valkey-benchmark.c Resolved

@enjoy-binbin enjoy-binbin left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM, thanks.

@codecov

codecov Bot commented Jul 14, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 76.69%. Comparing base (d382452) to head (6699825).
⚠️ Report is 5 commits behind head on unstable.

Additional details and impacted files
@@             Coverage Diff              @@
##           unstable    #4142      +/-   ##
============================================
+ Coverage     76.58%   76.69%   +0.11%     
============================================
  Files           162      162              
  Lines         81253    81253              
============================================
+ Hits          62226    62321      +95     
+ Misses        19027    18932      -95     
Files with missing lines Coverage Δ
src/valkey-benchmark.c 72.29% <100.00%> (ø)

... and 19 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@enjoy-binbin
enjoy-binbin merged commit 7bb7b7f into valkey-io:unstable Jul 14, 2026
65 checks passed
alon-arenberg pushed a commit to alon-arenberg/valkey that referenced this pull request Jul 15, 2026
Running:
```sh
./valkey-benchmark -t get 10000
```

causes `valkey-benchmark` to segfault.

After option parsing, the remaining argument (`10000`) is interpreted as
a repeat count for an arbitrary command sequence. Since no command
follows the repeat count, `start` becomes equal to `argc`.

When `start` becomes equal to `argc`, the parser evaluates
`sds_args[i][0]` before checking that `i` is still within bounds,
resulting in an out-of-bounds read and undefined behaviour.

This change guards the repeat-count parsing by checking `i < argc`
before accessing `sds_args[i]`.

Note: This avoids reading out of bounds but still does not handle what
happens after that.

Signed-off-by: lightsigma96 <[email protected]>
Signed-off-by: Alon Arenberg <[email protected]>
alon-arenberg pushed a commit to alon-arenberg/valkey that referenced this pull request Jul 30, 2026
Running:
```sh
./valkey-benchmark -t get 10000
```

causes `valkey-benchmark` to segfault.

After option parsing, the remaining argument (`10000`) is interpreted as
a repeat count for an arbitrary command sequence. Since no command
follows the repeat count, `start` becomes equal to `argc`.

When `start` becomes equal to `argc`, the parser evaluates
`sds_args[i][0]` before checking that `i` is still within bounds,
resulting in an out-of-bounds read and undefined behaviour.

This change guards the repeat-count parsing by checking `i < argc`
before accessing `sds_args[i]`.

Note: This avoids reading out of bounds but still does not handle what
happens after that.

Signed-off-by: lightsigma96 <[email protected]>
Signed-off-by: Alon Arenberg <[email protected]>
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.

2 participants