From fee6d88be47e5b4d90fa3bce5d860315850c82de Mon Sep 17 00:00:00 2001 From: Madelyn Olson Date: Wed, 14 Oct 2026 09:29:19 -0700 Subject: [PATCH] Fail the RDB transcode on bytes after the source frame When a disk-based full sync is transcoded, bytes that follow the end of the source VCS frame are left pending in the push reader. rdbTranscoderRun() then returns success, and the next write feeds the reader again while it still holds them, which trips serverAssert(len == 0 || !resuming) in streamPushReaderFeed() and crashes the replica. If the trailing bytes instead arrive in the final write they are silently dropped. A well-formed payload never has bytes after the frame: the receiver strips the $EOF mark before feeding the transcoder and the primary writes a single frame. Treat trailing bytes as a malformed payload and fail the transcode, so the replica aborts the sync and retries instead of crashing. Signed-off-by: Madelyn Olson --- src/rdb_transcoder.c | 9 +++++-- src/unit/test_compression.cpp | 51 +++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 2 deletions(-) diff --git a/src/rdb_transcoder.c b/src/rdb_transcoder.c index da7cf0f31..fd04066ce 100644 --- a/src/rdb_transcoder.c +++ b/src/rdb_transcoder.c @@ -139,8 +139,13 @@ static int rdbTranscoderRun(rdbTranscoder *t, const uint8_t *data, size_t len) { } } if (r == STREAM_PUSH_READER_NEED_OUTPUT) continue; - /* OK: input consumed. FRAME_DONE: the frame ended; the caller has already - * excluded any trailing bytes, so there is nothing more to feed. */ + /* The caller feeds only the RDB body, so nothing may follow the frame. + * The reader keeps any such bytes pending, and feeding it again would + * trip its resume assertion, so fail the transcode instead. */ + if (r == STREAM_PUSH_READER_FRAME_DONE && streamPushReaderHasPendingDecode(&t->reader)) { + t->failed = true; + return C_ERR; + } break; } return C_OK; diff --git a/src/unit/test_compression.cpp b/src/unit/test_compression.cpp index cb1d35860..b4a7ed8e1 100644 --- a/src/unit/test_compression.cpp +++ b/src/unit/test_compression.cpp @@ -12,6 +12,7 @@ extern "C" { #include "compression.h" #include "compression_stream.h" +#include "rdb_transcoder.h" #include "server.h" #include "zmalloc.h" } @@ -1794,3 +1795,53 @@ TEST(replCompression, pushReaderDrainsBufferedOutputWithoutMoreInput) { freeReplTestStream(&test_stream); zfree(payload); } + +/* The disk-based full sync feeds the transcoder only the RDB body, so nothing + * may follow the source frame. Trailing bytes, whether they arrive with the + * frame end or in a later write, must fail the transcode rather than be + * dropped or trip the push reader's resume assertion. */ +TEST(CompressionTest, rdbTranscoderRejectsBytesAfterSourceFrame) { + const char *payload = "transcoder-frame-body"; + const char *trailer = "TRAILER"; + const size_t ntargets = 1 + sizeof(testCompressionAlgos) / sizeof(testCompressionAlgos[0]); + + for (compressionAlgo src : testCompressionAlgos) { + DynamicBuf frame; + dynamicBufInit(&frame); + streamWriter w; + ASSERT_EQ(streamWriterInit(&w, src, true, emitToDynamicBuf, &frame), C_OK); + ASSERT_EQ(streamWriterWrite(&w, payload, strlen(payload)), C_OK); + ASSERT_EQ(streamWriterFinish(&w), C_OK); + streamWriterFree(&w); + const size_t frame_len = sdslen((sds)frame.data); + sds with_trailer = sdscatlen(sdsnewlen(frame.data, frame_len), trailer, strlen(trailer)); + + for (size_t ti = 0; ti < ntargets; ti++) { + compressionAlgo target = ti == 0 ? ALGO_NONE : testCompressionAlgos[ti - 1]; + if (target == src) continue; /* Verbatim route never decodes the frame. */ + for (bool same_write : {true, false}) { + SCOPED_TRACE(testing::Message() << "src=" << src << " target=" << target + << " same_write=" << same_write); + DynamicBuf out; + dynamicBufInit(&out); + rdbTranscoder t; + rdbTranscoderInit(&t, target, false, emitToDynamicBuf, &out); + if (same_write) { + EXPECT_EQ(rdbTranscoderWrite(&t, with_trailer, sdslen(with_trailer)), C_ERR); + } else { + /* The trailer spans two writes, as it does when the receive + * loop reads it in more than one chunk. */ + ASSERT_EQ(rdbTranscoderWrite(&t, frame.data, frame_len), C_OK); + EXPECT_EQ(rdbTranscoderWrite(&t, trailer, 3), C_ERR); + EXPECT_EQ(rdbTranscoderWrite(&t, trailer + 3, strlen(trailer) - 3), C_ERR); + } + EXPECT_TRUE(t.failed); + EXPECT_EQ(rdbTranscoderFinish(&t), C_ERR); + rdbTranscoderFree(&t); + dynamicBufFree(&out); + } + } + sdsfree(with_trailer); + dynamicBufFree(&frame); + } +}