Skip to content

Fix #11389 Do not warn for truncLongCastReturn if operands have known valid int#8192

Merged
danmar merged 4 commits intodanmar:mainfrom
ceJce:fix11389_unnecessary_truncLongCastReturn
Feb 24, 2026
Merged

Fix #11389 Do not warn for truncLongCastReturn if operands have known valid int#8192
danmar merged 4 commits intodanmar:mainfrom
ceJce:fix11389_unnecessary_truncLongCastReturn

Conversation

@ceJce
Copy link
Copy Markdown
Contributor

@ceJce ceJce commented Feb 9, 2026

This fix handles the first example in ticket, i.e. when the value is known to be a valid int:

long f() {
int n = 1;
return n << 12;
}

Copy link
Copy Markdown
Owner

@danmar danmar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks! I would like some test cases. One case where we don't write a false positive.

And verify that we still warn when we want.

@ceJce ceJce force-pushed the fix11389_unnecessary_truncLongCastReturn branch 2 times, most recently from 968f597 to 2c262a0 Compare February 17, 2026 07:55
@ceJce
Copy link
Copy Markdown
Contributor Author

ceJce commented Feb 17, 2026

thanks! I would like some test cases. One case where we don't write a false positive.

And verify that we still warn when we want.

Tests added.

@ceJce ceJce requested a review from danmar February 18, 2026 08:09
@ceJce ceJce force-pushed the fix11389_unnecessary_truncLongCastReturn branch 2 times, most recently from cf1e453 to 88ba04e Compare February 19, 2026 12:49
@ceJce ceJce force-pushed the fix11389_unnecessary_truncLongCastReturn branch from 88ba04e to 452304d Compare February 20, 2026 09:03
@ceJce ceJce force-pushed the fix11389_unnecessary_truncLongCastReturn branch from 452304d to 976db09 Compare February 20, 2026 11:39
@sonarqubecloud
Copy link
Copy Markdown

@danmar danmar merged commit bd33cf5 into danmar:main Feb 24, 2026
68 of 72 checks passed
francois-berder added a commit to francois-berder/cppcheck that referenced this pull request Apr 13, 2026
… condition

When a for loop's condition is impossible given the initial value (e.g.
`for (int i = 0; i > 10; i++)`), cppcheck was not emitting a
knownConditionTrueFalse warning.

The root cause was in valueFlowForLoop2: when the condition evaluated to
false on the first check (against the init state), the function returned
false immediately without populating any output maps, so the caller had
no information to work with.

Fix by populating memory1 and memoryAfter with the init-state when the
condition is immediately false (and !error), and returning true. The
caller detects this NeverRuns case via memory1 == memoryAfter (they
share the same ProgramMemory map pointer due to copy-on-write
semantics), then sets a known(0) value on the condition token.

Signed-off-by: Francois Berder <fberder@outlook.fr>
francois-berder added a commit to francois-berder/cppcheck that referenced this pull request Apr 13, 2026
… condition

When a for loop's condition is impossible given the initial value (e.g.
`for (int i = 0; i > 10; i++)`), cppcheck was not emitting a
knownConditionTrueFalse warning.

The root cause was in valueFlowForLoop2: when the condition evaluated to
false on the first check (against the init state), the function returned
false immediately without populating any output maps, so the caller had
no information to work with.

Fix by populating memory1 and memoryAfter with the init-state when the
condition is immediately false (and !error), and returning true. The
caller detects this NeverRuns case via memory1 == memoryAfter (they
share the same ProgramMemory map pointer due to copy-on-write
semantics), then sets a known(0) value on the condition token.

Signed-off-by: Francois Berder <fberder@outlook.fr>
francois-berder added a commit to francois-berder/cppcheck that referenced this pull request Apr 13, 2026
… condition

When a for loop's condition is impossible given the initial value (e.g.
`for (int i = 0; i > 10; i++)`), cppcheck was not emitting a
knownConditionTrueFalse warning.

Fix by populating memory1, memory2 and memoryAfter with the init state
when the condition is immediately false (and no error occured). We can
then set the value for the condition token.

Signed-off-by: Francois Berder <fberder@outlook.fr>
francois-berder added a commit to francois-berder/cppcheck that referenced this pull request Apr 13, 2026
When a for loop's condition is impossible given the initial value (e.g.
`for (int i = 0; i > 10; i++)`), cppcheck was not emitting a
knownConditionTrueFalse warning.

Fix by populating memory1, memory2 and memoryAfter with the init state
when the condition is immediately false (and no error occured). We can
then set the value for the condition token and thus emit a
knownConditionTrueFalse warning.

Signed-off-by: Francois Berder <fberder@outlook.fr>
francois-berder added a commit to francois-berder/cppcheck that referenced this pull request Apr 13, 2026
When a for loop's condition is impossible given the initial value (e.g.
`for (int i = 0; i > 10; i++)`), cppcheck was not emitting a
knownConditionTrueFalse warning.

Fix by populating memory1, memory2 and memoryAfter with the init state
when the condition is immediately false (and no error occured). We can
then set the value for the condition token and thus emit a
knownConditionTrueFalse warning.

Signed-off-by: Francois Berder <fberder@outlook.fr>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants