Skip to content

Commit 8a9587f

Browse files
committed
JS: fix RegExp::getSuccessor/getPredecessor for sequence end/starts
1 parent c5ee436 commit 8a9587f

File tree

3 files changed

+30
-23
lines changed

3 files changed

+30
-23
lines changed

javascript/ql/src/semmle/javascript/Regexp.qll

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -75,26 +75,41 @@ class RegExpTerm extends Locatable, @regexpterm {
7575

7676
/** Gets the regular expression term that is matched (textually) before this one, if any. */
7777
RegExpTerm getPredecessor() {
78-
exists(RegExpSequence seq, int i |
79-
seq.getChild(i) = this and
80-
seq.getChild(i - 1) = result
78+
exists(RegExpTerm parent | parent = getParent() |
79+
if parent instanceof RegExpSequence
80+
then
81+
exists(RegExpSequence seq, int i |
82+
seq = parent and
83+
seq.getChild(i) = this
84+
|
85+
seq.getChild(i - 1) = result
86+
or
87+
i = 0 and result = seq.getPredecessor()
88+
)
89+
else (
90+
not parent instanceof RegExpSubPattern and
91+
result = parent.getPredecessor()
92+
)
8193
)
82-
or
83-
result = getParent().(RegExpTerm).getPredecessor()
8494
}
8595

8696
/** Gets the regular expression term that is matched (textually) after this one, if any. */
8797
RegExpTerm getSuccessor() {
88-
exists(RegExpSequence seq, int i |
89-
seq.getChild(i) = this and
90-
seq.getChild(i + 1) = result
91-
)
92-
or
93-
exists(RegExpTerm parent |
94-
parent = getParent() and
95-
not parent instanceof RegExpSubPattern
96-
|
97-
result = parent.getSuccessor()
98+
exists(RegExpTerm parent | parent = getParent() |
99+
if parent instanceof RegExpSequence
100+
then
101+
exists(RegExpSequence seq, int i |
102+
seq = parent and
103+
seq.getChild(i) = this
104+
|
105+
seq.getChild(i + 1) = result
106+
or
107+
i = seq.getNumChild() - 1 and result = seq.getSuccessor()
108+
)
109+
else (
110+
not parent instanceof RegExpSubPattern and
111+
result = parent.getSuccessor()
112+
)
98113
)
99114
}
100115

javascript/ql/test/library-tests/RegExp/predecessors_and_successors/getPredecessor.expected

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
| tst.js:5:2:5:2 | a | tst.js:5:3:5:8 | (b[c]) |
88
| tst.js:5:2:5:2 | a | tst.js:5:4:5:4 | b |
99
| tst.js:5:2:5:2 | a | tst.js:5:4:5:7 | b[c] |
10-
| tst.js:5:2:5:2 | a | tst.js:5:5:5:7 | [c] |
11-
| tst.js:5:2:5:2 | a | tst.js:5:6:5:6 | c |
1210
| tst.js:5:4:5:4 | b | tst.js:5:5:5:7 | [c] |
1311
| tst.js:5:4:5:4 | b | tst.js:5:6:5:6 | c |
1412
| tst.js:6:2:6:2 | a | tst.js:6:3:6:9 | (b\|[b]) |
@@ -20,8 +18,6 @@
2018
| tst.js:7:2:7:2 | a | tst.js:7:4:7:6 | [b] |
2119
| tst.js:7:2:7:2 | a | tst.js:7:4:7:9 | [b][c] |
2220
| tst.js:7:2:7:2 | a | tst.js:7:5:7:5 | b |
23-
| tst.js:7:2:7:2 | a | tst.js:7:7:7:9 | [c] |
24-
| tst.js:7:2:7:2 | a | tst.js:7:8:7:8 | c |
2521
| tst.js:7:4:7:6 | [b] | tst.js:7:7:7:9 | [c] |
2622
| tst.js:7:4:7:6 | [b] | tst.js:7:8:7:8 | c |
2723
| tst.js:8:2:8:2 | a | tst.js:8:3:8:13 | (b\|[b]\|[b]) |

javascript/ql/test/library-tests/RegExp/predecessors_and_successors/getSuccessor.expected

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,13 @@
1616
| tst.js:13:3:13:4 | ab | tst.js:13:6:13:6 | c |
1717
| tst.js:14:2:14:7 | ([a]b) | tst.js:14:8:14:8 | c |
1818
| tst.js:14:3:14:5 | [a] | tst.js:14:6:14:6 | b |
19-
| tst.js:14:3:14:5 | [a] | tst.js:14:8:14:8 | c |
2019
| tst.js:14:3:14:6 | [a]b | tst.js:14:8:14:8 | c |
2120
| tst.js:14:4:14:4 | a | tst.js:14:6:14:6 | b |
22-
| tst.js:14:4:14:4 | a | tst.js:14:8:14:8 | c |
2321
| tst.js:14:6:14:6 | b | tst.js:14:8:14:8 | c |
2422
| tst.js:16:2:16:9 | ([a][b]) | tst.js:16:10:16:10 | c |
2523
| tst.js:16:3:16:5 | [a] | tst.js:16:6:16:8 | [b] |
26-
| tst.js:16:3:16:5 | [a] | tst.js:16:10:16:10 | c |
2724
| tst.js:16:3:16:8 | [a][b] | tst.js:16:10:16:10 | c |
2825
| tst.js:16:4:16:4 | a | tst.js:16:6:16:8 | [b] |
29-
| tst.js:16:4:16:4 | a | tst.js:16:10:16:10 | c |
3026
| tst.js:16:6:16:8 | [b] | tst.js:16:10:16:10 | c |
3127
| tst.js:16:7:16:7 | b | tst.js:16:10:16:10 | c |
3228
| tst.js:17:2:17:12 | ([a]\|[a]\|a) | tst.js:17:13:17:13 | b |

0 commit comments

Comments
 (0)