Skip to content

Commit 3956a1f

Browse files
committed
Python: Move min/maxParameter methods to Function
These seem generally useful outside of points-to, and so it might be better to add them to the `Function` class instead. I took the liberty of renaming these to say `Arguments` rather than `Parameters`, as this is more in line with the nomenclature that we're using elsewhere. (The internal points-to methods retain the old names.) I'm somewhat ambivalent about the behaviour of `getMaxParameters` on functions with `*varargs`. The hard-coded `INT_MAX` return value is somewhat awkward, but the alternative (to only have the predicate defined when a specific maximum exists) seems like it would potentially cause a lot of headaches.
1 parent 9458f07 commit 3956a1f

File tree

2 files changed

+14
-14
lines changed

2 files changed

+14
-14
lines changed

python/ql/lib/semmle/python/Function.qll

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,18 @@ class Function extends Function_, Scope, AstNode {
163163
ret.getValue() = result.getNode()
164164
)
165165
}
166+
167+
/** Gets the minimum number of positional arguments that can be correctly passed to this function. */
168+
int getMinArguments() {
169+
result = count(this.getAnArg()) - count(this.getDefinition().getArgs().getADefault())
170+
}
171+
172+
/** Gets the maximum number of positional arguments that can be correctly passed to this function. */
173+
int getMaxArguments() {
174+
if exists(this.getVararg())
175+
then result = 2147483647 // INT_MAX
176+
else result = count(this.getAnArg())
177+
}
166178
}
167179

168180
/** A def statement. Note that FunctionDef extends Assign as a function definition binds the newly created function */

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

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -738,21 +738,9 @@ class PythonFunctionValue extends FunctionValue {
738738
else result = "function " + this.getQualifiedName()
739739
}
740740

741-
override int minParameters() {
742-
exists(Function f |
743-
f = this.getScope() and
744-
result = count(f.getAnArg()) - count(f.getDefinition().getArgs().getADefault())
745-
)
746-
}
741+
override int minParameters() { result = this.getScope().getMinArguments() }
747742

748-
override int maxParameters() {
749-
exists(Function f |
750-
f = this.getScope() and
751-
if exists(f.getVararg())
752-
then result = 2147483647 // INT_MAX
753-
else result = count(f.getAnArg())
754-
)
755-
}
743+
override int maxParameters() { result = this.getScope().getMaxArguments() }
756744

757745
/** Gets a control flow node corresponding to a return statement in this function */
758746
ControlFlowNode getAReturnedNode() { result = this.getScope().getAReturnValueFlowNode() }

0 commit comments

Comments
 (0)