55import cpp
66import semmle.code.cpp.models.implementations.Strcat
77import semmle.code.cpp.models.interfaces.FormattingFunction
8+ import semmle.code.cpp.dataflow.new.DataFlow
89
910class StringConcatenation extends Call {
1011 StringConcatenation ( ) {
11- // printf -like functions, i.e., concat through formating
12+ // sprintf -like functions, i.e., concat through formating
1213 exists ( FormattingFunctionCall fc | this = fc )
1314 or
14- // strcat variants
15- exists ( StrcatFunction f | this .getTarget ( ) = f )
15+ this .getTarget ( ) instanceof StrcatFunction
16+ or
17+ this .getTarget ( ) instanceof StrlcatFunction
1618 or
1719 // operator+ concat
1820 exists ( Call call , Operator op |
@@ -35,7 +37,9 @@ class StringConcatenation extends Call {
3537 Expr getAnOperand ( ) {
3638 // The result is an argument of 'this' (a call)
3739 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
3943 // Limit the result type to string
4044 (
4145 result .getUnderlyingType ( ) .stripType ( ) .getName ( ) = "char"
@@ -69,11 +73,26 @@ class StringConcatenation extends Call {
6973 }
7074
7175 /**
72- * Gets the expression representing the concatenation result.
76+ * Gets the data flow node representing the concatenation result.
7377 */
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 )
7897 }
7998}
0 commit comments