mirror of
https://github.com/github/codeql.git
synced 2025-12-17 01:03:14 +01:00
Merge pull request #405 from geoffw0/selfcompare
CPP: Fix false positives in PointlessSelfComparison.ql
This commit is contained in:
@@ -18,4 +18,12 @@ from ComparisonOperation cmp
|
||||
where pointlessSelfComparison(cmp)
|
||||
and not nanTest(cmp)
|
||||
and not overflowTest(cmp)
|
||||
and not exists(MacroInvocation mi |
|
||||
// cmp is in mi
|
||||
mi.getAnExpandedElement() = cmp and
|
||||
|
||||
// and cmp was apparently not passed in as a macro parameter
|
||||
cmp.getLocation().getStartLine() = mi.getLocation().getStartLine() and
|
||||
cmp.getLocation().getStartColumn() = mi.getLocation().getStartColumn()
|
||||
)
|
||||
select cmp, "Self comparison."
|
||||
|
||||
@@ -3,3 +3,4 @@
|
||||
| test.cpp:83:10:83:15 | ... == ... | Self comparison. |
|
||||
| test.cpp:90:10:90:15 | ... == ... | Self comparison. |
|
||||
| test.cpp:118:7:118:32 | ... != ... | Self comparison. |
|
||||
| test.cpp:151:11:151:16 | ... == ... | Self comparison. |
|
||||
|
||||
@@ -123,3 +123,30 @@ int isSmallEnough(unsigned long long x) {
|
||||
// get compiled away on others.
|
||||
return x == (size_t)x && x == (u64)x; // GOOD
|
||||
}
|
||||
|
||||
#define markRange(str, x, y) \
|
||||
if ((x) == (y)) { \
|
||||
str[x] = '^'; \
|
||||
} else { \
|
||||
int i; \
|
||||
str[x] = '<'; \
|
||||
for (i = x + 1; i < y; i++) { \
|
||||
str[i] = '-'; \
|
||||
} \
|
||||
str[y] = '>'; \
|
||||
}
|
||||
|
||||
void useMarkRange(int offs) {
|
||||
char buffer[100];
|
||||
|
||||
markRange(buffer, 10, 20);
|
||||
markRange(buffer, 30, 30);
|
||||
markRange(buffer, offs, offs + 10);
|
||||
markRange(buffer, offs, offs); // GOOD (comparison is in the macro)
|
||||
}
|
||||
|
||||
#define MY_MACRO(x) (x)
|
||||
|
||||
void myMacroTest(int x) {
|
||||
MY_MACRO(x == x); // BAD
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user