Skip to content

fix(monitor-input): preserve byte count past NUL; handle CR-only line endings - #434

Merged
fcostaoliveira merged 1 commit into
masterfrom
fix/monitor-input-nul-and-cr-only
Jun 2, 2026
Merged

fcostaoliveira merged 1 commit into
masterfrom
fix/monitor-input-nul-and-cr-only

Conversation

@fcostaoliveira

@fcostaoliveira fcostaoliveira commented Jun 2, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Two silent data-loss bugs in monitor_command_list::load_from_file (config_types.cpp), found during 2.4 release review (finding #30).

Bug F1 — NUL truncation (strchr → memchr)
strchr(line, '"') stops at the first \0 byte. A MONITOR capture whose metadata prefix contains an embedded NUL causes the line to be silently skipped — first_quote becomes NULL even though getline returned the full byte count with the "CMD" portion intact. Fix: replace strchr with memchr(seg_start, '"', seg_len) so the search spans exactly the bytes returned by getline.

Additionally, std::string command_str(first_quote) used the C-string constructor which truncates at the first \0 in the command part. Fix: use std::string command_str(first_quote, cmd_len) with the explicit byte count.

Bug F2 — CR-only line endings
getline uses \n as its sole delimiter. A file with pure \r separators (classic Mac / some Windows exports) is read as one giant line; the metadata of every line after the first is appended as extra tokens to the first command — most commands are silently dropped. Fix: a pre-pass normalises bare \r → \n within the buffer before segment processing.

Neither path crashed; both lost data without surfacing.

Changes

  • config_types.cpp (load_from_file): CR normalisation pass + inner segment loop + memchr + explicit std::string(ptr, len) constructor.
  • tests/test_monitor_input.py: two new regression tests:
    • test_monitor_input_nul_byte_in_prefix_not_truncated — binary monitor file with \x00 in line 2's metadata prefix; asserts all 3 commands are loaded.
    • test_monitor_input_cr_only_line_endings — binary monitor file with \r-only separators; asserts all 3 commands are loaded and all 3 keys land in Redis.

Test plan

  • make -j$(nproc) — clean build, no new warnings.
  • make format-check-staged — clang-format clean (verified locally).
  • Existing tests/test_monitor_input.py tests pass unmodified (no behaviour change on normal \n-delimited files).
  • New test_monitor_input_nul_byte_in_prefix_not_truncated — fails on old code (count = 2), passes on fixed code (count = 3).
  • New test_monitor_input_cr_only_line_endings — fails on old code (count = 1 or parse error), passes on fixed code (count = 3, all 3 keys in Redis).

Refs: 2.4 review finding #30

🤖 Generated with Claude Code


Note

Low Risk
Scoped to monitor capture parsing in config_types.cpp with targeted regression tests; normal newline-delimited files should behave the same.

Overview
Fixes silent data loss when loading --monitor-input capture files in monitor_command_list::load_from_file (config_types.cpp).

NUL in metadata: Locating the command with strchr and building std::string from a C string could stop at embedded \0 bytes in the prefix, so valid lines were skipped. The loader now splits each getline buffer into segments, finds " with memchr over a bounded length, and copies commands with std::string(ptr, len) so NULs in the payload are preserved.

CR-only line endings: Bare \r separators (no \n) used to be one physical line; the code normalizes internal \r → \n and processes each segment so every MONITOR record becomes its own command.

tests/test_monitor_input.py adds regression tests for NUL-in-prefix (expects 3 loaded commands) and CR-only files (3 commands + keys in Redis).

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

… endings

load_from_file built std::string from the line via the C-string ctor,
silently truncating at the first embedded \0 byte in binary blobs.
getline used \n only, so classic-Mac \r-only files collapsed into
one giant line and dropped commands silently.

Neither path crashed; both lost data without surfacing.

- std::string ctor takes (first_quote, line+line_len-first_quote)
- Replace strchr with memchr so NUL bytes before the first '"' are
  skipped correctly when locating the command start
- Normalise \r-only line endings before tokenising

Refs: 2.4 review finding #30

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

jit-ci Bot commented Jun 2, 2026

Copy link
Copy Markdown

🛡️ Jit Security Scan Results

CRITICAL HIGH MEDIUM

✅ No security findings were detected in this PR


Security scan by Jit

@fcostaoliveira
fcostaoliveira merged commit 6288b5f into master Jun 2, 2026
82 checks passed
fcostaoliveira added a commit to filipecosta90/memtier_benchmark that referenced this pull request Jun 2, 2026
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.
fcostaoliveira added a commit that referenced this pull request Jun 2, 2026
…ownstream NUL issue) (#441)

* docs(monitor-input): code comment + README note on NUL/CR-only fixes

Follow-up to PR #434: add an inline comment explaining the memchr
choice, README paragraphs documenting the 2.4 capability changes
(binary payloads preserved, CR-only line endings supported), and a
code comment near the arbitrary_command ctor flagging that downstream
c_str() still truncates at embedded NULs (filed as separate
follow-up issue for the load-bearing fix).

Refs: 2.4 review #38 (Review 14 R3 + R6).

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

* docs(monitor-input): qualify "end-to-end" claim; trim ctor comment

Reviewers caught that the README's "preserved end-to-end" wording
overstated the fix -- the code comments in the same PR document that
downstream c_str() re-truncates at the first NUL. Re-worded the
README to match the actual scope (loader preserves; dispatch
re-truncates, tracked as #439). Also trimmed the verbose ctor comment.

Refs: PR #441 reviewers R1+R4+R6.

---------

Co-authored-by: Claude Sonnet 4.6 <[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.

1 participant