Skip to content

Commit 5b26d03

Browse files
committed
introduce backtracking, and also marking join/slice calls
1 parent 5489a80 commit 5b26d03

File tree

8 files changed

+55
-48
lines changed

8 files changed

+55
-48
lines changed

javascript/ql/src/Statements/IgnoreConcatReturn.qhelp renamed to javascript/ql/src/Statements/IgnoreArrayResult.qhelp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,18 @@
44
<qhelp>
55
<overview>
66
<p>
7-
The <code>concat</code> method on is pure and does not modify any of the input
8-
arrays. It is therefore generally an error to ignore the return value from a
9-
call to <code>concat</code>.
7+
The <code>concat</code>, <code>join</code> and <code>slice</code> methods are
8+
pure and does not modify any of the inputs or the array the method was called
9+
on. It is therefore generally an error to ignore the return value from a call
10+
to one of these methods.
1011
</p>
1112

1213
</overview>
1314
<recommendation>
1415

1516
<p>
16-
Use the returned value from the call to <code>concat</code>.
17+
Use the returned value from the calls to <code>concat</code>, <code>join</code>
18+
or <code>slice</code>.
1719
</p>
1820

1921
</recommendation>
@@ -26,19 +28,21 @@ function uses the <code>concat</code> method to add elements to the
2628
effect as the return value from <code>concat</code> is ignored.
2729
</p>
2830

29-
<sample src="examples/IgnoreConcat.js" />
31+
<sample src="examples/IgnoreArrayResult.js" />
3032

3133
<p>
3234
Assigning the returned value from the call to <code>concat</code> to the
3335
<code>arr</code> variable fixes the error.
3436
</p>
3537

36-
<sample src="examples/IgnoreConcatFixed.js" />
38+
<sample src="examples/IgnoreArrayResultFixed.js" />
3739

3840
</example>
3941
<references>
4042

4143
<li>Mozilla Developer Network: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/concat">Array concat</a>.</li>
44+
<li>Mozilla Developer Network: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice">Array slice</a>.</li>
45+
<li>Mozilla Developer Network: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/join">Array join</a>.</li>
4246

4347
</references>
4448
</qhelp>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* @name Ignoring result from pure array method
3+
* @description The array methods do not modify the array, ignoring the result of such a call is therefore generally an error.
4+
* @kind problem
5+
* @problem.severity warning
6+
* @id js/ignore-array-result
7+
* @tags maintainability,
8+
* correctness
9+
* @precision high
10+
*/
11+
12+
import javascript
13+
import Expressions.ExprHasNoEffect
14+
15+
DataFlow::SourceNode callsArray(DataFlow::TypeBackTracker t, DataFlow::MethodCallNode call) {
16+
isIgnoredPureArrayCall(call) and
17+
(
18+
t.start() and
19+
result = call.getReceiver()
20+
or
21+
exists(DataFlow::TypeBackTracker t2 | result = callsArray(t2, call).backtrack(t2, t))
22+
)
23+
}
24+
25+
DataFlow::SourceNode callsArray(DataFlow::MethodCallNode call) {
26+
result = callsArray(DataFlow::TypeBackTracker::end(), call)
27+
}
28+
29+
predicate isIgnoredPureArrayCall(DataFlow::MethodCallNode call) {
30+
inVoidContext(call.asExpr()) and
31+
(
32+
call.getMethodName() = "concat" and
33+
call.getNumArgument() = 1
34+
or
35+
call.getMethodName() = "join" and
36+
call.getNumArgument() < 2
37+
or
38+
call.getMethodName() = "slice" and
39+
call.getNumArgument() < 3
40+
)
41+
}
42+
43+
from DataFlow::MethodCallNode call
44+
where callsArray(call) instanceof DataFlow::ArrayCreationNode
45+
select call, "Result from call to " + call.getMethodName() + " ignored."

javascript/ql/src/Statements/IgnoreConcatReturn.ql

Lines changed: 0 additions & 42 deletions
This file was deleted.

javascript/ql/src/Statements/examples/IgnoreConcat.js renamed to javascript/ql/src/Statements/examples/IgnoreArrayResult.js

File renamed without changes.

javascript/ql/src/Statements/examples/IgnoreConcatFixed.js renamed to javascript/ql/src/Statements/examples/IgnoreArrayResultFixed.js

File renamed without changes.

javascript/ql/test/query-tests/Statements/IgnoreConcatReturn/IgnoreConcatReturn.expected renamed to javascript/ql/test/query-tests/Statements/IgnoreArrayResult/IgnoreArrayResult.expected

File renamed without changes.

javascript/ql/test/query-tests/Statements/IgnoreConcatReturn/IgnoreConcatReturn.qlref renamed to javascript/ql/test/query-tests/Statements/IgnoreArrayResult/IgnoreArrayResult.qlref

File renamed without changes.

javascript/ql/test/query-tests/Statements/IgnoreConcatReturn/tst.js renamed to javascript/ql/test/query-tests/Statements/IgnoreArrayResult/tst.js

File renamed without changes.

0 commit comments

Comments
 (0)