Closed
Description
The clang compiler is not optimizing away pointer check in the foo() function in this code. The gcc removes that part of code as dead code after '-fdelete-null-pointer-checks' pass. We can observe these behaviors in platforms like X86, ARM.
The clang assembly code in x86-64; https://godbolt.org/z/nncroxMx4
The gcc assembly code in x86-64; https://godbolt.org/z/YbqPYGon4
#include <stdio.h>
#include <string.h>
char dest[50];
void foo(char* dest, char* src) {
memcpy(dest, src, strlen(src));
if (!src) { // <------- This is redundant check as pointer is already de-referenced
printf("checking -fdelete-null-pointer-checks compiler optimization\n");
printf("This source code is redundant\n");
__builtin_abort ();
}
printf("After check for null pointer\n");
}
int main ()
{
char src[50] = "-fdelete-null-pointer-checks";
foo(dest, src);
return 0;
}