C89 4.10.3.2 The free function
C99 7.20.3.2 The free function
C11 7.22.3.3 The free function
If ptr is a null pointer, no action shall occur.
_aligned_free on MSDN:
If memblock is a NULL pointer, this function simply performs no actions.
Differential D52401
Remove redundant null pointer check in operator delete MaskRay on Sep 22 2018, 10:17 PM. Authored by
Details
C89 4.10.3.2 The free function If ptr is a null pointer, no action shall occur. _aligned_free on MSDN: If memblock is a NULL pointer, this function simply performs no actions.
Diff Detail
Event TimelineComment Actions Was this true pre-C11 too? If not, then this needs to be guarded by #if _LIBCPP_STD_VER >= 17, because C++ is only on top of C11 in C++17 and above (Marshall can double-check this). Comment Actions Thanks for the note. It is true in C89 (http://port70.net/~nsz/c/c89/c89-draft.html). Comment Actions You did not get a thumbs up from any of the code owners for libc++. Reverted in r342938. Comment Actions LGTM as well, unless @mclow.lists can tell us some history like interactions with K&R libc :) Comment Actions I suspect it's fine, but I need to check some stuff on old versions of glibc (I seem to recall a problem with that). Comment Actions I just checked an extremely old version of glibc fetched from https://ftp.gnu.org/pub/gnu/glibc/ glibc-2.0.1.tar.gz 1997-02-04 03:00 3.7M malloc/malloc.c #if __STD_C void fREe(Void_t* mem) #else void fREe(mem) Void_t* mem; #endif { arena *ar_ptr; mchunkptr p; /* chunk corresponding to mem */ #if defined(_LIBC) || defined(MALLOC_HOOKS) if (__free_hook != NULL) { (*__free_hook)(mem); return; } #endif if (mem == 0) /* free(0) has no effect */ return; __free_hook (defaults to NULL) is a user-supplied hook (https://www.gnu.org/software/libc/manual/html_node/Hooks-for-Malloc.html). If this failed for operator delete, it would mean various other free(NULL) would also fail. Comment Actions
I would like to know if your impression came from the common PWN technique when the attacker found a heap buffer overflow :) Comment Actions Very interesting, that means if we don't apply this patch, we essentially breaks glic __free_hook, because that one expects to be able to observe null pointers. |