Skip to content

Commit 460de3f

Browse files
Reduce generality of map and zip for performance
1 parent 4e36008 commit 460de3f

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

python/ql/lib/semmle/python/frameworks/Stdlib.qll

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4540,8 +4540,11 @@ module StdlibPrivate {
45404540
or
45414541
input = "Argument[" + (i + 1).toString() + "].SetElement"
45424542
or
4543+
// We reduce generality slightly by not tracking tuple contents on list arguments beyond the first, for performance.
4544+
// TODO: Once we have TupleElementAny, this generality can be increased.
4545+
i = 0 and
45434546
exists(DataFlow::TupleElementContent tc, int j | j = tc.getIndex() |
4544-
input = "Argument[" + (i + 1).toString() + "].TupleElement[" + j.toString() + "]"
4547+
input = "Argument[1].TupleElement[" + j.toString() + "]"
45454548
)
45464549
// TODO: Once we have DictKeyContent, we need to transform that into ListElementContent
45474550
) and
@@ -4624,6 +4627,9 @@ module StdlibPrivate {
46244627
or
46254628
input = "Argument[" + i.toString() + "].SetElement"
46264629
or
4630+
// We reduce generality slightly by not tracking tuple contents on arguments beyond the first two, for performance.
4631+
// TODO: Once we have TupleElementAny, this generality can be increased.
4632+
i in [0 .. 1] and
46274633
exists(DataFlow::TupleElementContent tc, int j | j = tc.getIndex() |
46284634
input = "Argument[" + i.toString() + "].TupleElement[" + j.toString() + "]"
46294635
)

python/ql/test/library-tests/dataflow/coverage/test_builtins.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,7 @@ def f(p1,p2):
414414
SINK(rl[0][0]) #$ flow="SOURCE, l:-10 -> rl[0][0]"
415415
SINK_F(rl[0][1])
416416

417+
417418
@expects(4)
418419
def test_map_dict():
419420
d1 = {SOURCE: "v1"}
@@ -429,6 +430,35 @@ def f(p1,p2):
429430
SINK(rl[0][0]) #$ MISSING: flow="SOURCE, l:-10 -> rl[0][0]"
430431
SINK_F(rl[0][1])
431432

433+
@expects(6)
434+
def test_map_multi_list():
435+
l1 = [SOURCE]
436+
l2 = [SOURCE]
437+
438+
def f(p1,p2):
439+
SINK(p1) #$ flow="SOURCE, l:-4 -> p1"
440+
SINK(p2) #$ flow="SOURCE, l:-4 -> p2"
441+
442+
return p1,p2
443+
444+
rl = list(map(f, l1, l2))
445+
SINK(rl[0][0]) #$ flow="SOURCE, l:-10 -> rl[0][0]"
446+
SINK(rl[0][1]) #$ flow="SOURCE, l:-10 -> rl[0][1]"
447+
448+
@expects(6)
449+
def test_map_multi_tuple():
450+
l1 = (SOURCE,)
451+
l2 = (SOURCE,)
452+
453+
def f(p1,p2):
454+
SINK(p1) #$ flow="SOURCE, l:-4 -> p1"
455+
SINK(p2) #$ MISSING: flow="SOURCE, l:-4 -> p2" # Tuples are not tracked beyond the first list argument for performance.
456+
return p1,p2
457+
458+
rl = list(map(f, l1, l2))
459+
SINK(rl[0][0]) #$ flow="SOURCE, l:-10 -> rl[0][0]"
460+
SINK(rl[0][1]) #$ MISSING: flow="SOURCE, l:-10 -> rl[0][1]"
461+
432462
### filter
433463

434464
@expects(2)
@@ -474,3 +504,11 @@ def f(p):
474504

475505
rl = list(filter(f,d))
476506
SINK(rl[0]) #$ MISSING: flow="SOURCE, l:-7 -> rl[0]"
507+
508+
def test_enumerate_list():
509+
# TODO
510+
pass
511+
512+
def test_zip_list():
513+
# TODO
514+
pass

0 commit comments

Comments
 (0)