5
5
import cpp
6
6
import semmle.code.cpp.models.implementations.Strcat
7
7
import semmle.code.cpp.models.interfaces.FormattingFunction
8
+ import semmle.code.cpp.dataflow.new.DataFlow
8
9
9
10
class StringConcatenation extends Call {
10
11
StringConcatenation ( ) {
11
- // printf -like functions, i.e., concat through formating
12
+ // sprintf -like functions, i.e., concat through formating
12
13
exists ( FormattingFunctionCall fc | this = fc )
13
14
or
14
- // strcat variants
15
- exists ( StrcatFunction f | this .getTarget ( ) = f )
15
+ this .getTarget ( ) instanceof StrcatFunction
16
+ or
17
+ this .getTarget ( ) instanceof StrlcatFunction
16
18
or
17
19
// operator+ concat
18
20
exists ( Call call , Operator op |
@@ -35,7 +37,9 @@ class StringConcatenation extends Call {
35
37
Expr getAnOperand ( ) {
36
38
// The result is an argument of 'this' (a call)
37
39
result = this .getAnArgument ( ) and
38
- not result instanceof Call and // addresses odd behavior with overloaded operators
40
+ // addresses odd behavior with overloaded operators
41
+ // i.e., "call to operator+" appearing as an operand
42
+ not result instanceof Call and
39
43
// Limit the result type to string
40
44
(
41
45
result .getUnderlyingType ( ) .stripType ( ) .getName ( ) = "char"
@@ -69,11 +73,26 @@ class StringConcatenation extends Call {
69
73
}
70
74
71
75
/**
72
- * Gets the expression representing the concatenation result.
76
+ * Gets the data flow node representing the concatenation result.
73
77
*/
74
- Expr getResultExpr ( ) {
75
- if this instanceof FormattingFunctionCall
76
- then result = this .( FormattingFunctionCall ) .getOutputArgument ( _)
77
- else result = this .( Call )
78
+ DataFlow:: Node getResultNode ( ) {
79
+ if this .getTarget ( ) instanceof StrcatFunction
80
+ then
81
+ result .asDefiningArgument ( ) =
82
+ this .getArgument ( this .getTarget ( ) .( StrcatFunction ) .getParamDest ( ) )
83
+ or
84
+ // Hardcoding it is also the return
85
+ [ result .asExpr ( ) , result .asIndirectExpr ( ) ] = this .( Call )
86
+ else
87
+ if this .getTarget ( ) instanceof StrlcatFunction
88
+ then (
89
+ [ result .asExpr ( ) , result .asIndirectExpr ( ) ] =
90
+ this .getArgument ( this .getTarget ( ) .( StrlcatFunction ) .getParamDest ( ) )
91
+ ) else
92
+ if this instanceof FormattingFunctionCall
93
+ then
94
+ [ result .asExpr ( ) , result .asIndirectExpr ( ) ] =
95
+ this .( FormattingFunctionCall ) .getOutputArgument ( _)
96
+ else [ result .asExpr ( ) , result .asIndirectExpr ( ) ] = this .( Call )
78
97
}
79
98
}
0 commit comments