Implementation-defined behavior on malloc(0)
Description
The C99 states that if the requested size for malloc, realloc, calloc, is 0, then the behavior of these functions is implementation defined.
Under Linux and Windows, they return a valid pointer to 0 bytes of allocated memory. Under FreeRTOS, our current implementation returns the NULL pointer.
These different behaviors must be uniformized for all platforms and documented.
Analysis
Only an in-depth analysis of the runtime behavior may underline where the memory allocation APIs are called with 0 size.
Short-term fixes
- change the FreeRTOS implementation to return a valid pointer (e.g. requesting at least 4 bytes of memory to the system),
- document the wrappers.
Long-term fixes
- change the wrapper implementation and either
assert(size > 0)
orreturn NULL
if size is 0, - find and fix calls with 0 size,
- document the wrappers.