Skip to content

Commit c58f67b

Browse files
committed
reintroduce performance improvement - but sound this time
1 parent 4392f02 commit c58f67b

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

javascript/ql/src/Performance/ReDoS.ql

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,23 @@ newtype TStatePair =
118118
MkStatePair(State q1, State q2) {
119119
isFork(q1, _, _, _, _) and q2 = q1
120120
or
121-
step(_, _, _, q1, q2)
121+
(step(_, _, _, q1, q2) or step(_, _, _, q2, q1)) and
122+
rankState(q1) <= rankState(q2)
122123
}
123124

125+
/**
126+
* Gets a unique number for a `state`.
127+
* Is used to create an ordering of states, where states with the same `toString()` will be ordered differently.
128+
*/
129+
int rankState(State state) {
130+
state =
131+
rank[result](State s, Location l |
132+
l = s.getRepr().getLocation()
133+
|
134+
s order by l.getStartLine(), l.getStartColumn(), s.toString()
135+
)
136+
}
137+
124138
class StatePair extends TStatePair {
125139
State q1;
126140
State q2;
@@ -190,12 +204,20 @@ predicate isFork(State q, InputSymbol s1, InputSymbol s2, State r1, State r2) {
190204
stateInsideBacktracking(r2)
191205
}
192206

207+
/**
208+
* Gets the state pair `(q1, q2)` or `(q2, q1)`; note that only
209+
* one or the other is defined.
210+
*/
211+
StatePair mkStatePair(State q1, State q2) {
212+
result = MkStatePair(q1, q2) or result = MkStatePair(q2, q1)
213+
}
214+
193215
/**
194216
* Holds if there are transitions from the components of `q` to the corresponding
195217
* components of `r` labelled with `s1` and `s2`, respectively.
196218
*/
197219
predicate step(StatePair q, InputSymbol s1, InputSymbol s2, StatePair r) {
198-
exists(State r1, State r2 | step(q, s1, s2, r1, r2) and r = MkStatePair(r1, r2))
220+
exists(State r1, State r2 | step(q, s1, s2, r1, r2) and r = mkStatePair(r1, r2))
199221
}
200222

201223
/**

0 commit comments

Comments
 (0)