1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 18:18:12 +00:00

LibC: Make *alloc return NULL in case of failure (POSIX)

This commit is contained in:
Michel Hermier 2022-01-12 13:37:54 +01:00 committed by Andreas Kling
parent 3bf89f1859
commit 1af072e0f3
3 changed files with 59 additions and 2 deletions

40
Tests/LibC/TestMalloc.cpp Normal file
View file

@ -0,0 +1,40 @@
/*
* Copyright (c) 2022, the SerenityOS developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibTest/TestCase.h>
#include <LibC/mallocdefs.h>
#include <errno.h>
#include <stdlib.h>
TEST_CASE(malloc_limits)
{
EXPECT_NO_CRASH("Allocation of 0 size should succed at allocation and release", [] {
errno = 0;
void* ptr = malloc(0);
EXPECT_EQ(errno, 0);
free(ptr);
return Test::Crash::Failure::DidNotCrash;
});
EXPECT_NO_CRASH("Allocation of the maximum `size_t` value should fails with `ENOMEM`", [] {
errno = 0;
void* ptr = malloc(NumericLimits<size_t>::max());
EXPECT_EQ(errno, ENOMEM);
EXPECT_EQ(ptr, nullptr);
free(ptr);
return Test::Crash::Failure::DidNotCrash;
});
EXPECT_NO_CRASH("Allocation of the maximum `size_t` value that does not overflow should fails with `ENOMEM`", [] {
errno = 0;
void* ptr = malloc(NumericLimits<size_t>::max() - ChunkedBlock::block_size - sizeof(BigAllocationBlock));
EXPECT_EQ(errno, ENOMEM);
EXPECT_EQ(ptr, nullptr);
free(ptr);
return Test::Crash::Failure::DidNotCrash;
});
}