Re: Development is hard if you can't trust your compiler (aka gcc6 did it again)


Paul Sokolovsky
 

Hello Daniel,

On Tue, 18 Jul 2017 14:30:06 +0100
Daniel Thompson <daniel.thompson@linaro.org> wrote:

On 18/07/17 13:50, Paul Sokolovsky wrote:
Hello,

Previously, there already were examples of the compiler fanciness:
https://lists.zephyrproject.org/pipermail/zephyr-devel/2017-March/007426.html

Here's another one, which is a pure bug. Given code like:

int i;
if (pollnum < NUM_FDS) {
i = pollnum++;
} else {
for (i = 0; pollfds[i].fd >= 0; i++) {
if (i >= NUM_FDS) {
return -1;
}
}
}
What is the definition of pollfds; if is has NUM_FDS elements then
the bug is in the code above rather than being in the compiler!
Heh, removing all I wrote in the unsent version of this reply, indeed,
it's my bug. It's the "for" played bad trick on me, seeing its surface
form one can forget that first "for"'s condition is checked, then "if"
inside is checked, and only then "for"'s trailing operation is executed.
Apparently, I subconsciously wanted the inside "if" to run after
increment, d'oh!

So, I withdraw "the real bug" valuation, it's just another case when
very well defined "undefined" behavior is turned by outsmart
compiler into truly undefinable behavior (all for programmer's good, as
in this case - I of course rewrote that loop before submitting it, btw
it was written like that in the first place to avoid "goto" (or
checking the same var twice, which is no-no!)).

Daniel.
--
Best Regards,
Paul

Linaro.org | Open source software for ARM SoCs
Follow Linaro: http://www.facebook.com/pages/Linaro
http://twitter.com/#!/linaroorg - http://www.linaro.org/linaro-blog

Join devel@lists.zephyrproject.org to automatically receive all group messages.