From: hongyang7 Date: Thu, 16 Dec 2021 11:02:17 +0000 (+0800) Subject: Fix segfaults on allocation failure (#4) X-Git-Tag: vectorscan/5.4.8~1^2~3^2~7 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=2731a3384bbd7ffc4933f6d43478ef2762e5b4d8;p=thirdparty%2Fvectorscan.git Fix segfaults on allocation failure (#4) Throw std::bad_alloc instead of returning nullptr from ue2::AlignedAllocator. Allocators for STL containers are expected never to return with an invalid pointer, and instead must throw on failure. Violating this expectation can lead to invalid pointer dereferences. Co-authored-by: johanngan fixes github issue #317 (PR #320) --- diff --git a/src/util/alloc.h b/src/util/alloc.h index de20c8d0..49b4a824 100644 --- a/src/util/alloc.h +++ b/src/util/alloc.h @@ -76,7 +76,11 @@ public: T *allocate(std::size_t size) const { size_t alloc_size = size * sizeof(T); - return static_cast(aligned_malloc_internal(alloc_size, N)); + T *ptr = static_cast(aligned_malloc_internal(alloc_size, N)); + if (!ptr) { + throw std::bad_alloc(); + } + return ptr; } void deallocate(T *x, std::size_t) const noexcept {