Skip to content

Commit

Permalink
Fix segfaults on allocation failure (#4)
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
hongyang7 authored and fatchanghao committed Apr 21, 2022
1 parent 90a5f90 commit 7d644e7
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/util/alloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,11 @@ class AlignedAllocator {

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 {
Expand Down

0 comments on commit 7d644e7

Please sign in to comment.