[PATCH v2 3/3] perf record: Don't store raw data on zstd compression failure

From: Dmitry Ilvokhin

Date: Tue Jun 30 2026 - 03:20:45 EST


On a ZSTD_compressStream() error, zstd_compress_stream_to_records()
falls back to copying the uncompressed input into the output buffer and
returning its size. That cannot work:

- a COMPRESSED2 payload is always fed to zstd_decompress_stream(), so
raw bytes placed there can never be read back.

- the record header is never finalized: process_header() is not called
to write the real header.size and data_size, so they keep their
initial values and do not match the bytes written.

- the copy is unbounded and can write past the output buffer.

Propagate the error instead.

Fixes: f24c1d7523e6 ("perf tools: Introduce Zstd streaming based compression API")
Signed-off-by: Dmitry Ilvokhin <d@xxxxxxxxxxxx>
---
tools/perf/util/zstd.c | 5 +----
1 file changed, 1 insertion(+), 4 deletions(-)

diff --git a/tools/perf/util/zstd.c b/tools/perf/util/zstd.c
index 04910dfed84f..cbb150b0f807 100644
--- a/tools/perf/util/zstd.c
+++ b/tools/perf/util/zstd.c
@@ -1,7 +1,5 @@
// SPDX-License-Identifier: GPL-2.0

-#include <string.h>
-
#include "util/compress.h"
#include "util/debug.h"

@@ -71,8 +69,7 @@ ssize_t zstd_compress_stream_to_records(struct zstd_data *data, void *dst, size_
if (ZSTD_isError(ret)) {
pr_err("failed to compress %ld bytes: %s\n",
(long)src_size, ZSTD_getErrorName(ret));
- memcpy(dst, src, src_size);
- return src_size;
+ return -1;
}
compressed += output.pos;
dst += output.pos;
--
2.53.0-Meta