Reminder: advanced compiler novelties are upon us

Paul Sokolovsky


This is old news, and there was a related drama at Linux kernel
community long ago. So, with uber-advanced compilers, like GCC 5/6
(Zephyr SDK 0.9 uses 6.2.0), at a certain optimization level (the one
Zephyr uses), if you ever write (unconditionally):


(some macro can write it for you, too)

then compiler thinks that you've signed a contract that foo is non-NULL
(because why would you deference it unconditionally otherwise), so any
later NULL checks will be optimized out. Yet, it could be the case that
you just prototype code, so not all checks are yet there (worse, you
can forget to check for NULL somewhere where you don't immediately
expect it). Then you can get interesting output (and behavior) like:

net_nuf: 0, net_buf == NULL: 0, !net_buf: 0

This is of especial interest for kernel/baremetal programming. For
example, in Zephyr (depending on a platform) dereferencing NULL pointer
is all nice and gets you values back, no crashes, and then beyond that
some parts of your code may be removed.

P.S. That's of course the reason why gcc 4.x branch will live for much,
much longer than even proverbial gcc 2.95 of egcs fork fame.

Best Regards,
Paul | Open source software for ARM SoCs
Follow Linaro:!/linaroorg -

Join to automatically receive all group messages.