|
| 1 | +import cpp |
| 2 | +import semmle.code.cpp.models.interfaces.Taint |
| 3 | + |
| 4 | +class IteratorTraits extends Class { |
| 5 | + IteratorTraits() { |
| 6 | + this.hasQualifiedName("std", "iterator_traits") and |
| 7 | + not this instanceof TemplateClass and |
| 8 | + exists(TypedefType t | |
| 9 | + this.getAMember() = t and |
| 10 | + t.getName() = "iterator_category" |
| 11 | + ) |
| 12 | + } |
| 13 | + |
| 14 | + Type getIteratorType() { result = this.getTemplateArgument(0) } |
| 15 | +} |
| 16 | + |
| 17 | +class IteratorByTypedefs extends Class { |
| 18 | + IteratorByTypedefs() { |
| 19 | + this.getAMember().(TypedefType).hasName("difference_type") and |
| 20 | + this.getAMember().(TypedefType).hasName("value_type") and |
| 21 | + this.getAMember().(TypedefType).hasName("pointer") and |
| 22 | + this.getAMember().(TypedefType).hasName("reference") and |
| 23 | + this.getAMember().(TypedefType).hasName("iterator_category") and |
| 24 | + not this.hasQualifiedName("std", "iterator_traits") |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +class IteratorByStdIteratorTraits extends Type { } |
| 29 | + |
| 30 | +class LegacyIterator extends Type { |
| 31 | + LegacyIterator() { |
| 32 | + this instanceof IteratorByTypedefs or |
| 33 | + exists(IteratorTraits it | it.getIteratorType() = this) |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +class IteratorOperatorStar extends Operator, TaintFunction { |
| 38 | + IteratorOperatorStar() { |
| 39 | + this.hasName("operator*") and |
| 40 | + this.getACallToThisFunction().getArgument(0).getFullyConverted().getUnderlyingType() instanceof |
| 41 | + LegacyIterator |
| 42 | + } |
| 43 | + |
| 44 | + override predicate hasTaintFlow(FunctionInput input, FunctionOutput output) { |
| 45 | + input.isParameter(0) and |
| 46 | + output.isReturnValue() |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +class IteratorOperatorPlusPlus extends Operator, TaintFunction { |
| 51 | + IteratorOperatorPlusPlus() { |
| 52 | + this.hasName("operator++") and |
| 53 | + this |
| 54 | + .getACallToThisFunction() |
| 55 | + .getArgument(0) |
| 56 | + .getFullyConverted() |
| 57 | + .getUnderlyingType() |
| 58 | + .(ReferenceType) |
| 59 | + .getBaseType() instanceof LegacyIterator |
| 60 | + } |
| 61 | + |
| 62 | + override predicate hasTaintFlow(FunctionInput input, FunctionOutput output) { |
| 63 | + input.isParameterDeref(0) and |
| 64 | + output.isParameterDeref(0) |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | + |
| 69 | +class IteratorOperatorMinusMinus extends Operator, TaintFunction { |
| 70 | + IteratorOperatorMinusMinus() { |
| 71 | + this.hasName("operator++") and |
| 72 | + this |
| 73 | + .getACallToThisFunction() |
| 74 | + .getArgument(0) |
| 75 | + .getFullyConverted() |
| 76 | + .getUnderlyingType() |
| 77 | + .(ReferenceType) |
| 78 | + .getBaseType() instanceof LegacyIterator |
| 79 | + } |
| 80 | + |
| 81 | + override predicate hasTaintFlow(FunctionInput input, FunctionOutput output) { |
| 82 | + input.isParameterDeref(0) and |
| 83 | + output.isParameterDeref(0) |
| 84 | + } |
| 85 | +} |
| 86 | +class IteratorOperatorArrow extends Operator, TaintFunction { |
| 87 | + IteratorOperatorArrow() { |
| 88 | + this.hasName("operator->") and |
| 89 | + this |
| 90 | + .getACallToThisFunction() |
| 91 | + .getArgument(0) |
| 92 | + .getFullyConverted() |
| 93 | + .getUnderlyingType() |
| 94 | + .(PointerType) |
| 95 | + .getBaseType() instanceof LegacyIterator |
| 96 | + } |
| 97 | + |
| 98 | + override predicate hasTaintFlow(FunctionInput input, FunctionOutput output) { |
| 99 | + input.isParameterDeref(0) and |
| 100 | + output.isReturnValue() |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +class IteratorMemberOperatorStar extends MemberFunction, TaintFunction { |
| 105 | + IteratorMemberOperatorStar() { |
| 106 | + this.hasName("operator*") and |
| 107 | + this.getDeclaringType() instanceof LegacyIterator |
| 108 | + } |
| 109 | + |
| 110 | + override predicate hasTaintFlow(FunctionInput input, FunctionOutput output) { |
| 111 | + input.isQualifierObject() and |
| 112 | + output.isReturnValue() |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +class IteratorMemberOperatorPlusPlus extends MemberFunction, TaintFunction { |
| 117 | + IteratorMemberOperatorPlusPlus() { |
| 118 | + this.hasName("operator++") and |
| 119 | + this.getDeclaringType() instanceof LegacyIterator |
| 120 | + } |
| 121 | + |
| 122 | + override predicate hasTaintFlow(FunctionInput input, FunctionOutput output) { |
| 123 | + input.isQualifierObject() and |
| 124 | + output.isQualifierObject() |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +class IteratorMemberOperatorArrow extends Operator, TaintFunction { |
| 129 | + IteratorMemberOperatorArrow() { |
| 130 | + this.hasName("operator->") and |
| 131 | + this.getDeclaringType() instanceof LegacyIterator |
| 132 | + } |
| 133 | + |
| 134 | + override predicate hasTaintFlow(FunctionInput input, FunctionOutput output) { |
| 135 | + input.isQualifierAddress() and |
| 136 | + output.isReturnValue() |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +class IteratorMemberOperatorMinusMinus extends MemberFunction, TaintFunction { |
| 141 | + IteratorMemberOperatorMinusMinus() { |
| 142 | + this.hasName("operator--") and |
| 143 | + this.getDeclaringType() instanceof LegacyIterator |
| 144 | + } |
| 145 | + |
| 146 | + override predicate hasTaintFlow(FunctionInput input, FunctionOutput output) { |
| 147 | + input.isQualifierObject() and |
| 148 | + output.isQualifierObject() |
| 149 | + } |
| 150 | +} |
0 commit comments