]> git.ipfire.org Git - thirdparty/vectorscan.git/commitdiff
Fix segfaults on allocation failure (#4)
authorhongyang7 <yang.a.hong@intel.com>
Thu, 16 Dec 2021 11:02:17 +0000 (19:02 +0800)
committerKonstantinos Margaritis <konstantinos@vectorcamp.gr>
Mon, 29 Aug 2022 12:03:18 +0000 (15:03 +0300)
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 <johanngan.us@gmail.com>
fixes github issue #317 (PR #320)

src/util/alloc.h

index de20c8d028e4112f377ad8bf5f00d21209b65589..49b4a824d10d639e7310d16e6e450238f37e757e 100644 (file)
@@ -76,7 +76,11 @@ public:
 
     T *allocate(std::size_t size) const {
         size_t alloc_size = size * sizeof(T);
-        return static_cast<T *>(aligned_malloc_internal(alloc_size, N));
+        T *ptr = static_cast<T *>(aligned_malloc_internal(alloc_size, N));
+        if (!ptr) {
+            throw std::bad_alloc();
+        }
+        return ptr;
     }
 
     void deallocate(T *x, std::size_t) const noexcept {