Skip to content

Commit 07ae402

Browse files
committed
Python: Don't allow getParameter(-1) for BoundMethodValue
As per discussion in the PR
1 parent affca1a commit 07ae402

File tree

4 files changed

+13
-8
lines changed

4 files changed

+13
-8
lines changed

python/ql/src/semmle/python/objects/Callables.qll

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -438,10 +438,18 @@ class BoundMethodObjectInternal extends CallableObjectInternal, TBoundMethod {
438438
PointsTo::pointsTo(result.getFunction(), ctx, this, _)
439439
}
440440

441-
override NameNode getParameter(int n) { result = this.getFunction().getParameter(n + 1) }
441+
/** Gets the parameter node that will be used for `self`. */
442+
NameNode getSelfParameter() { result = this.getFunction().getParameter(0) }
443+
444+
override NameNode getParameter(int n) {
445+
result = this.getFunction().getParameter(n + 1) and
446+
// don't return the parameter for `self` at `n = -1`
447+
n >= 0
448+
}
442449

443450
override NameNode getParameterByName(string name) {
444-
result = this.getFunction().getParameterByName(name)
451+
result = this.getFunction().getParameterByName(name) and
452+
not result = this.getSelfParameter()
445453
}
446454

447455
override predicate neverReturns() { this.getFunction().neverReturns() }

python/ql/src/semmle/python/objects/ObjectAPI.qll

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,9 @@ class BoundMethodValue extends CallableValue {
454454
* The value for `o` in `o.func`.
455455
*/
456456
Value getSelf() { result = this.(BoundMethodObjectInternal).getSelf() }
457+
458+
/** Gets the parameter node that will be used for `self`. */
459+
NameNode getSelfParameter() { result = this.(BoundMethodObjectInternal).getSelfParameter() }
457460
}
458461

459462
/**

python/ql/test/library-tests/PointsTo/calls/getParameter.expected

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@
55
| Function f | 1 | ControlFlowNode for arg1 |
66
| Function f | 2 | ControlFlowNode for arg2 |
77
| Method(Function C.n, C()) | 0 | ControlFlowNode for arg1 |
8-
| Method(Function C.n, C()) | -1 | ControlFlowNode for self |
98
| Method(Function C.n, class C) | 0 | ControlFlowNode for arg1 |
10-
| Method(Function C.n, class C) | -1 | ControlFlowNode for self |
119
| Method(Function f, C()) | 0 | ControlFlowNode for arg1 |
1210
| Method(Function f, C()) | 1 | ControlFlowNode for arg2 |
13-
| Method(Function f, C()) | -1 | ControlFlowNode for arg0 |

python/ql/test/library-tests/PointsTo/calls/getParameterByName.expected

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@
55
| Function f | arg1 | ControlFlowNode for arg1 |
66
| Function f | arg2 | ControlFlowNode for arg2 |
77
| Method(Function C.n, C()) | arg1 | ControlFlowNode for arg1 |
8-
| Method(Function C.n, C()) | self | ControlFlowNode for self |
98
| Method(Function C.n, class C) | arg1 | ControlFlowNode for arg1 |
10-
| Method(Function C.n, class C) | self | ControlFlowNode for self |
11-
| Method(Function f, C()) | arg0 | ControlFlowNode for arg0 |
129
| Method(Function f, C()) | arg1 | ControlFlowNode for arg1 |
1310
| Method(Function f, C()) | arg2 | ControlFlowNode for arg2 |

0 commit comments

Comments
 (0)