Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions include/mgard-x/Lossless/Zstd.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,19 @@ template <typename DeviceType> class Zstd {

uint32_t actual_out_count = 0;
actual_out_count = *reinterpret_cast<const size_t *>(in_data);
Resize(actual_out_count, compressionLevel, queue_idx);

// FIX: Only resize out_data if needed, do not touch in_data which contains
// the compressed data we just copied. The original code called Resize()
// which would free and reallocate BOTH in_data and out_data, destroying
// the compressed data when actual_out_count > buffer_size.
if (this->buffer_size < actual_out_count) {
size_t const estimated_out_size = ZSTD_compressBound(actual_out_count);
if (out_data != nullptr) {
MemoryManager<DeviceType>::FreeHost(out_data, queue_idx);
}
MemoryManager<DeviceType>::MallocHost(
out_data, estimated_out_size + sizeof(size_t), queue_idx);
}
DeviceRuntime<DeviceType>::SyncQueue(queue_idx);

ZSTD_decompress(out_data, actual_out_count, in_data + sizeof(size_t),
Expand All @@ -137,4 +149,4 @@ template <typename DeviceType> class Zstd {

} // namespace mgard_x

#endif
#endif
Loading