Menu

I was very puzzled that CppCheck could not detect such a simple problem.

Alex
2023-03-03
2023-03-06
  • Alex

    Alex - 2023-03-03
    I was very puzzled that CppCheck could not detect such a simple problem
    
    (1)In this case, CppCheck cannot detect the problem.
    
    • typeDef.h:
      typedef unsigned char BYTE;

      user.c:
      BYTE dwCount=0;

      dwCount++;
      if(dwCount>5000)//The sentence in question.
      {
      break;
      }

      (2)In this case, the problem can be detected.

    • user.c:
      unsigned char dwCount=0;

      dwCount++;
      if(dwCount>5000)//The sentence in question.
      {
      break;
      }

      What might be the cause of this? I have included all the source files and header files in CppCheck project.
      Am I using it the wrong way?
      Thank you for your support.

     

    Last edit: Alex 2023-03-03
  • Daniel Marjamäki

    I think your description is too incomplete so it's not possible to know what the problem is. There is no #include. And a if should be in a function somewhere, it's not clear if you have a function.

    What I get:

    daniel@laptop:~$ cppcheck/cppcheck --enable=style user.c
    Checking user.c ...
    user.c:7:15: style: Condition 'dwCount>5000' is always false [knownConditionTrueFalse]
        if(dwCount>5000)//The sentence in question.
                  ^
    user.c:5:18: note: Assignment 'dwCount=0', assigned value is 0
        BYTE dwCount=0;
                     ^
    user.c:6:5: note: dwCount is incremented', new value is 1
        dwCount++;
        ^
    user.c:7:15: note: Condition 'dwCount>5000' is always false
        if(dwCount>5000)//The sentence in question.
                  ^
    daniel@laptop:~$ cat typedef.h 
    typedef unsigned char BYTE;
    daniel@laptop:~$ cat user.c 
    
    #include "typedef.h"
    
    void foo() {
        BYTE dwCount=0;
        dwCount++;
        if(dwCount>5000)//The sentence in question.
        {
            break;
        }
    }
    
     

    Last edit: Daniel Marjamäki 2023-03-03
  • Daniel Marjamäki

    I have included all the source files and header files in CppCheck project.

    Do you mean that header files are included with -I and #include? If you check the header file that is the wrong approach.

     
  • Alex

    Alex - 2023-03-06

    @Daniel Marjamäki

    Thanks Daniel!

    According to your suggestion, I used CppCheck to check the code again, and this problem can indeed be detected!
    The key reason is that I didn't include the path of the header file. This problem will be detected if the path of the header file is included correctly.

    But I have an immature suggestion that "dwCount>5000" is a very serious error, which can easily lead to software function abnormalities. Can we improve its security level, such as ERROR or WARNING (instead of the current STYLE)?

     
  • Daniel Marjamäki

    I agree "style" does not sound right.. it is not just coding style that we warn about. It sounds like we might warn about spaces/tabs/line lengths/etc..

    the rules for the severities are:

    there is undefined behavior when code is executed => error
    there might be undefined behavior when code is executed => warning
    code "looks wrong/unintentional" in some way => style

    for this checker there is no undefined behavior.

    the purpose of most "style" checkers is to detect bugs. redundant assignments. redundant/unreachable code. inconsistent conditions...

     
  • Alex

    Alex - 2023-03-06

    Thanks Daniel!

    Ok, understood!
    I've used SonarQube before, and its definition of "style" is just about spaces/tabs/line lengths/etc...
    I realized I need to update understanding of what "style" means on CppCheck.

    By the way, can you help with another puzzle?
    Is there any guidance on how to make the CppCheck GUI support MISRA C,
    As described in the following link:
    https://sourceforge.net/p/cppcheck/discussion/general/thread/cde47d58f5/

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.