Skip to content

Commit

Permalink
Set errno in various failure cases. Do not attempt to zero nullptr.
Browse files Browse the repository at this point in the history
  • Loading branch information
ronorton committed Feb 22, 2022
1 parent 6a71b84 commit 6f6f4df
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/mem/chunkallocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ namespace snmalloc
if (slab_sizeclass >= NUM_SLAB_SIZES)
{
// Your address space is not big enough for this allocation!
errno = ENOMEM;
return {nullptr, nullptr};
}

Expand Down
7 changes: 6 additions & 1 deletion src/mem/external_alloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,20 @@ namespace snmalloc::external_alloc
if constexpr (bits::BITS == 64)
{
if (size >= 0x10000000000)
{
errno = ENOMEM;
return nullptr;
}
}

if (alignment < sizeof(void*))
alignment = sizeof(void*);

void* result;
if (posix_memalign(&result, alignment, size) != 0)
int err = posix_memalign(&result, alignment, size);
if (err != 0)
{
errno = err;
result = nullptr;
}
return result;
Expand Down
4 changes: 2 additions & 2 deletions src/mem/localalloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ namespace snmalloc
if (meta != nullptr)
meta->initialise_large();

if (zero_mem == YesZero)
if (zero_mem == YesZero && chunk.unsafe_ptr() != nullptr)
{
SharedStateHandle::Pal::template zero<false>(
chunk.unsafe_ptr(), size);
Expand Down Expand Up @@ -427,7 +427,7 @@ namespace snmalloc
// would guarantee.
void* result = external_alloc::aligned_alloc(
natural_alignment(size), round_size(size));
if constexpr (zero_mem == YesZero)
if (zero_mem == YesZero && result != nullptr)
memset(result, 0, size);
return result;
#else
Expand Down
7 changes: 6 additions & 1 deletion src/pal/pal_windows.h
Original file line number Diff line number Diff line change
Expand Up @@ -186,13 +186,18 @@ namespace snmalloc

void* ret = VirtualAlloc2FromApp(
nullptr, nullptr, size, flags, PAGE_READWRITE, &param, 1);
if (ret == nullptr)
errno = ENOMEM;
return ret;
}
# endif

static void* reserve(size_t size) noexcept
{
return VirtualAlloc(nullptr, size, MEM_RESERVE, PAGE_READWRITE);
void* ret = VirtualAlloc(nullptr, size, MEM_RESERVE, PAGE_READWRITE);
if (ret == nullptr)
errno = ENOMEM;
return ret;
}

/**
Expand Down

0 comments on commit 6f6f4df

Please sign in to comment.