ftmemsim-valgrind/memcheck/tests/realloc_size_zero.c
Paul Floyd 50bded71b2 Bug 436413 - Warn about realloc of size zero
Adds a new warning to memcheck when realloc is used with a size of 0.
For a long time this has been "implementation defined" and so
non-portable. With C23 it will become UB.

Also adds a switch to turn off the error generation and a
second switch to select between the most common
"implementation" behaviours. The defaults for this second
switch are baked in at build time.
2023-03-10 21:55:14 +01:00

40 lines
702 B
C

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
int main(void)
{
int i;
char* p = malloc(1024);
p[0] = '\0';
errno = 0;
p = realloc(p, 0);
if (p) {
printf("p not NULL after realloc 0\n");
} else {
printf("p is NULL after realloc 0\n");
}
if (errno) {
perror("realloc(something, 0):");
}
if (p) {
free(p);
}
errno = 0;
volatile void *ptr = NULL;
volatile size_t size = 0U;
char *p2 = realloc(ptr, size);
if (p2) {
printf("p2 not NULL after realloc 0\n");
} else {
printf("p2 is NULL after realloc 0\n");
}
if (errno) {
perror("realloc(NULL, 0):");
}
if (p2) {
free(p2);
}
}