1
0
mirror of https://github.com/cc65/cc65.git synced 2024-06-07 07:29:33 +00:00
cc65/test/val/lib_common_malloc.c
Colin Leroy-Mira 3e01ac9b04 Fix malloc and realloc overflow
If user requests a size >= 65532, adding the heap admin size
overflows size. Fixes #2358.
2024-01-24 09:54:54 +01:00

35 lines
871 B
C

#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include "unittest.h"
TEST
{
char *buf, *buf2;
unsigned int i;
buf = malloc(0);
ASSERT_IsTrue (buf == NULL, "malloc (0) returned something");
for (i = 1; i < 10; i++) {
buf = malloc(i);
ASSERT_IsTrue (buf != NULL, "small returned nothing");
}
buf = malloc(4096);
ASSERT_IsTrue (buf != NULL, "malloc (4096) returned nothing");
buf = malloc(61000UL);
ASSERT_IsTrue (buf == NULL, "malloc (61000) returned something");
for (i = 65535UL; i > _heapmaxavail(); i--) {
buf = malloc(i);
ASSERT_IsTrue (buf == NULL, "malloc returned something but shouldn't have");
}
buf = malloc(i);
ASSERT_IsTrue (buf != NULL, "malloc returned nothing but should have");
ASSERT_IsTrue(_heapmaxavail() == 0, "heapmaxavail should be 0");
}
ENDTEST