Skip to content

Commit 7ec79df

Browse files
Do not acknowledge valid messages when decompression runs out of memory
`inflateInit2` and `inflate` report an allocation failure as `Z_MEM_ERROR`, and `ZSTD_decompress` as `ZSTD_error_memory_allocation`. Both were turned into a plain decoding failure, so `uncompressMessageIfNeeded` treated the message as corrupted and acknowledged it, losing valid data under memory pressure. Throw `std::bad_alloc` instead (after `inflateEnd` for zlib). The existing exception handler in `uncompressMessageIfNeeded` then closes the connection without acknowledging, and the message is redelivered after reconnecting. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 023ace8 commit 7ec79df

2 files changed

Lines changed: 18 additions & 0 deletions

File tree

‎lib/CompressionCodecZLib.cc‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
#include <zlib.h>
2222

23+
#include <new>
24+
2325
#include "LogUtils.h"
2426

2527
DECLARE_LOG_OBJECT()
@@ -53,6 +55,11 @@ static bool buffer_uncompress(const char *compressedBuffer, unsigned long compre
5355
stream.opaque = NULL;
5456

5557
int res = inflateInit2(&stream, MAX_WBITS);
58+
// An allocation failure does not mean that the message is corrupted: throw, so that the consumer
59+
// does not acknowledge a valid message but closes the connection to have it redelivered.
60+
if (res == Z_MEM_ERROR) {
61+
throw std::bad_alloc();
62+
}
5663
if (res != Z_OK) {
5764
LOG_ERROR("Failed to initialize inflate stream: " << res);
5865
return false;
@@ -86,6 +93,10 @@ static bool buffer_uncompress(const char *compressedBuffer, unsigned long compre
8693
const uInt remainingIn = stream.avail_in;
8794
inflateEnd(&stream);
8895

96+
if (res == Z_MEM_ERROR) {
97+
throw std::bad_alloc();
98+
}
99+
89100
// A complete zlib stream ends with `Z_STREAM_END` after its checksum was verified.
90101
// The Java client compresses with a sync flush and does not finish the stream, so
91102
// its output has no final block and no checksum: accept it only when all the input

‎lib/CompressionCodecZstd.cc‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020

2121
#if HAS_ZSTD
2222
#include <zstd.h>
23+
#include <zstd_errors.h>
24+
25+
#include <new>
2326

2427
namespace pulsar {
2528

@@ -47,6 +50,10 @@ bool CompressionCodecZstd::decode(const SharedBuffer& encoded, uint32_t uncompre
4750
decompressed.bytesWritten(uncompressedSize);
4851
decoded = decompressed;
4952
return true;
53+
} else if (ZSTD_isError(result) && ZSTD_getErrorCode(result) == ZSTD_error_memory_allocation) {
54+
// An allocation failure does not mean that the message is corrupted: throw, so that the consumer
55+
// does not acknowledge a valid message but closes the connection to have it redelivered.
56+
throw std::bad_alloc();
5057
} else {
5158
// Decompression failed
5259
return false;

0 commit comments

Comments
 (0)