Safe realloc: frees old pointer on failure instead of leaking it. * Returns NULL on allocation failure (old memory is freed). */
| 20 | /* Safe realloc: frees old pointer on failure instead of leaking it. |
| 21 | * Returns NULL on allocation failure (old memory is freed). */ |
| 22 | static inline void *safe_realloc(void *ptr, size_t size) { |
| 23 | enum { SAFE_REALLOC_MIN = 1 }; |
| 24 | if (size == 0) { |
| 25 | size = SAFE_REALLOC_MIN; |
| 26 | } |
| 27 | void *tmp = realloc(ptr, size); |
| 28 | if (!tmp) { |
| 29 | free(ptr); |
| 30 | } |
| 31 | return tmp; |
| 32 | } |
| 33 | |
| 34 | /* Safe free: frees and NULLs a pointer to prevent double-free / use-after-free. |
| 35 | * Use via the safe_free() macro so the caller's pointer is actually cleared. */ |
no outgoing calls
no test coverage detected