home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.lang.c      Meh, in C you gotta define EVERYTHING      243,242 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 241,421 of 243,242   
   David Brown to Keith Thompson   
   Re: bugprone-switch-missing-default-case   
   23 Oct 25 10:44:46   
   
   From: david.brown@hesbynett.no   
      
   On 22/10/2025 21:41, Keith Thompson wrote:   
   > David Brown  writes:   
   >> 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.   
   >   
   > No, pozz meant "without any defined behavior", which is a direct quote   
   > from the cited document.   
   >   
      
   OK.   
      
   > That document is poorly written.  The phrase "without any defined   
   > behavior" strongly implies that the behavior is undefined, which is   
   > simply wrong.   
      
   I guess I owe Pozz an apology for thinking he had made a little typo,   
   when in fact it was a clear error from the clang-tidy folk!  Thanks for   
   clearing that up.   
      
   >   
   > It says:   
   >   
   >      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.   
   >   
   > It would be more accurate to say:   
   >   
   >      When a switch statement lacks a default case, if a value is   
   >      encountered that does not match any of the specified cases, the   
   >      switch statement will do nothing and the program will continue   
   >      execution without handling the value.   
   >   
   > A warning might be warranted, but the behavior is well defined.   
   >   
   > Note that the documentation is for the add-on tool clang-tidy, not for   
   > the clang compiler.   
   >   
   > I've submitted a bug report :   
   >   
   > https://github.com/llvm/llvm-project/issues/164699   
   >   
      
   It looks like your patch has been accepted, as far as I could tell.   
   Very helpful!   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca