This code was in PuTTY for less than a year. But in that time it managed to expose a bug in a C compiler. It was a small, independently written compiler (none of the huge code bases everyone's heard of now). We had an email from its maintainer to let us know – and to ask us what the code was _meant_ to do, because fair enough, it isn't obvious to everybody at first glance!
Show more of this post
The bug was that the compiler optimised away if (0) { ... } statements, on the theory that they were unreachable. But the code in these if (0) statements _is_ reachable, via the case labels related to the containing switch. So the compiler had thrown away code that was actually needed, and then got confused when it couldn't generate the jump for those switch cases. (The more usual strategy is to start by turning the whole function into a control-flow graph, i.e. blocks of straight-line code in a directed graph connected by edges that are effectively gotos. Then you can optimise conditional branches based on a constant into unconditional branches, deleting the never-traversed graph edge out of their block. Then you detect unreachable code by a search through the graph from the function entry point. That strategy avoids this problem.)



