From 554c402de71f7a11196ddc37b598283fb8264d1e Mon Sep 17 00:00:00 2001 From: "Chao Li (Evan)" Date: Mon, 12 Oct 2026 09:30:17 +0800 Subject: [PATCH v1] Avoid further compression and writes after LZ4 close failures LZ4Stream_close() continued to call LZ4F_compressEnd() after a failed buffer flush, and attempted the final write even if compression finalization failed. Skip these operations after a failure, while still freeing resources and closing the file. The function already returned failure in these cases, so this is a cleanup rather than a correctness fix. Author: Chao Li --- src/bin/pg_dump/compress_lz4.c | 36 ++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/src/bin/pg_dump/compress_lz4.c b/src/bin/pg_dump/compress_lz4.c index a4ae1a4272f..bf8b28ceb16 100644 --- a/src/bin/pg_dump/compress_lz4.c +++ b/src/bin/pg_dump/compress_lz4.c @@ -673,25 +673,31 @@ LZ4Stream_close(CompressFileHandle *CFH) state->bufdata = 0; } - status = LZ4F_compressEnd(state->ctx, - state->buffer + state->bufdata, - state->buflen - state->bufdata, - NULL); - if (LZ4F_isError(status)) + if (success) { - pg_log_error("could not end compression: %s", - LZ4F_getErrorName(status)); - success = false; + status = LZ4F_compressEnd(state->ctx, + state->buffer + state->bufdata, + state->buflen - state->bufdata, + NULL); + if (LZ4F_isError(status)) + { + pg_log_error("could not end compression: %s", + LZ4F_getErrorName(status)); + success = false; + } + else + state->bufdata += status; } - else - state->bufdata += status; - errno = 0; - if (fwrite(state->buffer, 1, state->bufdata, state->fp) != state->bufdata) + if (success) { - errno = (errno) ? errno : ENOSPC; - pg_log_error("could not write to output file: %m"); - success = false; + errno = 0; + if (fwrite(state->buffer, 1, state->bufdata, state->fp) != state->bufdata) + { + errno = (errno) ? errno : ENOSPC; + pg_log_error("could not write to output file: %m"); + success = false; + } } status = LZ4F_freeCompressionContext(state->ctx); -- 2.50.1 (Apple Git-155)