Skip to content

Commit cc28d04

Browse files
authored
Merge pull request #405 from geoffw0/selfcompare
CPP: Fix false positives in PointlessSelfComparison.ql
2 parents 26a248b + 33130b9 commit cc28d04

File tree

4 files changed

+37
-0
lines changed

4 files changed

+37
-0
lines changed

change-notes/1.19/analysis-cpp.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
| Missing return statement (`cpp/missing-return`) | Visible by default | The precision of this query has been increased from 'medium' to 'high', which makes it visible by default in LGTM. It was 'medium' in release 1.17 and 1.18 because it had false positives due to an extractor bug that was fixed in 1.18. |
2222
| Missing return statement | Fewer false positive results | The query is now produces correct results when a function returns a template-dependent type. |
2323
| Call to memory access function may overflow buffer | More correct results | Array indexing with a negative index is now detected by this query. |
24+
| Self comparison | Fewer false positive results | Code inside macro invocations is now excluded from the query. |
2425
| Suspicious call to memset | Fewer false positive results | Types involving decltype are now correctly compared. |
2526
| Suspicious add with sizeof | Fewer false positive results | Arithmetic with void pointers (where allowed) is now excluded from this query. |
2627
| Wrong type of arguments to formatting function | Fewer false positive results | False positive results involving typedefs have been removed. Expected argument types are determined more accurately, especially for wide string and pointer types. Custom (non-standard) formatting functions are also identified more accurately. |

cpp/ql/src/Likely Bugs/Arithmetic/PointlessSelfComparison.ql

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,12 @@ from ComparisonOperation cmp
1818
where pointlessSelfComparison(cmp)
1919
and not nanTest(cmp)
2020
and not overflowTest(cmp)
21+
and not exists(MacroInvocation mi |
22+
// cmp is in mi
23+
mi.getAnExpandedElement() = cmp and
24+
25+
// and cmp was apparently not passed in as a macro parameter
26+
cmp.getLocation().getStartLine() = mi.getLocation().getStartLine() and
27+
cmp.getLocation().getStartColumn() = mi.getLocation().getStartColumn()
28+
)
2129
select cmp, "Self comparison."

cpp/ql/test/query-tests/Likely Bugs/Arithmetic/BadAdditionOverflowCheck/PointlessSelfComparison.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
| test.cpp:83:10:83:15 | ... == ... | Self comparison. |
44
| test.cpp:90:10:90:15 | ... == ... | Self comparison. |
55
| test.cpp:118:7:118:32 | ... != ... | Self comparison. |
6+
| test.cpp:151:11:151:16 | ... == ... | Self comparison. |

cpp/ql/test/query-tests/Likely Bugs/Arithmetic/BadAdditionOverflowCheck/test.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,3 +123,30 @@ int isSmallEnough(unsigned long long x) {
123123
// get compiled away on others.
124124
return x == (size_t)x && x == (u64)x; // GOOD
125125
}
126+
127+
#define markRange(str, x, y) \
128+
if ((x) == (y)) { \
129+
str[x] = '^'; \
130+
} else { \
131+
int i; \
132+
str[x] = '<'; \
133+
for (i = x + 1; i < y; i++) { \
134+
str[i] = '-'; \
135+
} \
136+
str[y] = '>'; \
137+
}
138+
139+
void useMarkRange(int offs) {
140+
char buffer[100];
141+
142+
markRange(buffer, 10, 20);
143+
markRange(buffer, 30, 30);
144+
markRange(buffer, offs, offs + 10);
145+
markRange(buffer, offs, offs); // GOOD (comparison is in the macro)
146+
}
147+
148+
#define MY_MACRO(x) (x)
149+
150+
void myMacroTest(int x) {
151+
MY_MACRO(x == x); // BAD
152+
}

0 commit comments

Comments
 (0)