Skip to content

Fail the RDB load instead of crashing on truncated legacy document byte offsets - #11605

Merged
eyalrund merged 7 commits into
masterfrom
eyalrund-fix-legacy-byte-offsets-load
Sep 30, 2026
Merged

eyalrund merged 7 commits into
masterfrom
eyalrund-fix-legacy-byte-offsets-load

Conversation

@eyalrund

@eyalrund eyalrund commented Sep 29, 2026 •

Copy link
Copy Markdown
Collaborator

This branch also carries #11607 (the deleted-document payload fix, internal reference F-003), which was merged into it, and it has master merged in. See the verification table for the current head.

Describe the changes in the pull request

  1. Current: when Redis starts from an RDB file (or a replica runs a full sync) that holds a pre-2.0 index, any document whose stored byte offsets are shorter than their own length prefix crashes the server during the load. The loader reads past the end of the stored data and first allocates as much memory as the prefix claims.
  2. Change: the byte-offsets loader checks every read against the bytes that remain and rejects a short blob. The legacy doc-table loader then fails the load with a log line that names the document id. Separately, freeing a partly loaded legacy index no longer crashes. Before, any failed legacy load hit an assertion (debug builds) or a NULL dereference (release builds) in that cleanup.
  3. Outcome: Redis refuses the RDB and logs DocTable_LegacyRdbLoad: truncated byte offsets for doc id <n> instead of crashing. Well-formed legacy RDBs load and upgrade as before.

