Skip to content

Commit b5441a8

Browse files
jhofsteemarco-ippolito
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 <shelley.vohr@gmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de> Reviewed-By: Daniel Lemire <daniel@lemire.me> Reviewed-By: Gerhard Stöbich <deb2001-github@yahoo.de>
1 parent 789eabd commit b5441a8

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
@@ -493,20 +493,22 @@ class CompressionStream : public AsyncWrap, public ThreadPoolWork {
493493
}
494494

495495
static void* AllocForBrotli(void* data, size_t size) {
496-
size += sizeof(size_t);
496+
constexpr size_t offset = std::max(sizeof(size_t), alignof(max_align_t));
497+
size += offset;
497498
CompressionStream* ctx = static_cast<CompressionStream*>(data);
498499
char* memory = UncheckedMalloc(size);
499500
if (UNLIKELY(memory == nullptr)) return nullptr;
500501
*reinterpret_cast<size_t*>(memory) = size;
501502
ctx->unreported_allocations_.fetch_add(size,
502503
std::memory_order_relaxed);
503-
return memory + sizeof(size_t);
504+
return memory + offset;
504505
}
505506

506507
static void FreeForZlib(void* data, void* pointer) {
507508
if (UNLIKELY(pointer == nullptr)) return;
508509
CompressionStream* ctx = static_cast<CompressionStream*>(data);
509-
char* real_pointer = static_cast<char*>(pointer) - sizeof(size_t);
510+
constexpr size_t offset = std::max(sizeof(size_t), alignof(max_align_t));
511+
char* real_pointer = static_cast<char*>(pointer) - offset;
510512
size_t real_size = *reinterpret_cast<size_t*>(real_pointer);
511513
ctx->unreported_allocations_.fetch_sub(real_size,
512514
std::memory_order_relaxed);

0 commit comments

Comments
 (0)