Skip to content

Commit 6d07e4d

Browse files
authored
Apply codeql query format to regex library files
1 parent b48b22c commit 6d07e4d

3 files changed

Lines changed: 25 additions & 92 deletions

File tree

cpp/ql/lib/semmle/code/cpp/regex/RegexFlowConfigs.qll

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ private import semmle.code.cpp.dataflow.new.TaintTracking
2626
// ---------------------------------------------------------------------------
2727
// std::basic_regex identification
2828
// ---------------------------------------------------------------------------
29-
3029
/**
3130
* A `std::basic_regex` class type (or instantiation thereof, e.g. `std::regex`,
3231
* `std::wregex`).
@@ -91,7 +90,6 @@ private Parameter getStringLikeParameter(Function f) {
9190
// ---------------------------------------------------------------------------
9291
// Match / search / replace / iterator calls
9392
// ---------------------------------------------------------------------------
94-
9593
/**
9694
* A free function in namespace `std` that matches a subject against a regex:
9795
* one of `regex_match`, `regex_search`, or `regex_replace`.
@@ -140,17 +138,14 @@ predicate regexMatchCall(Call call, Expr regexArg, Expr subjectArg) {
140138
subjectArg = call.getArgument(sp.getIndex()) and
141139
// Prefer the earliest such parameter (matches the standard argument
142140
// order for these overloads).
143-
not exists(Parameter sp2 |
144-
sp2 = getStringLikeParameter(f) and sp2.getIndex() < sp.getIndex()
145-
)
141+
not exists(Parameter sp2 | sp2 = getStringLikeParameter(f) and sp2.getIndex() < sp.getIndex())
146142
)
147143
)
148144
}
149145

150146
// ---------------------------------------------------------------------------
151147
// Regex-flow sinks: places where a pattern (a StringLiteral) is used as a regex
152148
// ---------------------------------------------------------------------------
153-
154149
/**
155150
* A regex-flow sink: an expression at which a value is used as the pattern
156151
* for a `std::basic_regex` (construction, `.assign(...)`), or as the pattern
@@ -178,7 +173,6 @@ class RegexPatternSink extends DataFlow::Node {
178173
// ---------------------------------------------------------------------------
179174
// Fast-path: only track literals that look regex-y
180175
// ---------------------------------------------------------------------------
181-
182176
/**
183177
* A string literal that is a plausible ReDoS candidate: it contains at least
184178
* one unbounded-repetition quantifier (`+`, `*`, or `{n,}`). Used as an
@@ -209,7 +203,6 @@ private module RegexPatternFlow = TaintTracking::Global<RegexPatternFlowConfig>;
209203
// ---------------------------------------------------------------------------
210204
// Public predicates
211205
// ---------------------------------------------------------------------------
212-
213206
/**
214207
* Holds if the `StringLiteral` `regex` flows to a modeled `std::regex`
215208
* construction or usage site.
@@ -272,7 +265,6 @@ predicate regexMatchedAgainst(StringLiteral regex, Expr str) {
272265
// ---------------------------------------------------------------------------
273266
// Construction-site flags
274267
// ---------------------------------------------------------------------------
275-
276268
/**
277269
* Holds if `ec` is a `std::regex_constants` enum constant with the given
278270
* unqualified name.
@@ -288,9 +280,7 @@ private predicate regexConstantsEnum(EnumConstant ec, string name) {
288280
* enum constant with the given `name`, possibly through implicit conversions.
289281
*/
290282
private predicate refersToRegexConstant(Expr access, string name) {
291-
exists(EnumConstantAccess eca |
292-
eca = access.getAChild*() or eca = access
293-
|
283+
exists(EnumConstantAccess eca | eca = access.getAChild*() or eca = access |
294284
regexConstantsEnum(eca.getTarget(), name)
295285
)
296286
}
@@ -382,7 +372,6 @@ predicate hasNonEcmaScriptGrammarFlag(StringLiteral regex) {
382372
// ---------------------------------------------------------------------------
383373
// Grammar classification
384374
// ---------------------------------------------------------------------------
385-
386375
/**
387376
* The `std::regex` grammar dialects that the C++ regex parser is aware of.
388377
*

cpp/ql/lib/semmle/code/cpp/regex/RegexTreeView.qll

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ RegExpTerm getParsedRegExp(StringLiteral re) { result.getRegex() = re and result
2727
// ---------------------------------------------------------------------------
2828
// newtype TRegExpParent
2929
// ---------------------------------------------------------------------------
30-
3130
/**
3231
* An element that is either a regex literal (the root of a parse tree) or a
3332
* regex term (a node in the parse tree).
@@ -48,9 +47,7 @@ private newtype TRegExpParent =
4847
/** An alternation (`a|b`). */
4948
TRegExpAlt(RegExp re, int start, int end) {
5049
re.alternation(start, end) and
51-
exists(int part_end |
52-
re.alternationOption(start, end, start, part_end) and part_end < end
53-
) // require at least two alternatives
50+
exists(int part_end | re.alternationOption(start, end, start, part_end) and part_end < end) // require at least two alternatives
5451
} or
5552
/** A character class (`[...]`). */
5653
TRegExpCharacterClass(RegExp re, int start, int end) { re.charSet(start, end) } or
@@ -81,7 +78,6 @@ private newtype TRegExpParent =
8178
// ---------------------------------------------------------------------------
8279
// Helper: sequence children
8380
// ---------------------------------------------------------------------------
84-
8581
pragma[nomagic]
8682
private int seqChildEnd(RegExp re, int start, int end, int i) {
8783
result = seqChild(re, start, end, i).getEnd()
@@ -110,13 +106,11 @@ private RegExpTerm seqChild(RegExp re, int start, int end, int i) {
110106
// ---------------------------------------------------------------------------
111107
// Module Impl (implements RegexTreeViewSig)
112108
// ---------------------------------------------------------------------------
113-
114109
/** An implementation that satisfies the `RegexTreeViewSig` signature. */
115110
module Impl implements RegexTreeViewSig {
116111
// -------------------------------------------------------------------------
117112
// RegExpParent
118113
// -------------------------------------------------------------------------
119-
120114
/**
121115
* An element containing a regular expression term: either the literal itself
122116
* or a term node.
@@ -150,16 +144,13 @@ module Impl implements RegexTreeViewSig {
150144
// -------------------------------------------------------------------------
151145
// RegExpLiteral
152146
// -------------------------------------------------------------------------
153-
154147
/** A string literal used as a regular expression. */
155148
class RegExpLiteral extends TRegExpLiteral, RegExpParent {
156149
RegExp re;
157150

158151
RegExpLiteral() { this = TRegExpLiteral(re) }
159152

160-
override RegExpTerm getChild(int i) {
161-
i = 0 and result.getRegex() = re and result.isRootTerm()
162-
}
153+
override RegExpTerm getChild(int i) { i = 0 and result.getRegex() = re and result.isRootTerm() }
163154

164155
/**
165156
* Holds if dot `.` matches all characters including newlines.
@@ -204,7 +195,6 @@ module Impl implements RegexTreeViewSig {
204195
// -------------------------------------------------------------------------
205196
// RegExpTerm (base class for all parse-tree nodes)
206197
// -------------------------------------------------------------------------
207-
208198
/**
209199
* A regular expression term — a node in the parse tree of a regex literal.
210200
*/
@@ -353,7 +343,6 @@ module Impl implements RegexTreeViewSig {
353343
// -------------------------------------------------------------------------
354344
// Quantifiers
355345
// -------------------------------------------------------------------------
356-
357346
/**
358347
* A quantified regular expression term (`a*`, `a+`, `a?`, `a{n,m}`, etc.).
359348
*/
@@ -436,7 +425,6 @@ module Impl implements RegexTreeViewSig {
436425
// -------------------------------------------------------------------------
437426
// Sequences and alternations
438427
// -------------------------------------------------------------------------
439-
440428
/**
441429
* A sequence term — two or more items in a row.
442430
*
@@ -492,7 +480,6 @@ module Impl implements RegexTreeViewSig {
492480
// -------------------------------------------------------------------------
493481
// Character escapes and normal characters
494482
// -------------------------------------------------------------------------
495-
496483
/**
497484
* A normal character in a regular expression (including escape sequences).
498485
*/
@@ -566,7 +553,6 @@ module Impl implements RegexTreeViewSig {
566553
// -------------------------------------------------------------------------
567554
// Character classes [...]
568555
// -------------------------------------------------------------------------
569-
570556
/**
571557
* A character class in a regular expression (`[a-z]`, `[^0-9]`, etc.).
572558
*/
@@ -712,7 +698,6 @@ module Impl implements RegexTreeViewSig {
712698
// -------------------------------------------------------------------------
713699
// Special characters (`.`, `^`, `$`, `\b`, `\B`)
714700
// -------------------------------------------------------------------------
715-
716701
/**
717702
* A special (meta) character in a regular expression.
718703
*
@@ -801,7 +786,6 @@ module Impl implements RegexTreeViewSig {
801786
// -------------------------------------------------------------------------
802787
// Groups (capturing, non-capturing, named, lookahead/lookbehind)
803788
// -------------------------------------------------------------------------
804-
805789
/**
806790
* A grouped regular expression: `(...)`, `(?:...)`, `(?<name>...)`, or an
807791
* assertion group `(?=...)`, etc.
@@ -840,7 +824,6 @@ module Impl implements RegexTreeViewSig {
840824
// -------------------------------------------------------------------------
841825
// Zero-width matches and sub-patterns (lookahead/lookbehind)
842826
// -------------------------------------------------------------------------
843-
844827
/**
845828
* A zero-width match: an empty group or an assertion.
846829
*/
@@ -905,7 +888,6 @@ module Impl implements RegexTreeViewSig {
905888
// -------------------------------------------------------------------------
906889
// Back-references
907890
// -------------------------------------------------------------------------
908-
909891
/**
910892
* A back-reference: `\1`, `\k<name>`.
911893
*/
@@ -944,7 +926,6 @@ module Impl implements RegexTreeViewSig {
944926
// -------------------------------------------------------------------------
945927
// RegExpConstant
946928
// -------------------------------------------------------------------------
947-
948929
/**
949930
* A constant regular expression term — a sequence of characters that matches
950931
* a fixed string. Currently this is always a single character (or escape
@@ -976,14 +957,12 @@ module Impl implements RegexTreeViewSig {
976957
// -------------------------------------------------------------------------
977958
// Top
978959
// -------------------------------------------------------------------------
979-
980960
/** The common supertype of all regex-related elements. */
981961
class Top = RegExpParent;
982962

983963
// -------------------------------------------------------------------------
984964
// Signature predicates
985965
// -------------------------------------------------------------------------
986-
987966
/**
988967
* Holds if `term` is an escape class (e.g., `\d`), and `clazz` is the
989968
* character identifying the class (e.g., `"d"`).

0 commit comments

Comments
 (0)