Click on image to see enlargment

PC-lint/FlexeLint Output | Reference Manual Explanation | Home

            bug646.cpp

1    #include <stdio.h>
2    #include <ctype.h>
3    int arithmetic( const char *s )
4        {
5        int n = 0;
6        unsigned char ch;
7        switch( *s )
8            {
9        case '+':
10           while( (ch = *++s) )
11               {
12               if( !isdigit(ch) ) break;
13               n = n * 10 + (ch - '0');
14               if( !*s ) default:;
15               }
16           break;
17           }
18       return n;
19       }
20   int main()
21        { printf( "%d\n", arithmetic("-35") ); }

It's back to school time and the first grade teacher has initiated a program that will do arithmetic operations automatically. But to his amazement, "-35" is being converted to positive 35 and his compiler gave no warning. What's going on?


bug646.cpp lint Output

--- Module:   bug646.cpp (C++)
                         _
        while( (ch = *++s) )
bug646.cpp(10) : Info 732: Loss of sign (assignment) (char to unsigned char)
bug646.cpp(10) : Info 820: Boolean test of a parenthesized assignment
                      _
            if( !*s ) default:;
bug646.cpp(14) : Warning 616: control flows into case/default
bug646.cpp(14) : Info 825: control flows into case/default without -fallthrough comment 
bug646.cpp(14) : Warning 646: case/default within while loop; may have been misplaced

Reference Manual Explanation


646    case/default within Kind loop; may have been misplaced  -- A case or default
       statement was found within a for, do, or while loop. Was this intentional?
       At the very least, this reflects poor programming style.

Commentary

This is definitely not one of those bugs that a programmer will typically run into, but it is an example of how an outlandish misplacement of a keyword (default) is accepted without a whimper as perfectly normal by most compilers.



If you have comments or questions about this bug, please post them to our   Discussion Forum


Previous Bug - Bug #649 - August 2007

[ new ] Use our Interactive Demo to Run FlexeLint on our Bugs of the Month

PC-lint/FlexeLint - Product Overview

Home | Contact | Order

PC-lint and FlexeLint are trademarks of Gimpel Software
Copyright 2007, Gimpel Software, All rights reserved.