Skip to content

Commit f6ad22d

Browse files
authored
Merge pull request #2758 from asger-semmle/js/string-concat-concat
JS: Model concat() calls as string concatenation
2 parents c77a921 + b4df037 commit f6ad22d

File tree

5 files changed

+52
-0
lines changed

5 files changed

+52
-0
lines changed

javascript/ql/src/semmle/javascript/StringConcatenation.qll

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,24 @@ module StringConcatenation {
5151
call = Closure::moduleImport("goog.string.buildString").getACall() and
5252
result = call.getArgument(n)
5353
)
54+
or
55+
exists(DataFlow::MethodCallNode call |
56+
node = call and
57+
call.getMethodName() = "concat" and
58+
not (
59+
exists(DataFlow::ArrayCreationNode array |
60+
array.flowsTo(call.getAnArgument()) or array.flowsTo(call.getReceiver())
61+
)
62+
or
63+
DataFlow::reflectiveCallNode(_) = call
64+
) and
65+
(
66+
n = 0 and
67+
result = call.getReceiver()
68+
or
69+
result = call.getArgument(n - 1)
70+
)
71+
)
5472
}
5573

5674
/** Gets an operand to the string concatenation defining `node`. */

javascript/ql/test/library-tests/StringConcatenation/ClassContainsTwo.expected

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,7 @@
4242
| tst.js:89:3:89:3 | x |
4343
| tst.js:89:3:89:14 | x += 'three' |
4444
| tst.js:90:10:90:10 | x |
45+
| tst.js:95:3:95:30 | x = x.c ... three') |
46+
| tst.js:95:7:95:30 | x.conca ... three') |
47+
| tst.js:95:16:95:20 | 'two' |
48+
| tst.js:96:10:96:10 | x |

javascript/ql/test/library-tests/StringConcatenation/ContainsTwo.expected

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,7 @@
4242
| tst.js:89:3:89:3 | x |
4343
| tst.js:89:3:89:14 | x += 'three' |
4444
| tst.js:90:10:90:10 | x |
45+
| tst.js:95:3:95:30 | x = x.c ... three') |
46+
| tst.js:95:7:95:30 | x.conca ... three') |
47+
| tst.js:95:16:95:20 | 'two' |
48+
| tst.js:96:10:96:10 | x |

javascript/ql/test/library-tests/StringConcatenation/StringOps.expected

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ concatenation
4545
| tst.js:87:5:87:14 | x += 'two' |
4646
| tst.js:89:3:89:14 | x |
4747
| tst.js:89:3:89:14 | x += 'three' |
48+
| tst.js:95:7:95:30 | x.conca ... three') |
4849
concatenationOperand
4950
| closure.js:5:1:5:37 | build(' ... 'four') |
5051
| closure.js:5:7:5:11 | 'one' |
@@ -123,6 +124,9 @@ concatenationOperand
123124
| tst.js:87:10:87:14 | 'two' |
124125
| tst.js:89:3:89:3 | x |
125126
| tst.js:89:8:89:14 | 'three' |
127+
| tst.js:95:7:95:7 | x |
128+
| tst.js:95:16:95:20 | 'two' |
129+
| tst.js:95:23:95:29 | 'three' |
126130
concatenationLeaf
127131
| closure.js:5:7:5:11 | 'one' |
128132
| closure.js:5:14:5:18 | 'two' |
@@ -192,6 +196,9 @@ concatenationLeaf
192196
| tst.js:87:10:87:14 | 'two' |
193197
| tst.js:89:3:89:3 | x |
194198
| tst.js:89:8:89:14 | 'three' |
199+
| tst.js:95:7:95:7 | x |
200+
| tst.js:95:16:95:20 | 'two' |
201+
| tst.js:95:23:95:29 | 'three' |
195202
concatenationNode
196203
| closure.js:5:1:5:37 | build(' ... 'four') |
197204
| closure.js:5:1:5:46 | build(' ... 'five' |
@@ -307,6 +314,10 @@ concatenationNode
307314
| tst.js:89:3:89:14 | x |
308315
| tst.js:89:3:89:14 | x += 'three' |
309316
| tst.js:89:8:89:14 | 'three' |
317+
| tst.js:95:7:95:7 | x |
318+
| tst.js:95:7:95:30 | x.conca ... three') |
319+
| tst.js:95:16:95:20 | 'two' |
320+
| tst.js:95:23:95:29 | 'three' |
310321
operand
311322
| closure.js:5:1:5:37 | build(' ... 'four') | 0 | closure.js:5:7:5:11 | 'one' |
312323
| closure.js:5:1:5:37 | build(' ... 'four') | 1 | closure.js:5:14:5:28 | 'two' + 'three' |
@@ -407,6 +418,9 @@ operand
407418
| tst.js:89:3:89:14 | x | 1 | tst.js:89:8:89:14 | 'three' |
408419
| tst.js:89:3:89:14 | x += 'three' | 0 | tst.js:89:3:89:3 | x |
409420
| tst.js:89:3:89:14 | x += 'three' | 1 | tst.js:89:8:89:14 | 'three' |
421+
| tst.js:95:7:95:30 | x.conca ... three') | 0 | tst.js:95:7:95:7 | x |
422+
| tst.js:95:7:95:30 | x.conca ... three') | 1 | tst.js:95:16:95:20 | 'two' |
423+
| tst.js:95:7:95:30 | x.conca ... three') | 2 | tst.js:95:23:95:29 | 'three' |
410424
nextLeaf
411425
| closure.js:5:7:5:11 | 'one' | closure.js:5:14:5:18 | 'two' |
412426
| closure.js:5:14:5:18 | 'two' | closure.js:5:22:5:28 | 'three' |
@@ -450,6 +464,8 @@ nextLeaf
450464
| tst.js:61:27:61:27 | x | tst.js:61:29:61:33 | last |
451465
| tst.js:87:5:87:5 | x | tst.js:87:10:87:14 | 'two' |
452466
| tst.js:89:3:89:3 | x | tst.js:89:8:89:14 | 'three' |
467+
| tst.js:95:7:95:7 | x | tst.js:95:16:95:20 | 'two' |
468+
| tst.js:95:16:95:20 | 'two' | tst.js:95:23:95:29 | 'three' |
453469
htmlRoot
454470
| html-concat.js:2:14:2:26 | `<b>${x}</b>` |
455471
| html-concat.js:3:14:3:26 | `<B>${x}</B>` |

javascript/ql/test/library-tests/StringConcatenation/tst.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,13 @@ function addExprPhi(b) {
8989
x += 'three';
9090
return x;
9191
}
92+
93+
function concatCall() {
94+
let x = 'one';
95+
x = x.concat('two', 'three');
96+
return x;
97+
}
98+
99+
function arrayConcat(a, b) {
100+
return [].concat(a, b);
101+
}

0 commit comments

Comments
 (0)