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
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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=styleuser.cCheckinguser.c...user.c:7:15:style:Condition'dwCount>5000'isalwaysfalse[knownConditionTrueFalse]if(dwCount>5000)//Thesentenceinquestion.^user.c:5:18:note:Assignment'dwCount=0',assignedvalueis0BYTEdwCount=0;^user.c:6:5:note:dwCountisincremented', new value is 1 dwCount++; ^user.c:7:15: note: Condition 'dwCount>5000'isalwaysfalseif(dwCount>5000)//Thesentenceinquestion.^daniel@laptop:~$cattypedef.htypedefunsignedcharBYTE;daniel@laptop:~$catuser.c#include"typedef.h"voidfoo(){BYTEdwCount=0;dwCount++;if(dwCount>5000)//Thesentenceinquestion.{break;}}
Last edit: Daniel Marjamäki 2023-03-03
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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)?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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...
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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.
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
I think your description is too incomplete so it's not possible to know what the problem is. There is no
#include
. And aif
should be in a function somewhere, it's not clear if you have a function.What I get:
Last edit: Daniel Marjamäki 2023-03-03
Do you mean that header files are included with
-I
and#include
? If you check the header file that is the wrong approach.@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)?
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...
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/