Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #394704
| From | David Brown <david.brown@hesbynett.no> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: bugprone-switch-missing-default-case |
| Date | 2025-10-24 10:09 +0200 |
| Organization | A noiseless patient Spider |
| Message-ID | <10dfc82$2eobo$1@dont-email.me> (permalink) |
| References | (1 earlier) <10da8bo$hker$1@dont-email.me> <10dag3d$jkju$1@dont-email.me> <10danqv$ltmu$1@dont-email.me> <10dat1m$nfjr$2@dont-email.me> <877bwl2o0h.fsf@example.invalid> |
On 24/10/2025 01:31, Keith Thompson wrote:
> David Brown <david.brown@hesbynett.no> writes:
>> On 22/10/2025 15:56, Janis Papanagnou wrote:
> [...]
>>> switch (cmd) {
>>> case 'C': ...;
>>> case 'M': ...;
>>> default: printf ("Error: uncaught cmd '%c'\n", cmd);
>>> }
>>> It's good to take precautions and define the 'default' case. YMMV.
>>
>> That's not "taking precautions". If the "...optionally verify cmd"
>> part does a good job, then the default line is worse than useless
>> because it is code that never runs. If that part doesn't exist (since
>> it is "optional"), then the default line is not "taking precautions",
>> it is normal handling of a realistic situation.
>
> Error handling can be complicated.
>
That's a good entry for the "understatement of the week" competition!
> You're right that code that prints a message for a condition that
> should never happen (equivalently, that can only happen as a result
> of a programming error) is difficult to test. (I suppose you could
> tweak the code to introduce an error so the message is triggered,
> but that's ugly and difficult to automate.)
>
> For example, the failure of the first Ariane 5 launch involved
> an unexpected error diagnostic message being interpreted as data.
> If the error had been quietly ignored, the rocket might have survived.
>
> If a condition is really expected never to happen, something
> like gcc's _builtin_unreachable might be useful, or more portably
> an assert.
>
I like a macro that can then expand to different things according to
what I want to do such as "__builtin_unreachable()" for gcc,
"unreachable()" for C23, blank for other tools, for use with well
checked and well tested code. Alternatively it can expand to a check of
some sort, a log, a debug message, an LED indicator, or whatever during
testing, debugging, or fault-finding.
> If you want to test for a condition that should never happen, you
> need to think about how you would want to handle it. If the best
> way to handle it is to abort the application, that's easy enough.
> If it's a safety critical system, though, it might be better to
> attempt to log an error message (in a way that won't itself cause
> a problem) and try to continue running.
>
Indeed. Very often you see error handling that is done without due
thought about how the errors could occur, and what you should do about
them. I see code that obsessively checks "malloc" for zero returns,
because someone heard that you should always check the result of
"malloc". That's fair enough if you are in a situation where malloc
could fail, and when you can do something about it that is better than
ignoring it - but usually malloc cannot fail in any remotely realistic
use-case, and usually there's nothing you can do anyway that is better
than continuing until dereferencing a null pointer crashes your code.
Basically, there's no point in asking a question until you have a plan
of what to do with the answer.
> And yes, handling a condition that can actually happen (say, in the
> presence of bad input) is quite different.
>
Very much so, yes. "Edge" code - whether it is getting data from
outside, or is an API called by external code - has to sanitize its
inputs. Internal code should not be second-guessing other parts of the
same code - if you can't trust the different bits of the same project to
get things right, you have big problems that can't be solved by adding
default cases. (Where the boundaries between "internal" and "edge" code
go will obviously vary.)
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
bugprone-switch-missing-default-case pozz <pozzugno@gmail.com> - 2025-10-22 10:56 +0200
Re: bugprone-switch-missing-default-case Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2025-10-22 11:32 +0200
Re: bugprone-switch-missing-default-case Richard Harnden <richard.nospam@gmail.invalid> - 2025-10-22 12:44 +0100
Re: bugprone-switch-missing-default-case Thiago Adams <thiago.adams@gmail.com> - 2025-10-22 10:05 -0300
Re: bugprone-switch-missing-default-case Kaz Kylheku <643-408-1753@kylheku.com> - 2025-10-22 18:22 +0000
Re: bugprone-switch-missing-default-case David Brown <david.brown@hesbynett.no> - 2025-10-23 09:12 +0200
Re: bugprone-switch-missing-default-case Thiago Adams <thiago.adams@gmail.com> - 2025-10-23 08:03 -0300
Re: bugprone-switch-missing-default-case David Brown <david.brown@hesbynett.no> - 2025-10-23 17:06 +0200
Re: bugprone-switch-missing-default-case Thiago Adams <thiago.adams@gmail.com> - 2025-10-23 13:15 -0300
Re: bugprone-switch-missing-default-case bart <bc@freeuk.com> - 2025-10-23 17:46 +0100
Re: bugprone-switch-missing-default-case Thiago Adams <thiago.adams@gmail.com> - 2025-10-23 14:12 -0300
Re: bugprone-switch-missing-default-case David Brown <david.brown@hesbynett.no> - 2025-10-23 22:40 +0200
Re: bugprone-switch-missing-default-case scott@slp53.sl.home (Scott Lurndal) - 2025-10-23 20:51 +0000
Re: bugprone-switch-missing-default-case Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2025-10-23 16:21 -0700
Re: bugprone-switch-missing-default-case David Brown <david.brown@hesbynett.no> - 2025-10-24 08:59 +0200
Re: bugprone-switch-missing-default-case scott@slp53.sl.home (Scott Lurndal) - 2025-10-24 15:51 +0000
Re: bugprone-switch-missing-default-case tTh <tth@none.invalid> - 2025-10-23 23:57 +0200
Re: bugprone-switch-missing-default-case David Brown <david.brown@hesbynett.no> - 2025-10-24 09:22 +0200
Re: bugprone-switch-missing-default-case antispam@fricas.org (Waldek Hebisch) - 2025-10-23 23:23 +0000
Re: bugprone-switch-missing-default-case David Brown <david.brown@hesbynett.no> - 2025-10-24 09:53 +0200
Re: bugprone-switch-missing-default-case David Brown <david.brown@hesbynett.no> - 2025-10-22 15:41 +0200
Re: bugprone-switch-missing-default-case Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2025-10-22 16:05 +0200
Re: bugprone-switch-missing-default-case David Brown <david.brown@hesbynett.no> - 2025-10-22 17:23 +0200
Re: bugprone-switch-missing-default-case Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2025-10-23 04:54 +0200
Re: bugprone-switch-missing-default-case David Brown <david.brown@hesbynett.no> - 2025-10-23 09:30 +0200
Re: bugprone-switch-missing-default-case antispam@fricas.org (Waldek Hebisch) - 2025-10-23 22:40 +0000
Re: bugprone-switch-missing-default-case Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2025-10-22 12:41 -0700
Re: bugprone-switch-missing-default-case pozz <pozzugno@gmail.com> - 2025-10-23 08:47 +0200
Re: bugprone-switch-missing-default-case Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2025-10-23 16:17 -0700
Re: bugprone-switch-missing-default-case Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2025-10-23 19:13 -0700
Re: bugprone-switch-missing-default-case David Brown <david.brown@hesbynett.no> - 2025-10-23 10:44 +0200
Re: bugprone-switch-missing-default-case Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2025-10-22 15:56 +0200
Re: bugprone-switch-missing-default-case David Brown <david.brown@hesbynett.no> - 2025-10-22 17:25 +0200
Re: bugprone-switch-missing-default-case Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2025-10-23 04:39 +0200
Re: bugprone-switch-missing-default-case David Brown <david.brown@hesbynett.no> - 2025-10-23 09:08 +0200
Re: bugprone-switch-missing-default-case Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2025-10-23 16:31 -0700
Re: bugprone-switch-missing-default-case David Brown <david.brown@hesbynett.no> - 2025-10-24 10:09 +0200
Re: bugprone-switch-missing-default-case Kaz Kylheku <643-408-1753@kylheku.com> - 2025-10-22 18:13 +0000
csiph-web