https://godbolt.org/z/DY9ruv void if_delete(char *p) { if (p) { delete(p); } } $ gcc-8.2 -Os -fno-exceptions if_delete(char*): test rdi, rdi je .L1 mov esi, 1 jmp operator delete(void*, unsigned long) .L1: ret While clang removes the check at -Oz: $ clang -Oz -fno-exceptions if_delete(char*): jmp operator delete(void*)
Confirmed. IIRC there are duplicates of this bug.
(In reply to Richard Biener from comment #1) > Confirmed. IIRC there are duplicates of this bug. Well related bugs for the plain-C equivalent with free() at least
Still happening with gcc trunk. https://godbolt.org/z/5K94665GK
Seems like clang now added the check: $ clang++ -Oz -fno-exceptions if_delete(char*): # @if_delete(char*) test rdi, rdi jne operator delete(void*)@PLT # TAILCALL ret
As per: https://en.cppreference.com/w/cpp/memory/new/operator_delete """ In all cases, if ptr is a null pointer, the standard library deallocation functions do nothing. If the pointer passed to the standard library deallocation function was not obtained from the corresponding standard library allocation function, the behavior is undefined. """ So it should be fine to remove the check `if(p)`
Opened a bug for clang as well: https://github.com/llvm/llvm-project/issues/62783