|
20 | 20 | package org.sonar.python.checks; |
21 | 21 |
|
22 | 22 | import java.util.Arrays; |
| 23 | +import java.util.HashSet; |
23 | 24 | import java.util.List; |
| 25 | +import java.util.Set; |
24 | 26 | import org.sonar.check.Rule; |
25 | 27 | import org.sonar.plugins.python.api.PythonSubscriptionCheck; |
26 | 28 | import org.sonar.plugins.python.api.SubscriptionContext; |
27 | 29 | import org.sonar.plugins.python.api.symbols.Symbol; |
28 | 30 | import org.sonar.plugins.python.api.tree.Argument; |
| 31 | +import org.sonar.plugins.python.api.tree.BinaryExpression; |
29 | 32 | import org.sonar.plugins.python.api.tree.CallExpression; |
30 | 33 | import org.sonar.plugins.python.api.tree.Expression; |
31 | 34 | import org.sonar.plugins.python.api.tree.HasSymbol; |
@@ -75,21 +78,38 @@ private static void checkSensitiveArgument(List<Argument> arguments , int sensit |
75 | 78 | return; |
76 | 79 | } |
77 | 80 | Expression expression = modeArgument.expression(); |
| 81 | + if(isUnsafeExpression(expression, safeModulo, new HashSet<>())) { |
| 82 | + ctx.addIssue(modeArgument, MESSAGE); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + private static boolean isUnsafeExpression(Expression expression, int safeModulo, Set<Expression> checkedExpressions) { |
| 87 | + if (checkedExpressions.contains(expression)) { |
| 88 | + return false; |
| 89 | + } |
| 90 | + checkedExpressions.add(expression); |
78 | 91 | if (expression instanceof HasSymbol) { |
79 | 92 | Symbol symbol = ((HasSymbol) expression).symbol(); |
80 | 93 | if (symbol != null && SENSITIVE_CONSTANTS.contains(symbol.fullyQualifiedName())) { |
81 | | - ctx.addIssue(modeArgument, MESSAGE); |
82 | | - return; |
| 94 | + return true; |
83 | 95 | } |
84 | 96 | } |
85 | | - if (expression.is(Tree.Kind.NAME)) { |
86 | | - expression = Expressions.singleAssignedValue(((Name) expression)); |
| 97 | + if (expression.is(Tree.Kind.BITWISE_OR)) { |
| 98 | + BinaryExpression binaryExpression = (BinaryExpression) expression; |
| 99 | + return isUnsafeExpression(binaryExpression.leftOperand(), safeModulo, checkedExpressions) |
| 100 | + || isUnsafeExpression(binaryExpression.rightOperand(), safeModulo, checkedExpressions); |
87 | 101 | } |
88 | | - if (expression != null && expression.is(Tree.Kind.NUMERIC_LITERAL)) { |
| 102 | + if (expression.is(Tree.Kind.NUMERIC_LITERAL)) { |
89 | 103 | NumericLiteral numericLiteral = (NumericLiteral) expression; |
90 | | - if (numericLiteral.valueAsLong() % 8 != safeModulo) { |
91 | | - ctx.addIssue(modeArgument, MESSAGE); |
| 104 | + return numericLiteral.valueAsLong() % 8 != safeModulo; |
| 105 | + } |
| 106 | + if (expression.is(Tree.Kind.NAME)) { |
| 107 | + Expression singleAssignedValue = Expressions.singleAssignedValue(((Name) expression)); |
| 108 | + if (singleAssignedValue == null) { |
| 109 | + return false; |
92 | 110 | } |
| 111 | + return isUnsafeExpression(singleAssignedValue, safeModulo, checkedExpressions); |
93 | 112 | } |
| 113 | + return false; |
94 | 114 | } |
95 | 115 | } |
0 commit comments