mirror of
https://github.com/GrapheneOS/hardened_malloc.git
synced 2025-04-19 22:10:19 +02:00
Abort on C23 UB zero sized realloc
This commit is contained in:
parent
1d7fc7ffe0
commit
5f7e7dad20
5 changed files with 30 additions and 1 deletions
|
@ -1513,6 +1513,11 @@ EXPORT void *h_calloc(size_t nmemb, size_t size) {
|
|||
}
|
||||
|
||||
EXPORT void *h_realloc(void *old, size_t size) {
|
||||
// deprecated in C17, UB since C23
|
||||
if (unlikely(old != NULL && size == 0)) {
|
||||
fatal_error("invalid zero sized realloc");
|
||||
}
|
||||
|
||||
size = adjust_size_for_canary(size);
|
||||
if (old == NULL) {
|
||||
return alloc(size);
|
||||
|
|
1
test/.gitignore
vendored
1
test/.gitignore
vendored
|
@ -41,4 +41,5 @@ overflow_small_8_byte
|
|||
uninitialized_read_large
|
||||
uninitialized_read_small
|
||||
realloc_init
|
||||
realloc_c23_undefined_behaviour
|
||||
__pycache__/
|
||||
|
|
|
@ -67,7 +67,8 @@ EXECUTABLES := \
|
|||
invalid_malloc_object_size_small \
|
||||
invalid_malloc_object_size_small_quarantine \
|
||||
impossibly_large_malloc \
|
||||
realloc_init
|
||||
realloc_init \
|
||||
realloc_c23_undefined_behaviour
|
||||
|
||||
all: $(EXECUTABLES)
|
||||
|
||||
|
|
16
test/realloc_c23_undefined_behaviour.c
Normal file
16
test/realloc_c23_undefined_behaviour.c
Normal file
|
@ -0,0 +1,16 @@
|
|||
#include <stdlib.h>
|
||||
|
||||
#include "test_util.h"
|
||||
|
||||
OPTNONE int main(void) {
|
||||
void *p, *q;
|
||||
|
||||
p = malloc(16);
|
||||
if (!p) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
q = realloc(p, 0);
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -169,6 +169,12 @@ class TestSimpleMemoryCorruption(unittest.TestCase):
|
|||
self.assertEqual(stderr.decode("utf-8"),
|
||||
"fatal allocator error: invalid realloc\n")
|
||||
|
||||
def test_realloc_c23_undefined_behaviour(self):
|
||||
_stdout, stderr, returncode = self.run_test("realloc_c23_undefined_behaviour")
|
||||
self.assertEqual(returncode, -6)
|
||||
self.assertEqual(stderr.decode("utf-8"),
|
||||
"fatal allocator error: invalid zero sized realloc\n")
|
||||
|
||||
def test_write_after_free_large_reuse(self):
|
||||
_stdout, _stderr, returncode = self.run_test(
|
||||
"write_after_free_large_reuse")
|
||||
|
|
Loading…
Add table
Reference in a new issue