|
| 1 | +import cpp |
| 2 | +import semmle.code.cpp.models.implementations.StdContainer |
| 3 | + |
| 4 | +/** |
| 5 | + * Holds if `e` will be consumed by its parent as a glvalue and does not have |
| 6 | + * an lvalue-to-rvalue conversion. This means that it will be materialized into |
| 7 | + * a temporary object. |
| 8 | + */ |
| 9 | +predicate isTemporary(Expr e) { |
| 10 | + e instanceof TemporaryObjectExpr |
| 11 | + or |
| 12 | + e.isPRValueCategory() and |
| 13 | + e.getUnspecifiedType() instanceof Class and |
| 14 | + not e.hasLValueToRValueConversion() |
| 15 | +} |
| 16 | + |
| 17 | +/** Holds if `e` is written to a container. */ |
| 18 | +predicate isStoredInContainer(Expr e) { |
| 19 | + exists(StdSequenceContainerInsert insert, Call call, int index | |
| 20 | + call = insert.getACallToThisFunction() and |
| 21 | + index = insert.getAValueTypeParameterIndex() and |
| 22 | + call.getArgument(index) = e |
| 23 | + ) |
| 24 | + or |
| 25 | + exists(StdSequenceContainerPush push, Call call, int index | |
| 26 | + call = push.getACallToThisFunction() and |
| 27 | + index = push.getAValueTypeParameterIndex() and |
| 28 | + call.getArgument(index) = e |
| 29 | + ) |
| 30 | + or |
| 31 | + exists(StdSequenceEmplace emplace, Call call, int index | |
| 32 | + call = emplace.getACallToThisFunction() and |
| 33 | + index = emplace.getAValueTypeParameterIndex() and |
| 34 | + call.getArgument(index) = e |
| 35 | + ) |
| 36 | + or |
| 37 | + exists(StdSequenceEmplaceBack emplaceBack, Call call, int index | |
| 38 | + call = emplaceBack.getACallToThisFunction() and |
| 39 | + index = emplaceBack.getAValueTypeParameterIndex() and |
| 40 | + call.getArgument(index) = e |
| 41 | + ) |
| 42 | +} |
| 43 | + |
| 44 | +/** |
| 45 | + * Holds if `e` or a conversion of `e` has an lvalue-to-rvalue conversion. |
| 46 | + */ |
| 47 | +private predicate hasLValueToRValueConversion(Expr e) { |
| 48 | + e.getConversion*().hasLValueToRValueConversion() and |
| 49 | + not e instanceof ConditionalExpr // ConditionalExpr may be spuriously reported as having an lvalue-to-rvalue conversion |
| 50 | +} |
| 51 | + |
| 52 | +/** |
| 53 | + * Holds if the value of `e` outlives the enclosing full expression. For |
| 54 | + * example, because the value is stored in a local variable. |
| 55 | + */ |
| 56 | +predicate outlivesFullExpr(Expr e) { |
| 57 | + not hasLValueToRValueConversion(e) and |
| 58 | + ( |
| 59 | + any(Assignment assign).getRValue() = e |
| 60 | + or |
| 61 | + any(Variable v).getInitializer().getExpr() = e |
| 62 | + or |
| 63 | + any(ReturnStmt ret).getExpr() = e |
| 64 | + or |
| 65 | + exists(ConditionalExpr cond | |
| 66 | + outlivesFullExpr(cond) and |
| 67 | + [cond.getThen(), cond.getElse()] = e |
| 68 | + ) |
| 69 | + or |
| 70 | + exists(BinaryOperation bin | |
| 71 | + outlivesFullExpr(bin) and |
| 72 | + bin.getAnOperand() = e and |
| 73 | + not bin instanceof ComparisonOperation |
| 74 | + ) |
| 75 | + or |
| 76 | + exists(PointerFieldAccess fa | |
| 77 | + outlivesFullExpr(fa) and |
| 78 | + fa.getQualifier() = e |
| 79 | + ) |
| 80 | + or |
| 81 | + exists(AddressOfExpr ao | |
| 82 | + outlivesFullExpr(ao) and |
| 83 | + ao.getOperand() = e |
| 84 | + ) |
| 85 | + or |
| 86 | + exists(ClassAggregateLiteral aggr | |
| 87 | + outlivesFullExpr(aggr) and |
| 88 | + aggr.getAFieldExpr(_) = e |
| 89 | + ) |
| 90 | + or |
| 91 | + exists(ArrayAggregateLiteral aggr | |
| 92 | + outlivesFullExpr(aggr) and |
| 93 | + aggr.getAnElementExpr(_) = e |
| 94 | + ) |
| 95 | + or |
| 96 | + isStoredInContainer(e) |
| 97 | + ) |
| 98 | +} |
0 commit comments