|
| 1 | +/** |
| 2 | + * @name Recursive functions |
| 3 | + * @id tob/java/recursion |
| 4 | + * @description Detects recursive calls |
| 5 | + * @kind problem |
| 6 | + * @tags security |
| 7 | + * @precision low |
| 8 | + * @problem.severity warning |
| 9 | + * @security-severity 3.0 |
| 10 | + * @group security |
| 11 | + */ |
| 12 | + |
| 13 | +import java |
| 14 | + |
| 15 | +predicate isTestPackage(RefType referenceType) { |
| 16 | + referenceType.getPackage().getName().toLowerCase().matches("%test%") or |
| 17 | + referenceType.getPackage().getName().toLowerCase().matches("%benchmark%") or |
| 18 | + referenceType.getName().toLowerCase().matches("%test%") |
| 19 | +} |
| 20 | + |
| 21 | +predicate isBefore(Method a, Method b) { |
| 22 | + a.getLocation().getStartLine() < b.getLocation().getStartLine() |
| 23 | +} |
| 24 | + |
| 25 | +abstract class RecursiveCall extends MethodCall { |
| 26 | + Method m1; |
| 27 | + Method m2; |
| 28 | + Method m3; |
| 29 | + Method m4; |
| 30 | + |
| 31 | + abstract string asString(); |
| 32 | + |
| 33 | + string toString(Method m) { |
| 34 | + result = m.getName() + "(" + m.getLocation().getStartLine() + ")" + " -> " |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +class RecursiveCallOrder1 extends RecursiveCall { |
| 39 | + RecursiveCallOrder1() { |
| 40 | + this.getMethod() = this.getEnclosingCallable() and |
| 41 | + m1 = this.getMethod() |
| 42 | + } |
| 43 | + |
| 44 | + override string asString() { result = "Recursion(1): " + toString(m1) + m1.getName() } |
| 45 | +} |
| 46 | + |
| 47 | +class RecursiveCallOrder2 extends RecursiveCall { |
| 48 | + //RecursiveCallOrder1 { |
| 49 | + RecursiveCallOrder2() { |
| 50 | + m1 = this.getMethod() and |
| 51 | + m2 = m1.getACallee() and |
| 52 | + m2.getACallee() = m1 and |
| 53 | + isBefore(m1, m2) |
| 54 | + } |
| 55 | + |
| 56 | + override string asString() { |
| 57 | + result = "Recursion(2): " + toString(m1) + toString(m2) + m1.getName() |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +class RecursiveCallOrder3 extends RecursiveCall { |
| 62 | + RecursiveCallOrder3() { |
| 63 | + m1 = this.getMethod() and |
| 64 | + m2 = m1.getACallee() and |
| 65 | + m3 = m2.getACallee() and |
| 66 | + m3.getACallee() = m1 and |
| 67 | + isBefore(m1, m2) and |
| 68 | + isBefore(m1, m3) |
| 69 | + } |
| 70 | + |
| 71 | + override string asString() { |
| 72 | + result = "Recursion(3): " + toString(m1) + toString(m2) + toString(m3) + m1.getName() |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +class RecursiveCallOrder4 extends RecursiveCall { |
| 77 | + RecursiveCallOrder4() { |
| 78 | + m1 = this.getMethod() and |
| 79 | + m2 = m1.getACallee() and |
| 80 | + m3 = m2.getACallee() and |
| 81 | + m4 = m3.getACallee() and |
| 82 | + m4.getACallee() = m1 and |
| 83 | + isBefore(m1, m2) and |
| 84 | + isBefore(m1, m3) and |
| 85 | + isBefore(m1, m4) |
| 86 | + } |
| 87 | + |
| 88 | + override string asString() { |
| 89 | + result = |
| 90 | + "Recursion(4): " + toString(m1) + toString(m2) + toString(m3) + toString(m4) + m1.getName() |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +from RecursiveCall recursiveCall |
| 95 | +where not isTestPackage(recursiveCall.getMethod().getDeclaringType()) |
| 96 | +select recursiveCall, "$@", recursiveCall, recursiveCall.asString() |
0 commit comments