Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #394655

Re: bugprone-switch-missing-default-case

From David Brown <david.brown@hesbynett.no>
Newsgroups comp.lang.c
Subject Re: bugprone-switch-missing-default-case
Date 2025-10-22 17:23 +0200
Organization A noiseless patient Spider
Message-ID <10dasso$nfjr$1@dont-email.me> (permalink)
References <10da67g$3q59f$1@dont-email.me> <10da8bo$hker$1@dont-email.me> <10dag3d$jkju$1@dont-email.me> <10damv3$ku01$1@dont-email.me> <10daob9$m3em$1@dont-email.me>

Show all headers | View raw


On 22/10/2025 16:05, Janis Papanagnou wrote:
> On 22.10.2025 15:41, David Brown wrote:
>> On 22/10/2025 13:44, Richard Harnden wrote:
>>> On 22/10/2025 10:32, Janis Papanagnou wrote:
>>>> On 22.10.2025 10:56, pozz wrote:
>>>>>
>>>>>> Switch statements without a default case can lead to unexpected
>>>>>> behavior and incomplete handling of all possible cases. When a switch
>>>>>> statement lacks a default case, if a value is encountered that does
>>>>>> not match any of the specified cases, the program will continue
>>>>>> execution without any defined behavior or handling.
>>>>>
>>>>> Maybe I misunderstood that sentence caused by my bad English. I knew
>>>>> that in case the switch value is not present in any case inside the
>>>>> switch, the program continues without doing anything (in the switch)
>>>>> and
>>>>> without any problem.
>>>>>
>>>>> int x = 3;
>>>>> switch(x) {
>>>>>     case 1: printf("Hello");break;
>>>>>     case 2: printf("World");break;
>>>>> }
>>>>>
>>>>> Will the program execution continue without any defined behaviour?
>>
>> Presumably you meant "without any undefined behaviour" ?  The code is
>> fine - if no cases match and there is no default case, execution
>> continues from the end of the switch statement.  Like most warnings,
>> this is about a possible bug in the code - not a definite one.
>>
>>>>
>>>> Your program fragment is well defined.
>>>>
>>>> What the poster certainly tried to express was that in case you
>>>> haven't implemented a complete list of all possible cases and
>>>> also not provided a 'default' to catch all non-specified cases,
>>>> then you might get in troubles with your program, probably by
>>>> possible oversights, future extensions, new data, and whatnot.
>>>>
>>>> Personally I have the habit to always define a default branch,
>>>> and even if that default is impossible to reach you'll find an
>>>> error message (like "internal error with unexpected value...")
>>>> generated at that place.
>>
>> I don't think it is normally appropriate to add a default case unless
>> you actually need it
> 
> Yes I was saying that I want it; I "need" it once errors slip in and
> such a message or error log immediately clears the issue! (You might
> be excluding some "needs" from your repertoire of necessities, okay.)
> 
>> - code that exists but can never be reached is
>> untestable and can be confusing to people reading the code.  But
>> sometimes it can be useful to add a "default : printf("Internal
>> error...");" for debugging, however.
> 
> This printf error message or log entry is what I suggested. It isn't
> confusing because it even _documents_ what's the case here. Rather,
> a missing default leaves the reader with an unnecessary uncertainty.
> YMMV.
> 

Indeed YMMV.  But when I see a printf call that says something has gone 
horribly wrong, I wonder /how/ that could happen.  I don't put such 
statements into my code without it being a possible execution path.  I 
agree, of course, that a good error message here makes the code somewhat 
self-documenting in terms of what it does.  But it does nothing to help 
say how it happened to run.  A printf call that never runs is not free - 
it costs in many different ways, and should not be there unless it is 
worth those costs.

Back to comp.lang.c | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


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