Skip to content

Commit 89c5e97

Browse files
Make the heap-owning SZ_compress overload exception-safe (ClickHouse)
The overload SZ_compress(const Config &, const T *, size_t &) allocated its output buffer with a bare new char[] and returned it raw; if the inner compression (e.g. Lossless_zstd::compress on poorly-compressible data) threw, the buffer leaked. Hold it in std::unique_ptr and release only on success. Co-Authored-By: Claude Fable 5 <[email protected]>
1 parent 8e4db86 commit 89c5e97

1 file changed

Lines changed: 5 additions & 3 deletions

File tree

‎include/SZ3/api/sz.hpp‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
#include "SZ3/api/impl/SZImpl.hpp"
2525
#include "SZ3/version.hpp"
2626

27+
#include <memory>
28+
2729

2830
/**
2931
* Compresses the input data using the provided configuration and stores the result in a pre-allocated buffer.
@@ -95,10 +97,10 @@ char* SZ_compress(const SZ3::Config& config, const T* data, size_t& cmpSize) {
9597
using namespace SZ3;
9698

9799
size_t bufferLen = SZ_compress_size_bound<T>(config);
98-
auto buffer = new char[bufferLen];
99-
cmpSize = SZ_compress(config, data, buffer, bufferLen);
100+
std::unique_ptr<char[]> buffer(new char[bufferLen]);
101+
cmpSize = SZ_compress(config, data, buffer.get(), bufferLen);
100102

101-
return buffer;
103+
return buffer.release();
102104
}
103105

104106
/**

0 commit comments

Comments
 (0)