Spot the problem

Just for once, a vaguely work-related post, but without any work.

What is wrong with this (its in C, of course):

switch (stoat)
    bool goat = TRUE;
    case weasel:
        goat = FALSE
        /* Fallthrough */
    case ferret:

where you should assume that “mustelid” represents a block of code large enough to be worth not repeating. You should find this not very hard to solve, once I’ve presented it in this format.

For bonus points, what does the compiler say? What does your editor do?

[Update: OK, so the answer is that the initialisation of goat is skipped. Its a statement, but it isn’t behind a case label, so it is ignored. But the second part of the answer is that it is ignored silently, which I’ve got used to the compiler not doing: if you do something stupid which has no effect, it will normally warn you. quokka was the first to get both parts of the answer (those who said “there is a semi-colon missing” were correct, but fell for the “hide the hard answer behind the easy answer” test that I accidentally set. I wouldn’t bother write a post about a missing semi-colon, and anyway the compiler easily spots that and fails to compile it).

There is a third part, which is that not only is the non-functional statement not warned about, but it also fails to warn that “goat” may be used uninitialised, which is very naughty of it indeed. That may be gcc4 brokenness.

Someone else who investigated this more thoroughly than I wrote:

“GCC 4 (4.1.2, 4.4.4 and 4.6.1) won’t warn you about this even with -Wall and -W (and -O which is necessary for some of the analyses to work). For GCC 4.1.2 and 4.4.4 it takes the specific command line option -Wunreachable-code to tell you that the first assignment won’t be executed. However, -Wunreachable-code is not normally recommended. The GCC manual says:

This option is not made part of -Wall because in a debugging version of a program there is often substantial code which checks correct functioning of the program and is, hopefully, unreachable because the program does work. Another common use of unreachable code is to provide behavior which is selectable at compile-time.

GCC 4.6.1 doesn’t provide -Wunreachable-code but the new option -Wjump-misses-init finds the problem (this is not part of -Wall or -Wextra).”

Isn’t C interesting?]

Not the post you were looking for

A small excerpt from the true horror at The C Programming Language Brian W Kernighan & Dennis M Ritchie & HP Lovecraft:

Exercise 4-13. Write a function reverse(s) which reverses the string s by turning the mind inside out, converting madness into reality and opening the door to allow the Old Ones to creep forth once more from their sunken crypt beyond time.

(bonus points for spotting the error in Cthulhu).

You can’t parse [X]HTML with regex is good too, apparently sufficiently so to have blog postings devoted to it, not that this is.

Hat tip: Paul.