Skip to content

Commit 659cc81

Browse files
author
Max Schaefer
committed
JavaScript: Rephrase two predicates to help the optimiser.
1 parent db3eaa2 commit 659cc81

2 files changed

Lines changed: 25 additions & 11 deletions

File tree

javascript/ql/src/Security/CWE-116/IncompleteSanitization.ql

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,19 @@ predicate isSimple(RegExpTerm t) {
3232
or
3333
isSimple(t.(RegExpGroup).getAChild())
3434
or
35-
(
36-
t instanceof RegExpAlt
37-
or
38-
t instanceof RegExpCharacterClass and not t.(RegExpCharacterClass).isInverted()
39-
) and
35+
isSimpleCharacterClass(t)
36+
or
37+
isSimpleAlt(t)
38+
}
39+
40+
/** Holds if `t` is a non-inverted character class that contains no ranges. */
41+
predicate isSimpleCharacterClass(RegExpCharacterClass t) {
42+
not t.isInverted() and
43+
forall(RegExpTerm ch | ch = t.getAChild() | isSimple(ch))
44+
}
45+
46+
/** Holds if `t` is an alternation of simple terms. */
47+
predicate isSimpleAlt(RegExpAlt t) {
4048
forall(RegExpTerm ch | ch = t.getAChild() | isSimple(ch))
4149
}
4250

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -311,13 +311,19 @@ class RegExpSequence extends RegExpTerm, @regexp_seq {
311311
forall(RegExpTerm child | child = getAChild() | child.isNullable())
312312
}
313313

314-
language[monotonicAggregates]
315314
override string getConstantValue() {
316-
// note: due to use of monotonic aggregates, this `strictconcat` will fail if
317-
// `getConstantValue` is undefined for any child
318-
result = strictconcat(RegExpTerm ch, int i | ch = getChild(i) |
319-
ch.getConstantValue() order by i
320-
)
315+
result = getConstantValue(0)
316+
}
317+
318+
/**
319+
* Gets the single string matched by the `i`th child and all following children of
320+
* this sequence, if any.
321+
*/
322+
private string getConstantValue(int i) {
323+
i = getNumChild() and
324+
result = ""
325+
or
326+
result = getChild(i).getConstantValue() + getConstantValue(i+1)
321327
}
322328
}
323329

0 commit comments

Comments
 (0)