Skip to content

Commit dd0ce1c

Browse files
authored
Merge pull request github#2942 from RasmusWL/pyhton-improve-regex-docs
Python: Add a bit of regex docs
2 parents bd6c23d + 5439059 commit dd0ce1c

File tree

1 file changed

+17
-15
lines changed

1 file changed

+17
-15
lines changed

python/ql/src/semmle/python/regex.qll

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,27 @@ private predicate re_module_function(string name, int flags) {
1212
name = "subn" and flags = 4
1313
}
1414

15+
/**
16+
* Holds if `s` is used as a regex with the `re` module, with the regex-mode `mode` (if known).
17+
* If regex mode is not known, `mode` will be `"None"`.
18+
*/
1519
predicate used_as_regex(Expr s, string mode) {
1620
(s instanceof Bytes or s instanceof Unicode)
1721
and
18-
exists(ModuleValue re | re.getName() = "re" |
19-
/* Call to re.xxx(regex, ... [mode]) */
20-
exists(CallNode call, string name |
21-
call.getArg(0).refersTo(_, _, s.getAFlowNode()) and
22-
call.getFunction().pointsTo(re.attr(name)) |
23-
mode = "None"
24-
or
25-
exists(Value obj |
26-
mode = mode_from_mode_object(obj) |
27-
exists(int flags_arg |
28-
re_module_function(name, flags_arg) and
29-
call.getArg(flags_arg).pointsTo(obj)
30-
)
31-
or
32-
call.getArgByName("flags").pointsTo(obj)
22+
/* Call to re.xxx(regex, ... [mode]) */
23+
exists(CallNode call, string name |
24+
call.getArg(0).refersTo(_, _, s.getAFlowNode()) and
25+
call.getFunction().pointsTo(Module::named("re").attr(name)) |
26+
mode = "None"
27+
or
28+
exists(Value obj |
29+
mode = mode_from_mode_object(obj) |
30+
exists(int flags_arg |
31+
re_module_function(name, flags_arg) and
32+
call.getArg(flags_arg).pointsTo(obj)
3333
)
34+
or
35+
call.getArgByName("flags").pointsTo(obj)
3436
)
3537
)
3638
}

0 commit comments

Comments
 (0)