Repository navigation
Fail fast on malformed --data-import files (hang, crashes) - #767
Conversation
A --data-import item line that read_item() could not parse made memtier
spin forever at 100% CPU while loading keys, printing the same "error
parsing item values." line hundreds of MB at a time. read_keys() looped on
!is_eof(), but read_item() returns NULL without consuming the bad input,
so the stream never reached EOF. The first SIGTERM only requests a
graceful stop, which the run loop checks and this loop does not, so it
looked as if SIGTERM was ignored.
Other bad lines crashed instead: a truncated key dereferenced a NULL
read_string() result (SIGSEGV), nbytes < 2 underflowed `nbytes - 2` into
a heap overflow, and errors that were dropped ("0 keys read") or only hit
at run time with --generate-keys aborted in read_next_item() after
connecting.
read_item() now records a malformed item in an error flag (has_error())
and returns NULL. A partial fscanf() match at EOF counts as an error, not
a clean end. It also rejects nbytes < 2, a NULL key/data from
read_string(), and a key whose length does not match nkey. read_keys()
stops at the first NULL and fails on an error or an empty file. With
--generate-keys, import_object_generator::open_file() runs the same
validation scan at startup. Every bad file now prints its error once,
with file:line, and exits 1 before connecting. Valid files load the same
data as before.
Fixes #766
Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
🤖 Automated first-pass review — a human maintainer's review is still required before merge. Thanks for this. I went through the reader changes and the tests, and I only have a few comments. The core fix looks right to me. Stopping at the first NULL instead of looping on
None of this blocks the fix. I'd like an answer on the short-key question before this merges, though. (would file this as a comment, not a formal block) |
Recover the interrupted owner pass on top of the published PR head. Share startup validation, keep TLS test setup consistent, and reject overflowing length tokens before allocation. Exercise wrap-around and good/bad/good inputs with sanitizer exits distinct from parse failures.
|
Addressed all three points in e7229f9:
The previously red valid-file test now uses the shared TLS arguments and env.getConnection(). Six independent mutants deleting the reader's error assignments are all caught, including by the new good/bad/good cases. Full CI and the remaining independent review lenses are still pending; this is not a merge claim. |
|
On the latest edit:
|
|
Thanks for the updated review. I considered replacing the declared key length with The repeated truncated-tail warning is acknowledged. Startup validation and each client reader are independent, and runtime reopening can encounter a changed file. Suppressing all later diagnostics based on the startup scan would hide that change; per-reader warning deduplication would be a separate behavior change. This fix preserves the existing warning behavior and does not add suppression state. The 512 MiB limits remain for the signed per-field and combined protocol accounting reasons in the previous reply. The |
Problem
Malformed
--data-importfiles can hang at 100% CPU while flooding stderr, crash on truncated keys or overflowing lengths, or reach a worker and abort after connecting.--generate-keyspreviously skipped startup validation entirely.Root cause
imported_keylist::read_keys()retried until EOF, but a failedfscanf()does not consume the invalid bytes. A NULL item also conflated an input error with EOF. Length arithmetic could underflow or wrap, and allocations and truncated strings were not checked.Fix
--generate-keyspaths inmain()useimported_keylist::read_keys(); the generated-key path uses a temporary keylist andread_keys(false)to validate without storing keys. Generated-key errors sayinvalid --data-import file.intkey/value lengths and add both payloads and framing into anintbyte count.UINT_MAX - 2would permit negative narrowed lengths; independentINT_MAXlimits could still overflow combined accounting. A looser limit needs coupled length/framing validation and wider protocol accounting. This intentionally rejects some formerly accepted large files even when a server is configured for larger payloads; Redis's configurable default is precedent for the chosen cap, not the safety argument.premature end of fileand acts as EOF. Earlier complete items still load, including when the worker wraps around. A file containing only a cut-off item fails. Missing-final-newline and missing-closing-quote warnings retain their existing behavior.--generate-keysadds one sequential startup scan without storing imported keys. The previous session measured approximately +3.9 s on a 488 MiB / two-million-item file; README documents this cost.Tests
tests/test_data_import_parse_errors.pyruns 114 invalid-input cases across ordinary import,--verify-only, and--generate-keys, including each malformed item both alone and between valid items. Subprocesses have a five-second timeout, bounded stderr and a distinct sanitizer exit code (86). Valid-file tests use the shared TLS helpers, request more items than the file contains, parse JSON operation counts, check Redis values and require no warnings for valid CRLF files. Truncated-final-item tests perform real imports and wrap around in both stored-key and generated-key modes.Local owner validation on the final code:
make check: 13/13 executables pass, including new reader reopen/error/EOF tests; sanitizer build passes too.make formatandmake format-checkpass.Measured before/after from the initial reproduction (retained from the previous session):
master3fcbfbc)Fixes #766
Affected versions
The faulty read loop and unchecked import parser originate in 833a8df (
First memtier_benchmark version., 2013).git tag --contains 833a8dfincludes every release from 1.0.0 through 2.5.2, plus affected master. The previous session reproduced the hang on master 3fcbfbc and 2.5.2; older releases are identified by source history.🤖 Generated with Claude Code
Note
Medium Risk
Changes startup behavior for all
--data-importruns and tightens accepted file shapes (including rejecting some formerly tolerated inputs), but limits risk by validating before connections and preserving truncated-tail compatibility.Overview
Hardens
--data-importso malformed or empty dump files exit 1 at startup (before connecting), including--generate-keysand--verify-only.The import reader now stops on the first bad item (fixing infinite CPU loops), tracks
has_error(), validates bounded key/value lengths (max 512 MiB, no empty fields, length/column mismatches are errors), and handles OOM on column reads. A truncated final item after at least one complete row still warns and is treated as EOF for size-capped dumps; short keys no longer load garbage bytes.imported_keylist::read_keys(bool keep_keys)supports a validation-only scan for generated-key mode;memtier_benchmarkwires that path inmain(). Addsfile_io_testplustests/test_data_import_parse_errors.py(timeouts, sanitizer exit code) and README notes on validation cost for large files.Reviewed by Cursor Bugbot for commit fa345e8. Bugbot is set up for automated code reviews on this repo. Configure here.