Skip to content

Instantly share code, notes, and snippets.

@fluks
Created October 8, 2015 02:22
Show Gist options
  • Save fluks/e94da1c29bb2315c62e1 to your computer and use it in GitHub Desktop.
Save fluks/e94da1c29bb2315c62e1 to your computer and use it in GitHub Desktop.
xrealloc modification
void* new_ptr = realloc(ptr, size);
- if (!new_ptr) {
- // If size is zero, free() was called, instead of realloc(). Don't use after free().
- if (size != 0) {
- fprintf(stderr, "\nxrealloc(%p, %zu): %s\n", ptr, size, strerror(errno));
- free(ptr);
- }
+ // If size is zero, realloc() is equivalent to free().
+ if (!new_ptr && size != 0) {
+ fprintf(stderr, "\nxrealloc(%p, %zu): %s\n", ptr, size, strerror(errno));
+ free(ptr);
exit(errno);
}
return new_ptr;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment