Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1d26d4c

Browse files
authored
Merge pull request #2404 from jbj/signed-overflow-macro
C++: Fix SignedOverflowCheck.ql performance
2 parents 4a21123 + eb0b0d1 commit 1d26d4c

2 files changed

Lines changed: 14 additions & 6 deletions

File tree

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import cpp
1515
private import semmle.code.cpp.valuenumbering.GlobalValueNumbering
1616
private import semmle.code.cpp.rangeanalysis.SimpleRangeAnalysis
17+
private import semmle.code.cpp.commons.Exclusions
1718

1819
from RelationalOperation ro, AddExpr add, Expr expr1, Expr expr2
1920
where
@@ -22,7 +23,7 @@ where
2223
ro.getAnOperand() = expr2 and
2324
globalValueNumber(expr1) = globalValueNumber(expr2) and
2425
add.getUnspecifiedType().(IntegralType).isSigned() and
25-
not exists(MacroInvocation mi | mi.getAnAffectedElement() = add) and
26+
not isFromMacroDefinition(ro) and
2627
exprMightOverflowPositively(add) and
2728
exists(Compilation c | c.getAFileCompiled() = ro.getFile() |
2829
not c.getAnArgument() = "-fwrapv" and

cpp/ql/src/semmle/code/cpp/commons/Exclusions.qll

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,18 @@ predicate functionContainsPreprocCode(Function f) {
9494
* ```
9595
*/
9696
predicate isFromMacroDefinition(Element e) {
97-
exists(MacroInvocation mi |
98-
// e is in mi
97+
exists(MacroInvocation mi, Location eLocation, Location miLocation |
9998
mi.getAnExpandedElement() = e and
100-
// and e was apparently not passed in as a macro parameter
101-
e.getLocation().getStartLine() = mi.getLocation().getStartLine() and
102-
e.getLocation().getStartColumn() = mi.getLocation().getStartColumn()
99+
eLocation = e.getLocation() and
100+
miLocation = mi.getLocation() and
101+
// If the location of `e` coincides with the macro invocation, then `e` did
102+
// not come from a macro argument. The inequalities here could also be
103+
// equalities, but that confuses the join orderer into joining on the source
104+
// locations too early.
105+
// There are cases where the start location of a non-argument element comes
106+
// right after the invocation's open parenthesis, so it appears to be more
107+
// robust to match on the end location instead.
108+
eLocation.getEndLine() >= miLocation.getEndLine() and
109+
eLocation.getEndColumn() >= miLocation.getEndColumn()
103110
)
104111
}

0 commit comments

Comments
 (0)