Skip to content

zlib: fix pointer alignment #57727

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 9, 2025
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
8 changes: 5 additions & 3 deletions src/node_zlib.cc
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,8 @@ class CompressionStream : public AsyncWrap, public ThreadPoolWork {
}

static void* AllocForBrotli(void* data, size_t size) {
size += sizeof(size_t);
constexpr size_t offset = std::max(sizeof(size_t), alignof(max_align_t));
size += offset;
CompressionStream* ctx = static_cast<CompressionStream*>(data);
char* memory = UncheckedMalloc(size);
if (memory == nullptr) [[unlikely]] {
Expand All @@ -617,15 +618,16 @@ class CompressionStream : public AsyncWrap, public ThreadPoolWork {
*reinterpret_cast<size_t*>(memory) = size;
ctx->unreported_allocations_.fetch_add(size,
std::memory_order_relaxed);
return memory + sizeof(size_t);
return memory + offset;
}

static void FreeForZlib(void* data, void* pointer) {
if (pointer == nullptr) [[unlikely]] {
return;
}
CompressionStream* ctx = static_cast<CompressionStream*>(data);
char* real_pointer = static_cast<char*>(pointer) - sizeof(size_t);
constexpr size_t offset = std::max(sizeof(size_t), alignof(max_align_t));
char* real_pointer = static_cast<char*>(pointer) - offset;
size_t real_size = *reinterpret_cast<size_t*>(real_pointer);
ctx->unreported_allocations_.fetch_sub(real_size,
std::memory_order_relaxed);
Expand Down
Loading