16
NeoSama
4y

While coding in C, I once forgot to add a semicolon at the end of a while loop polling a register value.
The logic required me to make it zero as soon as it read non-zero and continue the rest of the process. Hence the 'while' that missed the semicolon ended up being a single instruction assignment to the same volatile register that I kept polling. This caused synchronisation issue with the FPGA, and my code got stuck in an uncertain infinite loop.
Took me 2 days and a silly, yet valid question from my teammate to figure out the cause of this stupid bug.

Comments
  • 5
    That was unfortunate, but don't forget that the devrant tag is only for devrant related topics not Dev rants.
  • 4
    @p100sch Sorry, first-timer here. Thanks for the info :)
  • 5
    GCC 6 and later warns about misleading indentation when using -Wall, but unfortunately, that does not work in this specific case. I've just tested it.

    In the "forgotten semicolon" version, the statement after the while has the same indentation as the while because it wasn't meant to be in the while scope, but no warning.
  • 2
    @Fast-Nop Yeah. I was using gcc 7+ and -Wall was enabled
  • 4
    @NeoSama Holy shit, not even Clang 10 with -Wall -Wextra -Wpedantic finds that, and neither CppCheck. Good to know that this kind of bug needs manual attention.
  • 2
    @Fast-Nop All credit goes to my teammate for helping me figure out this bug.
  • 3
    There's a reason why many style guides require curly braces in all cases.
  • 1
    @electrineer Okay now I'm adding that to my own template.
Add Comment