Skip to content

Commit 6639e5a

Browse files
authored
Merge pull request github#12590 from yoff/python/patch-uninitialized-local
Python: Patch uninitialized local query
2 parents 17c9ba9 + ed15cce commit 6639e5a

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

python/ql/src/Variables/UninitializedLocal.ql

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ import Undefined
1515
import semmle.python.pointsto.PointsTo
1616

1717
predicate uninitialized_local(NameNode use) {
18-
exists(FastLocalVariable local | use.uses(local) or use.deletes(local) | not local.escapes()) and
18+
exists(FastLocalVariable local | use.uses(local) or use.deletes(local) |
19+
not local.escapes() and not local = any(Nonlocal nl).getAVariable()
20+
) and
1921
(
2022
any(Uninitialized uninit).taints(use) and
2123
PointsToInternal::reachableBlock(use.getBasicBlock(), _)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: fix
3+
---
4+
* Nonlocal variables are excluded from alerts.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/python
2+
3+
def topLevel():
4+
foo = 3
5+
6+
def bar():
7+
nonlocal foo
8+
print(foo)
9+
foo = 4
10+
11+
bar()
12+
print(foo)
13+
14+
topLevel()

0 commit comments

Comments
 (0)