|
| 1 | +/** |
| 2 | + * @id cpp/misra/unparenthesized-macro-argument |
| 3 | + * @name RULE-19-3-4: Parentheses shall be used to ensure macro arguments are expanded appropriately |
| 4 | + * @description Expanded macro arguments shall be enclosed in parentheses to ensure the resulting |
| 5 | + * expressions have the expected precedence and order of operations. |
| 6 | + * @kind problem |
| 7 | + * @precision very-high |
| 8 | + * @problem.severity error |
| 9 | + * @tags external/misra/id/rule-19-3-4 |
| 10 | + * scope/single-translation-unit |
| 11 | + * correctness |
| 12 | + * maintainability |
| 13 | + * external/misra/enforcement/decidable |
| 14 | + * external/misra/obligation/required |
| 15 | + */ |
| 16 | + |
| 17 | +import cpp |
| 18 | +import codingstandards.cpp.misra |
| 19 | +import codingstandards.cpp.Macro |
| 20 | +import codingstandards.cpp.MatchingParenthesis |
| 21 | +import codeql.util.Boolean |
| 22 | + |
| 23 | +/** |
| 24 | + * This regex is used to find macro arguments that appear to have critical operators in them, before |
| 25 | + * we do the expensive process of parsing them to look for parenthesis. |
| 26 | + */ |
| 27 | +pragma[noinline] |
| 28 | +string criticalOperatorRegex() { |
| 29 | + result = |
| 30 | + ".*(" + |
| 31 | + concat(string op | |
| 32 | + op in [ |
| 33 | + "\\*=?", "/=?", "%=?", "\\+=?", "-=?", "<<?=?", ">>?=?", "==?", "!=", "&&?=?", "\\^/?", |
| 34 | + "\\|\\|?=?", "\\?" |
| 35 | + ] |
| 36 | + | |
| 37 | + op, "|" |
| 38 | + ) + ").*" |
| 39 | +} |
| 40 | + |
| 41 | +/** |
| 42 | + * Whether a string appears to contain a critical operator. |
| 43 | + */ |
| 44 | +bindingset[input] |
| 45 | +predicate hasCriticalOperator(string input) { input.regexpMatch(criticalOperatorRegex()) } |
| 46 | + |
| 47 | +/** |
| 48 | + * A critical operator is an operator with "level" between 13 and 2, according to the MISRA C++ |
| 49 | + * standard. This includes from the "multiplicative" level (13) to the "conditional" level (2). |
| 50 | + */ |
| 51 | +class CriticalOperatorExpr extends Expr { |
| 52 | + string operator; |
| 53 | + |
| 54 | + CriticalOperatorExpr() { |
| 55 | + operator = this.(BinaryOperation).getOperator() |
| 56 | + or |
| 57 | + this instanceof ConditionalExpr and operator = "?" |
| 58 | + or |
| 59 | + operator = this.(Assignment).getOperator() |
| 60 | + } |
| 61 | + |
| 62 | + string getOperator() { result = operator } |
| 63 | +} |
| 64 | + |
| 65 | +/** |
| 66 | + * An invocation of a macro that has a parameter that is not precedence-protected with parentheses, |
| 67 | + * and that produces a critical operator expression. |
| 68 | + * |
| 69 | + * This class is used in two passes. Firstly, with `hasRiskyParameter`, to find the macro paramaters |
| 70 | + * that should be parsed for parenthesis. Secondly, with `hasNonCompliantParameter`, to parse the |
| 71 | + * risky parameters and attempt to match the produced AST to an unparenthesized occurence of that |
| 72 | + * operator in the argument text. |
| 73 | + * |
| 74 | + * For a given macro invocation to be considered risky, it must |
| 75 | + * - The macro must have a parameter that is not precedence-protected with parentheses. |
| 76 | + * - The macro must produce a critical operator expression. |
| 77 | + * - The macro must produce only expressions, statements, or variable declarations with initializers. |
| 78 | + * |
| 79 | + * For a risky macro to be non-compliant, it must hold for some values of the predicate |
| 80 | + * `hasNonCompliantParameter`. |
| 81 | + */ |
| 82 | +class RiskyMacroInvocation extends MacroInvocation { |
| 83 | + FunctionLikeMacro macro; |
| 84 | + string riskyParamName; |
| 85 | + int riskyParamIdx; |
| 86 | + |
| 87 | + RiskyMacroInvocation() { |
| 88 | + macro = getMacro() and |
| 89 | + // The parameter is not precedence-protected with parentheses in the macro body. |
| 90 | + macro.parameterPrecedenceUnprotected(riskyParamIdx) and |
| 91 | + riskyParamName = macro.getParameter(riskyParamIdx) and |
| 92 | + // This macro invocation produces a critical operator expression. |
| 93 | + getAGeneratedElement() instanceof CriticalOperatorExpr and |
| 94 | + // It seems to generate an expression, statement, or variable declaration with initializer. |
| 95 | + forex(Element e | e = getAGeneratedElement() | |
| 96 | + e instanceof Expr |
| 97 | + or |
| 98 | + e instanceof Stmt |
| 99 | + or |
| 100 | + e.(Variable).getInitializer().getExpr() = getAGeneratedElement() |
| 101 | + or |
| 102 | + e.(VariableDeclarationEntry).getDeclaration().getInitializer().getExpr() = |
| 103 | + getAGeneratedElement() |
| 104 | + ) |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * A stage 1 pass used to find macro parameters that are not precedence-protected, and have a |
| 109 | + * critical operator in them, and therefore need to be parsed to check for parenthesis at the |
| 110 | + * macro call-site, which is expensive. |
| 111 | + */ |
| 112 | + predicate hasRiskyParameter(string name, int index, string value) { |
| 113 | + name = riskyParamName and |
| 114 | + index = riskyParamIdx and |
| 115 | + value = getExpandedArgument(riskyParamIdx) and |
| 116 | + hasCriticalOperator(value) |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * A stage 2 pass that occurs after risky parameters have been parsed, to check for parenthesis at the macro |
| 121 | + * call-site. |
| 122 | + * |
| 123 | + * For a given macro argument to be flagged, it must: |
| 124 | + * - be risky as determined by the characteristic predicate (produce a critical operator and only |
| 125 | + * expressions, statements, etc). |
| 126 | + * - be flagged by stage 1 as a risky parameter (i.e. it must have a critical operator in it and |
| 127 | + * correspond to a macro parameter that is not precedence-protected with parentheses) |
| 128 | + * - there must be a top-level text node that contains the operator in the argument string |
| 129 | + * - the operator cannot be the first character in the string (i.e. it should not look like a |
| 130 | + * unary - or +) |
| 131 | + * - the operator cannot exist inside a generated string literal |
| 132 | + * - the operator existence of the operator should not be as a substring of "->", "++", or "--" |
| 133 | + * operators. |
| 134 | + * |
| 135 | + * The results of this predicate should be flagged by the query. |
| 136 | + */ |
| 137 | + predicate hasNonCompliantParameter(string name, int index, string value, string operator) { |
| 138 | + hasRiskyParameter(name, index, value) and |
| 139 | + exists( |
| 140 | + ParsedRoot parsedRoot, ParsedText topLevelText, string text, CriticalOperatorExpr opExpr, |
| 141 | + int opIndex |
| 142 | + | |
| 143 | + parsedRoot.getInputString() = value and |
| 144 | + (topLevelText.getParent() = parsedRoot or topLevelText = parsedRoot) and |
| 145 | + text = topLevelText.getText().trim() and |
| 146 | + opExpr = getAGeneratedElement() and |
| 147 | + operator = opExpr.getOperator() and |
| 148 | + opIndex = text.indexOf(operator) and |
| 149 | + // Ignore "->", "++", and "--" operators. |
| 150 | + not [text.substring(opIndex - 1, opIndex + 1), text.substring(opIndex, opIndex + 2)] = |
| 151 | + ["--", "++", "->"] and |
| 152 | + // Ignore operators inside string literals. |
| 153 | + not exists(Literal l | |
| 154 | + l = getAGeneratedElement() and |
| 155 | + exists(l.getValue().indexOf(operator)) |
| 156 | + ) and |
| 157 | + // A leading operator is probably unary and not a problem. |
| 158 | + (opIndex > 0 or topLevelText.getChildIdx() > 0) |
| 159 | + ) |
| 160 | + } |
| 161 | +} |
| 162 | + |
| 163 | +/** |
| 164 | + * A string class that is used to determine what macro arguments will be parsed. |
| 165 | + * |
| 166 | + * This should be a reasonably small set of strings, as parsing is expensive. |
| 167 | + */ |
| 168 | +class RiskyMacroArgString extends string { |
| 169 | + RiskyMacroArgString() { any(RiskyMacroInvocation mi).hasRiskyParameter(_, _, this) } |
| 170 | +} |
| 171 | + |
| 172 | +// Import `ParsedRoot` etc for the parsed macro arguments. |
| 173 | +import MatchingParenthesis<RiskyMacroArgString> |
| 174 | + |
| 175 | +from |
| 176 | + RiskyMacroInvocation mi, FunctionLikeMacro m, string paramName, string criticalOperator, |
| 177 | + int paramIndex, string argumentString |
| 178 | +where |
| 179 | + not isExcluded([m.(Element), mi.(Element)], |
| 180 | + Preprocessor2Package::unparenthesizedMacroArgumentQuery()) and |
| 181 | + mi.getMacro() = m and |
| 182 | + mi.hasNonCompliantParameter(paramName, paramIndex, argumentString, criticalOperator) |
| 183 | +select mi, |
| 184 | + "Macro argument " + paramIndex + " (with expanded value '" + argumentString + "') contains a" + |
| 185 | + " critical operator '" + criticalOperator + |
| 186 | + "' that is not parenthesized, but macro $@ argument '" + paramName + |
| 187 | + "' is not precedence-protected with parenthesis.", m, m.getName() |
0 commit comments