Skip to content

Commit c6ae06f

Browse files
committed
Python: modernize regex library to use new points-to.
1 parent 54a8c64 commit c6ae06f

File tree

2 files changed

+13
-13
lines changed

2 files changed

+13
-13
lines changed

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

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import python
2+
import semmle.python.objects.ObjectInternal
23

34
private predicate re_module_function(string name, int flags) {
45
name = "compile" and flags = 1 or
@@ -14,44 +15,42 @@ private predicate re_module_function(string name, int flags) {
1415
predicate used_as_regex(Expr s, string mode) {
1516
(s instanceof Bytes or s instanceof Unicode)
1617
and
17-
exists(ModuleObject re | re.getName() = "re" |
18+
exists(ModuleValue re | re.getName() = "re" |
1819
/* Call to re.xxx(regex, ... [mode]) */
1920
exists(CallNode call, string name |
2021
call.getArg(0).refersTo(_, _, s.getAFlowNode()) and
21-
call.getFunction().refersTo(re.attr(name)) |
22+
call.getFunction().pointsTo(re.attr(name)) |
2223
mode = "None"
2324
or
24-
exists(Object obj |
25+
exists(Value obj |
2526
mode = mode_from_mode_object(obj) |
2627
exists(int flags_arg |
2728
re_module_function(name, flags_arg) and
28-
call.getArg(flags_arg).refersTo(obj)
29+
call.getArg(flags_arg).pointsTo(obj)
2930
)
3031
or
31-
call.getArgByName("flags").refersTo(obj)
32+
call.getArgByName("flags").pointsTo(obj)
3233
)
3334
)
3435
)
3536
}
3637

37-
string mode_from_mode_object(Object obj) {
38+
string mode_from_mode_object(Value obj) {
3839
(
3940
result = "DEBUG" or result = "IGNORECASE" or result = "LOCALE" or
4041
result = "MULTILINE" or result = "DOTALL" or result = "UNICODE" or
4142
result = "VERBOSE"
4243
) and
43-
obj = ModuleObject::named("sre_constants").attr("SRE_FLAG_" + result)
44-
or
45-
exists(BinaryExpr be, Object sub | obj.getOrigin() = be |
46-
be.getOp() instanceof BitOr and
47-
be.getASubExpression().refersTo(sub) and
48-
result = mode_from_mode_object(sub)
44+
exists(int flag |
45+
flag = Value::named("sre_constants.SRE_FLAG_" + result).(ObjectInternal).intValue()
46+
and
47+
obj.(ObjectInternal).intValue().bitAnd(flag) = flag
4948
)
5049
}
5150

5251
/** A StrConst used as a regular expression */
5352
abstract class RegexString extends Expr {
54-
53+
5554
RegexString() {
5655
(this instanceof Bytes or this instanceof Unicode)
5756
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
semmle-extractor-options: --max-import-depth=3
2+
optimize: true

0 commit comments

Comments
 (0)