Bug 2240705 - False positive knownConditionTrueFalse with cppcheck 2.12.0
Summary: False positive knownConditionTrueFalse with cppcheck 2.12.0
Keywords:
Status: NEW
Alias: None
Product: Fedora
Classification: Fedora
Component: cppcheck
Version: 38
Hardware: Unspecified
OS: Linux
unspecified
medium
Target Milestone: ---
Assignee: Susi Lehtola
QA Contact: Fedora Extras Quality Assurance
URL:
Whiteboard:
Depends On:
Blocks:
TreeView+ depends on / blocked
 
Reported: 2023-09-26 01:06 UTC by David Gibson
Modified: 2023-09-29 05:35 UTC (History)
6 users (show)

Fixed In Version:
Doc Type: If docs needed, set a value
Doc Text:
Clone Of:
Environment:
Last Closed:
Type: ---
Embargoed:


Attachments (Terms of Use)

Description David Gibson 2023-09-26 01:06:32 UTC
New cppcheck version has introduced a false positive knownConditionTrueFalse case where it incorrectly deduces that a MIN() operation is clamping a value to 0.

Problem seems to be specific to unsigned variables.

Reproducible: Always

Steps to Reproduce:
1. Create this C file, example.c

#ifndef MIN
#define MIN(x, y)		(((x) < (y)) ? (x) : (y))
#endif

void example(unsigned int old_val)
{
	unsigned int new_val = MIN(old_val, 65535);

	if (!new_val) {
	}
}

2. Run
    $ cppcheck --enable=all example.c

Actual Results:  
$ cppcheck --enable=all example.c 
Checking example.c ...
example.c:9:6: style: Condition '!new_val' is always true [knownConditionTrueFalse]
 if (!new_val) {
     ^
example.c:7:25: note: Assignment 'new_val=((old_val)<65535)?old_val:65535', assigned value is less than 1
 unsigned int new_val = MIN(old_val, 65535);
                        ^
example.c:9:6: note: Condition '!new_val' is always true
 if (!new_val) {
     ^
example.c:5:0: style: The function 'example' is never used. [unusedFunction]
void example(unsigned int old_val)
^
Active checkers: 106/565


Expected Results:  
Only the unusedFunction warning.  The knownConditionTrueFalse warning is clearly bogus.

Occurs with both the current Fedora 38 released cppcheck:
    $ cppcheck --version
    Cppcheck 2.12.0
    $ rpm -q cppcheck
    cppcheck-2.12.0-2.fc38.x86_64

Also occurs with the update in testing:
    $ cppcheck --version
    Cppcheck 2.12.1
    $ rpm -q cppcheck
    cppcheck-2.12.1-1.fc38.x86_64

Problem does *not* occur with the version in Fedora 38 base (not updates):
    $ cppcheck --version
    Cppcheck 2.9
    $ rpm -q cppcheck
    cppcheck-2.9-4.fc38.x86_64

Comment 1 David Gibson 2023-09-29 05:35:00 UTC
I also mentioned this on the upstream forums at:

https://sourceforge.net/p/cppcheck/discussion/general/thread/f5b1a00646/

The problem still appears to be there in the latest upstream git.


Note You need to log in before you can comment on or make changes to this bug.