Merge pull request #405 from geoffw0/selfcompare

CPP: Fix false positives in PointlessSelfComparison.ql
This commit is contained in:
Jonas Jensen
2018-11-20 09:25:10 +01:00
committed by GitHub
4 changed files with 37 additions and 0 deletions

View File

@@ -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."

View File

@@ -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. |

View File

@@ -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
}