4 ms·
That's a nice example. It'd definitely be nice to have a warning for this one. Fwiw GCC does have a related warning flag (-Wnull-dereference) but I'm not sure
by foxfluff 5y ago
That's a nice example. It'd definitely be nice to have a warning for this one.
Fwiw GCC does have a related warning flag (-Wnull-dereference) but I'm not sure it's made exactly for this. I believe it works based on functions being annotated for possibly returning NULL, e.g. malloc. It's also not enabled by -Wall or -W because apparently there were too many false positives: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96554 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96554
I imagine patches would be welcome. I'm guessing there are more people who like to wish for more compiler features than there are people who like to develop compilers :)
- mzs 5y agoedit: Thanks, I just checked and the warning doesn't work https://godbolt.org/z/4TP1hfx4j https://godbolt.org/z/4TP1hfx4j But your hint found -fno-delete-null-pointer-checks which does the trick https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html And -fno-delete-null-pointer-checks made it into LLVM too. It's a good to know but a little late for when I needed it, cheers :) % cc -O3 -fno-delete-null-pointer-checks -Wall -Wextra -c foo.c % objdump -dr foo.o foo.o: file format Mach-O 64-bit x86-64 Disassembly of section __TEXT,__text: 0000000000000000 _foo: 0: 55 pushq %rbp 1: 48 89 e5 movq %rsp, %rbp 4: 48 85 ff testq %rdi, %rdi 7: 74 04 je 4 <_foo+0xd> 9: 8b 07 movl (%rdi), %eax b: 5d popq %rbp c: c3 retq d: bf 02 00 00 00 movl $2, %edi 12: e8 00 00 00 00 callq 0 <_foo+0x17> 0000000000000013: X86_64_RELOC_BRANCH _exit
- astrange 5y agoIf you want a general approach you can turn on ubsan in trap-only mode and see what traps have ended up in your output.