1111 * external/cwe/cwe-561
1212 */
1313import cpp
14+ private import semmle.code.cpp.commons.Exclusions
1415
1516class PureExprInVoidContext extends ExprInVoidContext {
1617 PureExprInVoidContext ( ) { this .isPure ( ) }
@@ -23,71 +24,29 @@ predicate accessInInitOfForStmt(Expr e) {
2324 s .getExpr ( ) = e )
2425}
2526
26- /**
27- * Holds if the preprocessor branch `pbd` is on line `pbdStartLine` in file `file`.
28- */
29- predicate pbdLocation ( PreprocessorBranchDirective pbd , string file , int pbdStartLine ) {
30- pbd .getLocation ( ) .hasLocationInfo ( file , pbdStartLine , _, _, _)
31- }
32-
33- /**
34- * Holds if the body of the function `f` is on lines `fBlockStartLine` to `fBlockEndLine` in file `file`.
35- */
36- predicate functionLocation ( Function f , string file , int fBlockStartLine , int fBlockEndLine ) {
37- f .getBlock ( ) .getLocation ( ) .hasLocationInfo ( file , fBlockStartLine , _, fBlockEndLine , _)
38- }
3927/**
4028 * Holds if the function `f`, or a function called by it, contains
4129 * code excluded by the preprocessor.
4230 */
43- predicate containsDisabledCode ( Function f ) {
44- // `f` contains a preprocessor branch that was not taken
45- exists ( PreprocessorBranchDirective pbd , string file , int pbdStartLine , int fBlockStartLine , int fBlockEndLine |
46- functionLocation ( f , file , fBlockStartLine , fBlockEndLine ) and
47- pbdLocation ( pbd , file , pbdStartLine ) and
48- pbdStartLine <= fBlockEndLine and
49- pbdStartLine >= fBlockStartLine and
50- (
51- pbd .( PreprocessorBranch ) .wasNotTaken ( ) or
52-
53- // an else either was not taken, or it's corresponding branch
54- // was not taken.
55- pbd instanceof PreprocessorElse
56- )
57- ) or
58-
31+ predicate functionContainsDisabledCodeRecursive ( Function f ) {
32+ functionContainsDisabledCode ( f ) or
5933 // recurse into function calls
6034 exists ( FunctionCall fc |
6135 fc .getEnclosingFunction ( ) = f and
62- containsDisabledCode ( fc .getTarget ( ) )
36+ functionContainsDisabledCodeRecursive ( fc .getTarget ( ) )
6337 )
6438}
6539
66-
6740/**
6841 * Holds if the function `f`, or a function called by it, is inside a
6942 * preprocessor branch that may have code in another arm
7043 */
71- predicate definedInIfDef ( Function f ) {
72- exists ( PreprocessorBranchDirective pbd , string file , int pbdStartLine , int pbdEndLine , int fBlockStartLine , int fBlockEndLine |
73- functionLocation ( f , file , fBlockStartLine , fBlockEndLine ) and
74- pbdLocation ( pbd , file , pbdStartLine ) and
75- pbdLocation ( pbd .getNext ( ) , file , pbdEndLine ) and
76- pbdStartLine <= fBlockStartLine and
77- pbdEndLine >= fBlockEndLine and
78- // pbd is a preprocessor branch where multiple branches exist
79- (
80- pbd .getNext ( ) instanceof PreprocessorElse or
81- pbd instanceof PreprocessorElse or
82- pbd .getNext ( ) instanceof PreprocessorElif or
83- pbd instanceof PreprocessorElif
84- )
85- ) or
86-
44+ predicate functionDefinedInIfDefRecursive ( Function f ) {
45+ functionDefinedInIfDef ( f ) or
8746 // recurse into function calls
8847 exists ( FunctionCall fc |
8948 fc .getEnclosingFunction ( ) = f and
90- definedInIfDef ( fc .getTarget ( ) )
49+ functionDefinedInIfDefRecursive ( fc .getTarget ( ) )
9150 )
9251}
9352
@@ -121,8 +80,8 @@ where // EQExprs are covered by CompareWhereAssignMeant.ql
12180 not parent instanceof PureExprInVoidContext and
12281 not peivc .getEnclosingFunction ( ) .isCompilerGenerated ( ) and
12382 not peivc .getType ( ) instanceof UnknownType and
124- not containsDisabledCode ( peivc .( FunctionCall ) .getTarget ( ) ) and
125- not definedInIfDef ( peivc .( FunctionCall ) .getTarget ( ) ) and
83+ not functionContainsDisabledCodeRecursive ( peivc .( FunctionCall ) .getTarget ( ) ) and
84+ not functionDefinedInIfDefRecursive ( peivc .( FunctionCall ) .getTarget ( ) ) and
12685 if peivc instanceof FunctionCall then
12786 exists ( Function target |
12887 target = peivc .( FunctionCall ) .getTarget ( ) and
0 commit comments