2 ms·
Thanks to the wonders of UB, the sample on the article when compiled with optimization on, it actually reduces to, //clang -o null_dereference null_derefere
by pjmlp 2y ago
Thanks to the wonders of UB, the sample on the article when compiled with optimization on, it actually reduces to,
//clang -o null_dereference null_dereference.c
#include <stdio.h>
int main() {
int *ptr = NULL; // Initialize a pointer to NULL
int value = *ptr; // Attempt to dereference the NULL pointer
printf("Value: %d\n", value); // This line will reached, because the previous will be optimized away.
return 0;
}
Example, https://godbolt.org/z/Kv3rcz1ch https://godbolt.org/z/Kv3rcz1ch
- deleted 2y ago[deleted]
- pajko 2y agoWhat do you mean? There is nothing optimized away. If there would be an if statement to check for a NULL pointer, yes, that most likely would be optimized away due to UB (if something is undefined, it is not NULL, so the check can be removed...). To make it implementation defined, the -fno-delete-null-pointer-checks parameter can be used: https://news.ycombinator.com/item?id=39463804 https://news.ycombinator.com/item?id=39463804
- pjmlp 2y agoLook at the generated assembly, only printf remains, the assignments preceding it were removed, thus execution reaches printf and fails elsewhere, not at the point shown on the article.