|
| 1 | +/** |
| 2 | + * A library for detecting general string concatenations. |
| 3 | + */ |
| 4 | + |
| 5 | +import cpp |
| 6 | +import semmle.code.cpp.models.implementations.Strcat |
| 7 | +import semmle.code.cpp.models.interfaces.FormattingFunction |
| 8 | +private import semmle.code.cpp.dataflow.new.DataFlow |
| 9 | + |
| 10 | +/** |
| 11 | + * A call that performs a string concatenation. A string can be either a C |
| 12 | + * string (i.e., a value of type `char*`), or a C++ string (i.e., a value of |
| 13 | + * type `std::string`). |
| 14 | + */ |
| 15 | +class StringConcatenation extends Call { |
| 16 | + StringConcatenation() { |
| 17 | + // sprintf-like functions, i.e., concat through formatting |
| 18 | + this instanceof FormattingFunctionCall |
| 19 | + or |
| 20 | + this.getTarget() instanceof StrcatFunction |
| 21 | + or |
| 22 | + this.getTarget() instanceof StrlcatFunction |
| 23 | + or |
| 24 | + // operator+ and ostream (<<) concat |
| 25 | + exists(Call call, Operator op | |
| 26 | + call.getTarget() = op and |
| 27 | + op.hasQualifiedName(["std", "bsl"], ["operator+", "operator<<"]) and |
| 28 | + op.getType() |
| 29 | + .stripType() |
| 30 | + .(UserType) |
| 31 | + .hasQualifiedName(["std", "bsl"], ["basic_string", "basic_ostream"]) and |
| 32 | + this = call |
| 33 | + ) |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Gets an operand of this concatenation (one of the string operands being |
| 38 | + * concatenated). |
| 39 | + * Will not return out param for sprintf-like functions, but will consider the format string |
| 40 | + * to be part of the operands. |
| 41 | + */ |
| 42 | + Expr getAnOperand() { |
| 43 | + // The result is an argument of 'this' (a call) |
| 44 | + result = this.getAnArgument() and |
| 45 | + // addresses odd behavior with overloaded operators |
| 46 | + // i.e., "call to operator+" appearing as an operand |
| 47 | + // occurs in cases like `string s = s1 + s2 + s3`, which is represented as |
| 48 | + // `string s = (s1.operator+(s2)).operator+(s3);` |
| 49 | + // By limiting to non-calls we get the leaf operands (the variables or raw strings) |
| 50 | + // also, by not enumerating allowed types (variables and strings) we avoid issues |
| 51 | + // with missed corner cases or extensions/changes to CodeQL in the future which might |
| 52 | + // invalidate that approach. |
| 53 | + not result instanceof Call and |
| 54 | + // Limit the result type to string |
| 55 | + ( |
| 56 | + result.getUnderlyingType().stripType().getName() = "char" |
| 57 | + or |
| 58 | + result |
| 59 | + .getType() |
| 60 | + .getUnspecifiedType() |
| 61 | + .(UserType) |
| 62 | + .hasQualifiedName(["std", "bsl"], "basic_string") |
| 63 | + ) and |
| 64 | + // when 'this' is a `FormattingFunctionCall` the result must be the format string argument |
| 65 | + // or one of the formatting arguments |
| 66 | + ( |
| 67 | + this instanceof FormattingFunctionCall |
| 68 | + implies |
| 69 | + ( |
| 70 | + result = this.(FormattingFunctionCall).getFormat() |
| 71 | + or |
| 72 | + exists(int n | |
| 73 | + result = this.getArgument(n) and |
| 74 | + n >= this.(FormattingFunctionCall).getTarget().getFirstFormatArgumentIndex() |
| 75 | + ) |
| 76 | + ) |
| 77 | + ) |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Gets the data flow node representing the concatenation result. |
| 82 | + */ |
| 83 | + DataFlow::Node getResultNode() { |
| 84 | + if this.getTarget() instanceof StrcatFunction |
| 85 | + then |
| 86 | + result.asDefiningArgument() = |
| 87 | + this.getArgument(this.getTarget().(StrcatFunction).getParamDest()) |
| 88 | + or |
| 89 | + // Hardcoding it is also the return |
| 90 | + result.asExpr() = this.(Call) |
| 91 | + else |
| 92 | + if this.getTarget() instanceof StrlcatFunction |
| 93 | + then ( |
| 94 | + result.asDefiningArgument() = |
| 95 | + this.getArgument(this.getTarget().(StrlcatFunction).getParamDest()) |
| 96 | + ) else |
| 97 | + if this instanceof FormattingFunctionCall |
| 98 | + then result.asDefiningArgument() = this.(FormattingFunctionCall).getOutputArgument(_) |
| 99 | + else result.asExpr() = this.(Call) |
| 100 | + } |
| 101 | +} |
0 commit comments