Skip to content

Commit f74175a

Browse files
lum1n0usclaude
andcommitted
test(mem-alloc): add realloc rejection tests
Verify realloc correctly rejects aligned allocations and still works for normal allocations. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
1 parent 3f517a1 commit f74175a

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

tests/unit/mem-alloc/mem_alloc_test.c

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,60 @@ test_aligned_alloc_valid_alignments(void **state)
9191

9292
mem_allocator_destroy(allocator);
9393
}
94+
95+
/* Test: Realloc rejects aligned allocations */
96+
static void
97+
test_realloc_rejects_aligned(void **state)
98+
{
99+
mem_allocator_t allocator;
100+
char heap_buf[64 * 1024];
101+
void *ptr, *new_ptr;
102+
103+
allocator = mem_allocator_create(heap_buf, sizeof(heap_buf));
104+
assert_non_null(allocator);
105+
106+
/* Allocate aligned */
107+
ptr = mem_allocator_malloc_aligned(allocator, 128, 64);
108+
assert_non_null(ptr);
109+
assert_true(is_aligned_allocation(ptr));
110+
111+
/* Realloc should reject aligned allocation */
112+
new_ptr = mem_allocator_realloc(allocator, ptr, 256);
113+
assert_null(new_ptr);
114+
115+
/* Original pointer should still be valid - free it */
116+
mem_allocator_free(allocator, ptr);
117+
118+
mem_allocator_destroy(allocator);
119+
}
120+
121+
/* Test: Realloc still works for normal allocations */
122+
static void
123+
test_normal_realloc_works(void **state)
124+
{
125+
mem_allocator_t allocator;
126+
char heap_buf[64 * 1024];
127+
void *ptr, *new_ptr;
128+
129+
allocator = mem_allocator_create(heap_buf, sizeof(heap_buf));
130+
assert_non_null(allocator);
131+
132+
/* Allocate normal */
133+
ptr = mem_allocator_malloc(allocator, 128);
134+
assert_non_null(ptr);
135+
136+
/* Write some data */
137+
memset(ptr, 0xAB, 128);
138+
139+
/* Realloc should work */
140+
new_ptr = mem_allocator_realloc(allocator, ptr, 256);
141+
assert_non_null(new_ptr);
142+
143+
/* Data should be preserved */
144+
for (int i = 0; i < 128; i++) {
145+
assert_int_equal(((unsigned char *)new_ptr)[i], 0xAB);
146+
}
147+
148+
mem_allocator_free(allocator, new_ptr);
149+
mem_allocator_destroy(allocator);
150+
}

tests/unit/mem-alloc/test_runner.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ main(void)
1818
const struct CMUnitTest tests[] = {
1919
cmocka_unit_test(test_normal_alloc_basic),
2020
cmocka_unit_test(test_aligned_alloc_valid_alignments),
21+
cmocka_unit_test(test_realloc_rejects_aligned),
22+
cmocka_unit_test(test_normal_realloc_works),
2123
};
2224

2325
return cmocka_run_group_tests(tests, NULL, NULL);

0 commit comments

Comments
 (0)