Skip to content

Commit 66d66fe

Browse files
committed
JS: fix false positives for splice with conditional index decrement
1 parent 7292a76 commit 66d66fe

File tree

3 files changed

+19
-5
lines changed

3 files changed

+19
-5
lines changed

javascript/ql/src/Statements/LoopIterationSkippedDueToShifting.ql

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,12 @@ class ArrayIterationLoop extends ForStmt {
146146
or
147147
this.hasPathThrough(splice, cfg.getAPredecessor()) and
148148
this.getLoopEntry().dominates(cfg.getBasicBlock()) and
149-
not this.hasIndexingManipulation(cfg)
149+
not this.hasIndexingManipulation(cfg) and
150+
// Don't continue through a branch that tests the splice call's return value
151+
not exists(ConditionGuardNode guard | cfg = guard |
152+
guard.getTest() = splice.asExpr() and
153+
guard.getOutcome() = false
154+
)
150155
}
151156
}
152157

javascript/ql/test/query-tests/Statements/LoopIterationSkippedDueToShifting/LoopIterationSkippedDueToShifting.expected

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,4 @@
22
| tst.js:13:29:13:46 | parts.splice(i, 1) | Removing an array item without adjusting the loop index 'i' causes the subsequent array item to be skipped. |
33
| tst.js:24:9:24:26 | parts.splice(i, 1) | Removing an array item without adjusting the loop index 'i' causes the subsequent array item to be skipped. |
44
| tst.js:128:11:128:33 | pending ... e(i, 1) | Removing an array item without adjusting the loop index 'i' causes the subsequent array item to be skipped. |
5-
| tst.js:136:32:136:47 | toc.splice(i, 1) | Removing an array item without adjusting the loop index 'i' causes the subsequent array item to be skipped. |
6-
| tst.js:143:10:143:25 | toc.splice(i, 1) | Removing an array item without adjusting the loop index 'i' causes the subsequent array item to be skipped. |
5+
| tst.js:153:11:153:26 | toc.splice(i, 1) | Removing an array item without adjusting the loop index 'i' causes the subsequent array item to be skipped. |

javascript/ql/test/query-tests/Statements/LoopIterationSkippedDueToShifting/tst.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,16 +133,26 @@ function withTryCatch(pendingCSS) {
133133

134134
function andOperand(toc) {
135135
for (let i = 0; i < toc.length; i++) {
136-
toc[i].ignoreSubHeading && toc.splice(i, 1) && i--; // $ SPURIOUS:Alert
136+
toc[i].ignoreSubHeading && toc.splice(i, 1) && i--;
137137
}
138138
}
139139

140140
function ifStatement(toc) {
141141
for (let i = 0; i < toc.length; i++) {
142142
if(toc[i].ignoreSubHeading){
143-
if(toc.splice(i, 1)){ // $ SPURIOUS:Alert
143+
if(toc.splice(i, 1)){
144144
i--;
145145
}
146146
}
147147
}
148148
}
149+
150+
function ifStatement2(toc) {
151+
for (let i = 0; i < toc.length; i++) {
152+
if(toc[i].ignoreSubHeading){
153+
if(!toc.splice(i, 1)){ // $Alert
154+
i--;
155+
}
156+
}
157+
}
158+
}

0 commit comments

Comments
 (0)