Created
October 8, 2015 02:22
-
-
Save fluks/e94da1c29bb2315c62e1 to your computer and use it in GitHub Desktop.
xrealloc modification
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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