On 2025-02-04 13:38, Bruno Haible wrote:
Paul Eggert wrote:
For situations like these I prefer "default: unreachable ();" to
"default: abort ();", as "unreachable ()" lets the builder decide
whether to abort or optimize; but it's no big deal.

If there's only the slightest chance of that 'default:' case being
reached (possibly through programmer mistake or heap corruption),
I prefer to call abort() rather than unreachable()

Yes if there's a chance the program is buggy, 'abort' is typically better. I use 'unreachable' when there's no chance, which means that calling 'abort' can mislead both the human reader and the compiler. For this particular case I thought there was no chance, but I didn't read the code carefully and if there is a chance then 'abort ()' is better.

In performance-sensitive cases I might also prefer 'unreachable' to 'abort'.

Obviously these are judgment calls.


compile this code with "gcc -m32 -O2 -S":
     -----------------------------------------------------
     void foo (void) { __builtin_unreachable(); }

Compile with -fsanitize=undefined and 'unreachable ()' traps, with both gcc and clang. This is an advantage of 'unreachable' over 'abort'.

Reply via email to