Serenity Operating System
1/*
2 * Copyright (c) 2022, the SerenityOS developers.
3 *
4 * SPDX-License-Identifier: BSD-2-Clause
5 */
6
7#include <LibTest/TestCase.h>
8
9#include <errno.h>
10#include <mallocdefs.h>
11#include <stdlib.h>
12
13TEST_CASE(malloc_limits)
14{
15 EXPECT_NO_CRASH("Allocation of 0 size should succeed at allocation and release", [] {
16 errno = 0;
17 void* ptr = malloc(0);
18 EXPECT_EQ(errno, 0);
19 free(ptr);
20 return Test::Crash::Failure::DidNotCrash;
21 });
22
23 EXPECT_NO_CRASH("Allocation of the maximum `size_t` value should fails with `ENOMEM`", [] {
24 errno = 0;
25 void* ptr = malloc(NumericLimits<size_t>::max());
26 EXPECT_EQ(errno, ENOMEM);
27 EXPECT_EQ(ptr, nullptr);
28 free(ptr);
29 return Test::Crash::Failure::DidNotCrash;
30 });
31
32 EXPECT_NO_CRASH("Allocation of the maximum `size_t` value that does not overflow should fails with `ENOMEM`", [] {
33 errno = 0;
34 void* ptr = malloc(NumericLimits<size_t>::max() - ChunkedBlock::block_size - sizeof(BigAllocationBlock));
35 EXPECT_EQ(errno, ENOMEM);
36 EXPECT_EQ(ptr, nullptr);
37 free(ptr);
38 return Test::Crash::Failure::DidNotCrash;
39 });
40}