Skip to content

Commit 73f2770

Browse files
Fix handling for some wrappers + add test case
1 parent f3a5608 commit 73f2770

File tree

3 files changed

+14
-6
lines changed

3 files changed

+14
-6
lines changed

python/ql/src/Functions/IterReturnsNonSelf.ql

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ predicate iterWrapperMethods(Function iter, Function next) {
5050
exists(string field |
5151
exists(Return r, DataFlow::Node self, DataFlow::AttrRead read |
5252
r.getScope() = iter and
53-
r.getValue() = iterCall(read).asExpr() and
53+
r.getValue() = [iterCall(read).asExpr(), read.asExpr()] and
5454
read.accesses(self, field) and
5555
isSelfVar(iter, self.asExpr())
5656
) and
@@ -63,15 +63,13 @@ predicate iterWrapperMethods(Function iter, Function next) {
6363
)
6464
}
6565

66-
/** Gets a call to `iter(arg)`, `arg.__iter__()`, or `arg` itself (which we assume may already be an iterator). */
66+
/** Gets a call to `iter(arg)` or `arg.__iter__()`. */
6767
private DataFlow::CallCfgNode iterCall(DataFlow::Node arg) {
6868
result.(DataFlow::MethodCallNode).calls(arg, "__iter__")
6969
or
7070
result = API::builtin("iter").getACall() and
7171
arg = result.getArg(0) and
7272
not exists(result.getArg(1))
73-
or
74-
result = arg // assume the wrapping field is already an iterator
7573
}
7674

7775
/** Gets a call to `next(arg)` or `arg.__next__()`. */
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
| test.py:5:5:5:23 | Function __iter__ | Iter method of iterator $@ does not return `self`. | test.py:1:1:1:11 | Class Bad1 | Bad1 |
2-
| test.py:41:5:41:23 | Function __iter__ | Iter method of iterator $@ does not return `self`. | test.py:32:1:32:21 | Class FalsePositive1 | FalsePositive1 |
2+
| test.py:51:5:51:23 | Function __iter__ | Iter method of iterator $@ does not return `self`. | test.py:42:1:42:21 | Class FalsePositive1 | FalsePositive1 |

python/ql/test/query-tests/Functions/iterators/test.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,18 @@ def __next__(self):
2121

2222
def __iter__(self): # GOOD: iter and next are wrappers around a field
2323
return self._it.__iter__()
24-
24+
2525
class Good3:
26+
def __init__(self):
27+
self._it = iter([0,0,0])
28+
29+
def __next__(self):
30+
return self._it.__next__()
31+
32+
def __iter__(self): # GOOD: iter and next are wrappers around a field
33+
return self._it
34+
35+
class Good4:
2636
def __next__(self):
2737
return 0
2838

0 commit comments

Comments
 (0)