Skip to content

Commit 35a63e2

Browse files
committed
Python: Fix bad join in regex::used_as_regex
Since the number of relevant attributes in the `re` module is fairly small, it made sense to factor this out in a separate predicate, and the join order also became more sensible.
1 parent 035e747 commit 35a63e2

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,19 @@ private predicate re_module_function(string name, int flags) {
1919
name = "subn" and flags = 4
2020
}
2121

22+
/**
23+
* Gets the names and corresponding values of attributes of the `re` module that are likely to be
24+
* methods taking regular expressions as arguments.
25+
*
26+
* This is a helper predicate that fixes a bad join order, and should not be inlined without checking
27+
* that this is safe.
28+
*/
29+
pragma[nomagic]
30+
private Value relevant_re_attr(string name) {
31+
result = Module::named("re").attr(name) and
32+
name != "escape"
33+
}
34+
2235
/**
2336
* Holds if `s` is used as a regex with the `re` module, with the regex-mode `mode` (if known).
2437
* If regex mode is not known, `mode` will be `"None"`.
@@ -28,8 +41,7 @@ predicate used_as_regex(Expr s, string mode) {
2841
/* Call to re.xxx(regex, ... [mode]) */
2942
exists(CallNode call, string name |
3043
call.getArg(0).pointsTo(_, _, s.getAFlowNode()) and
31-
call.getFunction().pointsTo(Module::named("re").attr(name)) and
32-
not name = "escape"
44+
call.getFunction().pointsTo(relevant_re_attr(name))
3345
|
3446
mode = "None"
3547
or

0 commit comments

Comments
 (0)