An idea for a tool to test memory allocation failure handling
It's best practice to check the return value of allocation, but since allocation nearly always succeeds, this code is often completely untested.
So the idea is that you run your test case with this tool, I'll call it malcheck for now, and it overrides the malloc/calloc/realloc symbols in libc to its own. These symbols just call the original ones, with a couple differences:
* Does the standard memory leak checking, tools for this exist already. * There's a number, N. The Nth allocation returns NULL.
So the semantics of the test case is that it should test some thing, but any time it gets an error code that is "out of memory" it should try again.
Then this tool runs the test case over and over again, for different values of N. Maybe it starts with 0 and increments it linearly (this should theoretically test every allocation if you make it to a large enough number N). Maybe it uses randomness with a seed to choose N.
If the test case exits successfully and with no memory leaks, then it passes the malcheck test for that value of N. Otherwise, malcheck tells you what N is and you can run your code again with the same configuration to duplicate the error.