Which additional issues this PR fixes

  1. Internal reference F-002
  2. Internal reference F-003 (from Fix crash loading a legacy RDB with a deleted document flagged as having a payload #11607)

Main objects this PR modified

  1. LoadByteOffsets (src/byte_offsets.c): checks the field records, the data length and the data against the bytes remaining, and returns NULL when the blob is short.
  2. DocTable_LegacyRdbLoad (src/doc_table.c): on NULL, logs, frees the document it was building, and fails the load.
  3. IndexSpec_LegacyRdbLoad (src/spec.c) and Indexes_UpgradeLegacyIndexes (src/indexes_scan.c): the legacy spec's IndexError is initialized when the spec is created, so IndexSpec_Free is safe on every failed legacy load. It is cleared before the upgrade re-initializes the stats.
  4. From Fix crash loading a legacy RDB with a deleted document flagged as having a payload #11607: DocTable_LegacyRdbLoad clears Document_HasPayload on a deleted document whose payload it does not load, so DMD_Free no longer dereferences a NULL payload.
  5. test_legacy_module_types.py: builds a legacy (encver 16) index RDB in the test and starts the server from it.

Mark if applicable

  • This PR introduces API changes
  • This PR introduces serialization changes

No format change. RDBs that used to crash the server during load are now rejected. Nothing that loaded before is rejected: blobs with trailing bytes after the data are still accepted.

Release Notes

  • This PR requires release notes
  • This PR does not require release notes

Review notes

Why this path only. On master, LoadByteOffsets has one caller, the legacy (encver 2-16) doc-table loader. A legacy index can only be loaded during the first RDB load after the module loads. RESTORE of one is already refused (#10872), so the trigger is starting from, or fully syncing, an RDB file.

Why Buffer_Read is unchanged. Its bounds check is commented out and it has many readers, some of which read buffers whose cap is not their data size. The check lives in the one loader that parses untrusted lengths.

Verification. Each regression test was run in fresh checkouts of master (base, neither fix) and this branch's head, one run per test:

Check Base master cf6be8195 Head 61c47d657
Build (DEBUG=1) passed passed
testLegacySpecWithTruncatedByteOffsetsFailsToLoad failed as expected: SIGSEGV in LoadByteOffsets called from DocTable_LegacyRdbLoad passed: load rejected with the new log line, no crash report
testLegacySpecWithDeletedPayloadDocLoads (encver 16) failed as expected: SIGSEGV in DMD_Free during the RDB load passed
testLegacySpecWithDeletedPayloadDocLoadsExpireEncoding (encver 13) failed as expected: SIGSEGV in DMD_Free during the RDB load passed
Existing legacy and RDB-compatibility tests (7, including testRDBCompatibility over the bundled 1.2-2.0 RDBs and #11619's failed-sync RESTORE test) 7 passed 7 passed
Independent review (each run) passed, 0 actionable

Performance. A few length comparisons per document, only in the legacy loader, which runs once per legacy index at startup. Not measured.

Breaking changes. None identified.

Limitations.

  • The new tests skip under ASan, like the RDB-rejection tests in test_aux_save2.py: the server exits mid-load by design. So the new cleanup path (freeing the partial offsets, document and spec) has no sanitizer run.
  • The rejection test covers a data length larger than the blob. A short field-record region takes the same early return but is not exercised on its own.
  • The rejection test uses RLTest's startupGraceSecs and its "Redis server is dead" error text, as test_aux_save2.py does.
  • The test environment differs from CI: Redis 8.10-m03 built with MALLOC=libc, and headers not regenerated (REDISEARCH_GENERATE_HEADERS=0, safe because no Rust FFI signature changed).

🤖 Generated with Claude Code

eyalrund and others added 2 commits September 29, 2026 12:39
Build a legacy (encver 16) index spec RDB file in the test and start the
server from it. One test checks that byte offsets which exactly fill
their blob load and upgrade. The other gives the offsets a length prefix
larger than the blob, which must fail the load with a diagnostic rather
than crash the server.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
LoadByteOffsets trusted the counts and lengths in the serialized blob,
so a blob shorter than its own length prefix made it read past the end
of the buffer, after allocating as much as the prefix claimed. Check
each read against the bytes remaining and return NULL when the blob is
short. DocTable_LegacyRdbLoad, its only caller, now logs and fails the
load in that case.

Failing the load also needs the legacy spec's cleanup to be safe:
IndexSpec_LegacyRdbLoad never initialized the spec's IndexError, so
freeing the spec on any load error hit an assertion (debug) or a NULL
dereference (release). Initialize it when the spec is created, and
clear it before the legacy upgrade reinitializes the stats.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 29, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-09-29T16:53:32.488322Z 61c47d6 New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 1a3675c850

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/doc_table.c Resolved
Comment thread src/byte_offsets.c
Comment on lines +73 to +74
if (BufferReader_Remaining(&r) < numFields * fieldRecordSize + sizeof(uint32_t)) {
return NULL;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Exercise truncated offset headers

The added test reaches only the offsetsLen check with a structurally complete header; it does not exercise rejection of an empty blob or a field count followed by a truncated field record/data-length header. A regression in this branch could therefore restore an out-of-bounds Buffer_ReadU32 during RDB startup without failing the suite, so add malformed fixtures for both newly handled header layouts and assert the same clean load rejection.

AGENTS.md reference: AGENTS.md:L22-L22

Useful? React with 👍 / 👎.

eyalrund and others added 2 commits September 29, 2026 13:29
Build a legacy (encver 16) index spec RDB file in the test and start the
server from it. Its only document is deleted and flagged as having a
payload; the load must drop it and upgrade the index.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
DocTable_LegacyRdbLoad does not load the payload of a deleted document,
but it left Document_HasPayload set on it. Freeing the dropped document
then dereferenced its NULL payload in DMD_Free and crashed the server
during the RDB load. Clear the flag when the payload is not loaded.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@codecov

codecov Bot commented Sep 29, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 89.47368% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 83.53%. Comparing base (b1b6903) to head (61c47d6).
⚠️ Report is 2 commits behind head on master.

Files with missing lines Patch % Lines
src/byte_offsets.c 77.77% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master   #11605      +/-   ##
==========================================
- Coverage   84.24%   83.53%   -0.72%     
==========================================
  Files         640      665      +25     
  Lines       85437    86558    +1121     
  Branches    39264    40356    +1092     
==========================================
+ Hits        71976    72305     +329     
- Misses      13095    13879     +784     
- Partials      366      374       +8     
Flag Coverage Δ
flow 84.63% <89.47%> (+<0.01%) ⬆️
unit 58.95% <0.00%> (-0.43%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

eyalrund and others added 2 commits September 29, 2026 13:55
Encver 13 is the only encoding that saved the payload of a deleted doc,
so its load has to skip that string as well as clear the payload flag.
Parameterize the legacy spec fixture by encoding version and cover it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Resolve the conflict in test_legacy_module_types.py by keeping both the
legacy-loader tests from this branch and the failed-sync RESTORE test from
master (#11619).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
eyalrund added a commit that referenced this pull request Sep 29, 2026
…field-path-load

Pick up master (via #11605's branch) and resolve the conflict in
test_legacy_module_types.py by keeping both this branch's test and
master's failed-sync RESTORE test (#11619).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@sonarqubecloud

Copy link
Copy Markdown

@eyalrund
eyalrund added this pull request to the merge queue Sep 30, 2026
Merged via the queue into master with commit 00ee1cc Sep 30, 2026
62 checks passed
@eyalrund
eyalrund deleted the eyalrund-fix-legacy-byte-offsets-load branch September 30, 2026 11:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants