mirror of
https://github.com/Zenithsiz/ftmemsim-valgrind.git
synced 2026-02-03 01:51:29 +00:00
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.
42 lines
841 B
C++
42 lines
841 B
C++
#include <iostream>
|
|
#include <cstdlib>
|
|
#include <cerrno>
|
|
|
|
using std::realloc;
|
|
using std::cout;
|
|
using std::perror;
|
|
|
|
int main(void)
|
|
{
|
|
char* p = new char[1024];
|
|
p[0] = '\0';
|
|
errno = 0;
|
|
// mismatch
|
|
p = static_cast<char *>(realloc(p, 0));
|
|
if (p) {
|
|
cout << "p not nullptr after realloc 0\n";
|
|
} else {
|
|
cout << "p is nullptr after realloc 0\n";
|
|
}
|
|
if (errno) {
|
|
perror("realloc(something, 0):");
|
|
}
|
|
// mismatch again
|
|
delete [] p;
|
|
|
|
errno = 0;
|
|
volatile void *ptr = NULL;
|
|
volatile size_t size = 0U;
|
|
char *p2 = static_cast<char *>(realloc(const_cast<void*>(ptr), size));
|
|
if (p2) {
|
|
cout << "p2 not nullptr after realloc 0\n";
|
|
} else {
|
|
cout << "p2 is nullptr after realloc 0\n";
|
|
}
|
|
if (errno) {
|
|
perror("realloc(NULL, 0):");
|
|
}
|
|
// mismatch
|
|
delete [] p2;
|
|
}
|