-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Fix #15000 Propagate integral cast ranges to condition analysis #8819
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jfdeverge
wants to merge
1
commit into
cppcheck-opensource:main
Choose a base branch
from
jfdeverge:dev/fix_misra_14_3
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+120
−5
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6546,6 +6546,79 @@ class TestCondition : public TestFixture { | |
| "[test.cpp:4:13]: (style) Comparing expression of type 'const unsigned int &' against value 4294967295. Condition is always false. [compareValueOutOfTypeRangeError]\n", | ||
| errout_str()); | ||
|
|
||
| check("typedef unsigned int uint32;\n" | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's no need for those typedefs, please use e.g. |
||
| "typedef unsigned long long uint64;\n" | ||
| "typedef long long sint64;\n" | ||
| "void f(uint32 x) {\n" | ||
| " uint64 tmp = ((uint64)x) + 1ULL;\n" | ||
| " if (tmp > 4294967295ULL)\n" | ||
| " tmp = 4294967295ULL;\n" | ||
| " if ((((sint64)((uint32)tmp)) - 1LL) < 0LL) {}\n" | ||
| " if ((((sint64)((uint32)tmp)) - 1LL) > 4294967295LL) {}\n" | ||
| "}\n", settingsUnix64); | ||
| ASSERT_EQUALS("[test.cpp:9:41]: (style) Condition '(((long long)((unsigned int)tmp))-1LL)>4294967295LL' is always false [knownConditionTrueFalse]\n", | ||
| errout_str()); | ||
|
|
||
| check("typedef unsigned int uint32;\n" | ||
| "typedef unsigned long long uint64;\n" | ||
| "typedef long long sint64;\n" | ||
| "void f(uint64 tmp) {\n" | ||
| " if (tmp > 4294967295ULL)\n" | ||
| " tmp = 4294967295ULL;\n" | ||
| " if (static_cast<sint64>(static_cast<uint32>(tmp)) - 1LL > 4294967295LL) {}\n" | ||
| "}\n", settingsUnix64); | ||
| ASSERT_EQUALS("[test.cpp:7:61]: (style) Condition 'static_cast<long long>(static_cast<unsigned int>(tmp))-1LL>4294967295LL' is always false [knownConditionTrueFalse]\n", | ||
| errout_str()); | ||
|
|
||
| // cast directly around variable: both the range-based and the declared-type | ||
| // analysis can prove the condition invariant; diag() must prevent duplicates | ||
| check("void f(unsigned char c) {\n" | ||
| " if ((unsigned char)c > 255) {}\n" | ||
| "}\n", settingsUnix64); | ||
| ASSERT_EQUALS("[test.cpp:2:28]: (style) Comparing expression of type 'unsigned char' against value 255. Condition is always false. [compareValueOutOfTypeRangeError]\n", | ||
| errout_str()); | ||
|
|
||
| check("void f(unsigned char c) {\n" | ||
| " if ((unsigned char)c == 256) {}\n" | ||
| "}\n", settingsUnix64); | ||
| ASSERT_EQUALS("[test.cpp:2:29]: (style) Comparing expression of type 'unsigned char' against value 256. Condition is always false. [compareValueOutOfTypeRangeError]\n", | ||
| errout_str()); | ||
|
|
||
| check("void f(unsigned int u) {\n" | ||
| " if ((unsigned int)u > 4294967295ULL) {}\n" | ||
| "}\n", settingsUnix64); | ||
| ASSERT_EQUALS("[test.cpp:2:27]: (style) Comparing expression of type 'unsigned int' against value 4294967295. Condition is always false. [compareValueOutOfTypeRangeError]\n", | ||
| errout_str()); | ||
|
|
||
| check("void f(unsigned short s) {\n" | ||
| " if ((unsigned int)s > 4294967295ULL) {}\n" | ||
| "}\n", settingsUnix64); | ||
| ASSERT_EQUALS("[test.cpp:2:27]: (style) Comparing expression of type 'unsigned int' against value 4294967295. Condition is always false. [compareValueOutOfTypeRangeError]\n", | ||
| errout_str()); | ||
|
|
||
| // wchar_t range is derived through ValueType::getSizeOf() | ||
| check("void f(wchar_t c) {\n" | ||
| " if ((wchar_t)c > 0x7fffffff) {}\n" | ||
| "}\n", settingsUnix64); | ||
| ASSERT_EQUALS("[test.cpp:2:20]: (style) Condition '(wchar_t)c>0x7fffffff' is always false [knownConditionTrueFalse]\n", | ||
| errout_str()); | ||
|
|
||
| check("void f(unsigned int x) {\n" | ||
| " if (-(signed char)x < -129) {}\n" | ||
| "}\n", settingsUnix64); | ||
| ASSERT_EQUALS("[test.cpp:2:25]: (style) Condition '-(char)x<-129' is always false [knownConditionTrueFalse]\n", | ||
| errout_str()); | ||
|
|
||
| check("void f(int x) {\n" | ||
| " if ((x) > 0) {}\n" | ||
| "}\n", settingsUnix64); | ||
| ASSERT_EQUALS("", errout_str()); | ||
|
|
||
| check("void f(unsigned int x) {\n" | ||
| " if ((unsigned int)x > 0) {}\n" | ||
| "}\n", settingsUnix64); | ||
| ASSERT_EQUALS("", errout_str()); | ||
|
|
||
| check("void f() {\n" | ||
| " long long ll = 1024 * 1024 * 1024;\n" | ||
| " if (ll * 8 < INT_MAX) {}\n" | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand how signed 64 bit and unsigned 64 bit have the same max value. Maybe for unsigned you meant ULLONG_MAX?