Skip to content

Commit 17965eb

Browse files
jhofsteeRafaelGSS
authored andcommitted
zlib: fix pointer alignment
The function AllocForBrotli prefixes the allocated memory with its size, and returns a pointer to the region after it. This pointer can however no longer be suitably aligned. Correct this by allocating the maximum of the the size of the size_t and the max alignment. On Arm 32bits the size_t is 4 bytes long, but the alignment is 8 for some NEON instructions. When Brotli is compiled with optimizations enabled newer GCC versions will use the NEON instructions and trigger a bus error killing node. see google/brotli#1159 PR-URL: #57727 Reviewed-By: Shelley Vohr <[email protected]> Reviewed-By: Tobias Nießen <[email protected]> Reviewed-By: Daniel Lemire <[email protected]> Reviewed-By: Gerhard Stöbich <[email protected]>
1 parent 0bf2c28 commit 17965eb

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

src/node_zlib.cc

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -608,7 +608,8 @@ class CompressionStream : public AsyncWrap, public ThreadPoolWork {
608608
}
609609

610610
static void* AllocForBrotli(void* data, size_t size) {
611-
size += sizeof(size_t);
611+
constexpr size_t offset = std::max(sizeof(size_t), alignof(max_align_t));
612+
size += offset;
612613
CompressionStream* ctx = static_cast<CompressionStream*>(data);
613614
char* memory = UncheckedMalloc(size);
614615
if (memory == nullptr) [[unlikely]] {
@@ -617,15 +618,16 @@ class CompressionStream : public AsyncWrap, public ThreadPoolWork {
617618
*reinterpret_cast<size_t*>(memory) = size;
618619
ctx->unreported_allocations_.fetch_add(size,
619620
std::memory_order_relaxed);
620-
return memory + sizeof(size_t);
621+
return memory + offset;
621622
}
622623

623624
static void FreeForZlib(void* data, void* pointer) {
624625
if (pointer == nullptr) [[unlikely]] {
625626
return;
626627
}
627628
CompressionStream* ctx = static_cast<CompressionStream*>(data);
628-
char* real_pointer = static_cast<char*>(pointer) - sizeof(size_t);
629+
constexpr size_t offset = std::max(sizeof(size_t), alignof(max_align_t));
630+
char* real_pointer = static_cast<char*>(pointer) - offset;
629631
size_t real_size = *reinterpret_cast<size_t*>(real_pointer);
630632
ctx->unreported_allocations_.fetch_sub(real_size,
631633
std::memory_order_relaxed);

0 commit comments

Comments
 (0)