|
| 1 | +/** |
| 2 | + * @name Inconsistent direction of for loop |
| 3 | + * @description A for-loop iteration expression goes backward with respect of the initialization statement and condition expression. |
| 4 | + * @kind problem |
| 5 | + * @problem.severity error |
| 6 | + * @precision high |
| 7 | + * @id cpp/inconsistent-loop-direction |
| 8 | + * @tags correctness |
| 9 | + * external/cwe/cwe-835 |
| 10 | + * external/microsoft/6293 |
| 11 | + * @msrc.severity important |
| 12 | + */ |
| 13 | +import cpp |
| 14 | +import semmle.code.cpp.rangeanalysis.SimpleRangeAnalysis |
| 15 | +import semmle.code.cpp.dataflow.DataFlow |
| 16 | + |
| 17 | +predicate illDefinedDecrForStmt( ForStmt forstmt, Variable v, Expr initialCondition, Expr terminalCondition ) { |
| 18 | + v.getAnAssignedValue() = initialCondition |
| 19 | + and |
| 20 | + exists( |
| 21 | + RelationalOperation rel | |
| 22 | + rel = forstmt.getCondition() | |
| 23 | + terminalCondition = rel.getGreaterOperand() |
| 24 | + and v.getAnAccess() = rel.getLesserOperand() |
| 25 | + and |
| 26 | + DataFlow::localFlowStep(DataFlow::exprNode(initialCondition), DataFlow::exprNode(rel.getLesserOperand())) |
| 27 | + ) |
| 28 | + and |
| 29 | + exists( |
| 30 | + DecrementOperation dec | |
| 31 | + dec = forstmt.getUpdate().(DecrementOperation) |
| 32 | + and dec.getAnOperand() = v.getAnAccess() |
| 33 | + ) |
| 34 | + and |
| 35 | + ( |
| 36 | + ( upperBound(initialCondition) < lowerBound(terminalCondition) ) |
| 37 | + or |
| 38 | + ( forstmt.conditionAlwaysFalse() or forstmt.conditionAlwaysTrue() ) |
| 39 | + ) |
| 40 | +} |
| 41 | + |
| 42 | +predicate illDefinedIncrForStmt( ForStmt forstmt, Variable v, Expr initialCondition, Expr terminalCondition ) { |
| 43 | + v.getAnAssignedValue() = initialCondition |
| 44 | + and |
| 45 | + exists( |
| 46 | + RelationalOperation rel | |
| 47 | + rel = forstmt.getCondition() | |
| 48 | + terminalCondition = rel.getLesserOperand() |
| 49 | + and v.getAnAccess() = rel.getGreaterOperand() |
| 50 | + and |
| 51 | + DataFlow::localFlowStep(DataFlow::exprNode(initialCondition), DataFlow::exprNode(rel.getGreaterOperand())) |
| 52 | + ) |
| 53 | + and |
| 54 | + exists( IncrementOperation incr | |
| 55 | + incr = forstmt.getUpdate().(IncrementOperation) |
| 56 | + and |
| 57 | + incr.getAnOperand() = v.getAnAccess() |
| 58 | + ) |
| 59 | + and |
| 60 | + ( |
| 61 | + ( upperBound(terminalCondition) < lowerBound(initialCondition)) |
| 62 | + or |
| 63 | + ( forstmt.conditionAlwaysFalse() or forstmt.conditionAlwaysTrue()) |
| 64 | + ) |
| 65 | +} |
| 66 | + |
| 67 | +predicate illDefinedForStmtWrongDirection( ForStmt forstmt, Variable v, Expr initialCondition, Expr terminalCondition |
| 68 | + , boolean isIncr ) { |
| 69 | + ( illDefinedDecrForStmt( forstmt, v, initialCondition, terminalCondition) and isIncr = false ) |
| 70 | + or |
| 71 | + ( illDefinedIncrForStmt( forstmt, v, initialCondition, terminalCondition) and isIncr = true) |
| 72 | +} |
| 73 | + |
| 74 | +bindingset[b] |
| 75 | +private string forLoopdirection(boolean b){ |
| 76 | + if( b = true ) then result = "upward" |
| 77 | + else result = "downward" |
| 78 | +} |
| 79 | + |
| 80 | +bindingset[b] |
| 81 | +private string forLoopTerminalConditionRelationship(boolean b){ |
| 82 | + if( b = true ) then result = "lower" |
| 83 | + else result = "higher" |
| 84 | +} |
| 85 | + |
| 86 | + predicate illDefinedForStmt( ForStmt for, string message ) { |
| 87 | + exists( |
| 88 | + boolean isIncr, |
| 89 | + Variable v, |
| 90 | + Expr initialCondition, |
| 91 | + Expr terminalCondition | |
| 92 | + illDefinedForStmtWrongDirection(for, v, initialCondition, terminalCondition, isIncr) |
| 93 | + and |
| 94 | + if( for.conditionAlwaysFalse() ) then |
| 95 | + ( |
| 96 | + message = "Ill-defined for-loop: a loop using variable \"" + v + "\" counts " |
| 97 | + + forLoopdirection(isIncr) + " from a value ("+ initialCondition +"), but the terminal condition is always false." |
| 98 | + ) |
| 99 | + else if |
| 100 | + ( |
| 101 | + for.conditionAlwaysTrue() ) then ( |
| 102 | + message = "Ill-defined for-loop: a loop using variable \"" + v + "\" counts " |
| 103 | + + forLoopdirection(isIncr) + " from a value ("+ initialCondition +"), but the terminal condition is always true." |
| 104 | + ) |
| 105 | + else |
| 106 | + ( |
| 107 | + message = "Ill-defined for-loop: a loop using variable \"" + v + "\" counts " |
| 108 | + + forLoopdirection(isIncr) + " from a value ("+ initialCondition +"), but the terminal condition is " |
| 109 | + + forLoopTerminalConditionRelationship(isIncr) + " (" + terminalCondition + ")." |
| 110 | + ) |
| 111 | + ) |
| 112 | +} |
| 113 | + |
| 114 | +from ForStmt forstmt, string message |
| 115 | + where illDefinedForStmt(forstmt, message) |
| 116 | +select forstmt, message |
0 commit comments