mirror of
https://github.com/github/codeql.git
synced 2026-05-03 12:45:27 +02:00
CPP: Exclude functions containing preprocessor logic.
This commit is contained in:
@@ -10,6 +10,7 @@
|
||||
*/
|
||||
|
||||
import cpp
|
||||
import semmle.code.cpp.commons.Exclusions
|
||||
|
||||
Stmt getNextRealStmt(Block b, int i) {
|
||||
result = b.getStmt(i + 1) and
|
||||
@@ -30,4 +31,6 @@ where b.getStmt(i) = js
|
||||
// the next statement isn't a loop that can be jumped into
|
||||
and not exists (LabelStmt ls | s.(Loop).getStmt().getAChild*() = ls)
|
||||
and not exists (SwitchCase sc | s.(Loop).getStmt().getAChild*() = sc)
|
||||
// no preprocessor logic applies
|
||||
and not functionContainsPreprocCode(js.getEnclosingFunction())
|
||||
select js, "This statement makes $@ unreachable.", s, s.toString()
|
||||
|
||||
@@ -58,3 +58,16 @@ predicate functionContainsDisabledCode(Function f) {
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Holds if the function `f` contains code that could be excluded by the preprocessor.
|
||||
*/
|
||||
predicate functionContainsPreprocCode(Function f) {
|
||||
// `f` contains a preprocessor branch
|
||||
exists(PreprocessorBranchDirective pbd, string file, int pbdStartLine, int fBlockStartLine, int fBlockEndLine |
|
||||
functionLocation(f, file, fBlockStartLine, fBlockEndLine) and
|
||||
pbdLocation(pbd, file, pbdStartLine) and
|
||||
pbdStartLine <= fBlockEndLine and
|
||||
pbdStartLine >= fBlockStartLine
|
||||
)
|
||||
}
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
| test.cpp:2:2:2:12 | goto ... | This statement makes $@ unreachable. | test.cpp:3:2:3:5 | ExprStmt | ExprStmt |
|
||||
| test.cpp:9:3:9:8 | break; | This statement makes $@ unreachable. | test.cpp:10:3:10:6 | ExprStmt | ExprStmt |
|
||||
| test.cpp:37:3:37:8 | break; | This statement makes $@ unreachable. | test.cpp:38:3:38:11 | return ... | return ... |
|
||||
| test.cpp:91:2:91:11 | goto ... | This statement makes $@ unreachable. | test.cpp:93:2:93:5 | ExprStmt | ExprStmt |
|
||||
|
||||
@@ -88,7 +88,7 @@ void test8() {
|
||||
int x = 0;
|
||||
|
||||
#ifdef CONFIG_DEFINE
|
||||
goto skip; // GOOD (the `x++` is still reachable in some configurations) [FALSE POSITIVE]
|
||||
goto skip; // GOOD (the `x++` is still reachable in some configurations)
|
||||
#endif
|
||||
x++;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user