Skip to content

Commit 2d8f3bb

Browse files
committed
Python: Use TUnknown instead of TUnknownInstance.
1 parent 4b5a20d commit 2d8f3bb

File tree

1 file changed

+47
-29
lines changed

1 file changed

+47
-29
lines changed

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

Lines changed: 47 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,10 @@ class BuiltinFunctionObjectInternal extends CallableObjectInternal, TBuiltinFunc
215215
cls = ObjectInternal::builtin("bool") and obj = ObjectInternal::bool(_)
216216
) and
217217
origin = CfgOrigin::unknown()
218+
or
219+
this.returnTypeUnknown() and
220+
obj = ObjectInternal::unknown() and
221+
origin = CfgOrigin::unknown()
218222
}
219223

220224
override ControlFlowNode getOrigin() {
@@ -231,27 +235,15 @@ class BuiltinFunctionObjectInternal extends CallableObjectInternal, TBuiltinFunc
231235

232236
Builtin getReturnType() {
233237
exists(Builtin func |
234-
func = this.getBuiltin() |
235-
/* Enumerate the types of a few builtin functions, that the CPython analysis misses.
236-
*/
237-
func = Builtin::builtin("hex") and result = Builtin::special("str")
238-
or
239-
func = Builtin::builtin("oct") and result = Builtin::special("str")
240-
or
241-
func = Builtin::builtin("intern") and result = Builtin::special("str")
242-
or
243-
func = Builtin::builtin("__import__") and result = Builtin::special("ModuleType")
244-
or
245-
/* Fix a few minor inaccuracies in the CPython analysis */
246-
ext_rettype(func, result) and not (
247-
func = Builtin::builtin("__import__")
248-
or
249-
func = Builtin::builtin("compile") and result = Builtin::special("NoneType")
250-
or
251-
func = Builtin::builtin("sum")
252-
or
253-
func = Builtin::builtin("filter")
254-
)
238+
func = this.getBuiltin() and
239+
result = getBuiltinFunctionReturnType(func)
240+
)
241+
}
242+
243+
private predicate returnTypeUnknown() {
244+
exists(Builtin func |
245+
func = this.getBuiltin() and
246+
not exists(getBuiltinFunctionReturnType(func))
255247
)
256248
}
257249

@@ -294,6 +286,29 @@ class BuiltinFunctionObjectInternal extends CallableObjectInternal, TBuiltinFunc
294286

295287
}
296288

289+
private Builtin getBuiltinFunctionReturnType(Builtin func) {
290+
/* Enumerate the types of a few builtin functions, that the CPython analysis misses. */
291+
func = Builtin::builtin("hex") and result = Builtin::special("str")
292+
or
293+
func = Builtin::builtin("oct") and result = Builtin::special("str")
294+
or
295+
func = Builtin::builtin("intern") and result = Builtin::special("str")
296+
or
297+
func = Builtin::builtin("__import__") and result = Builtin::special("ModuleType")
298+
or
299+
/* Fix a few minor inaccuracies in the CPython analysis */
300+
ext_rettype(func, result) and not (
301+
func = Builtin::builtin("__import__")
302+
or
303+
func = Builtin::builtin("compile") and result = Builtin::special("NoneType")
304+
or
305+
func = Builtin::builtin("sum")
306+
or
307+
func = Builtin::builtin("filter")
308+
)
309+
}
310+
311+
297312
/** Class representing methods of built-in classes (otherwise known as method-descriptors) such as `list.append`.
298313
*/
299314
class BuiltinMethodObjectInternal extends CallableObjectInternal, TBuiltinMethodObject {
@@ -330,6 +345,10 @@ class BuiltinMethodObjectInternal extends CallableObjectInternal, TBuiltinMethod
330345
cls = ObjectInternal::builtin("bool") and obj = ObjectInternal::bool(_)
331346
) and
332347
origin = CfgOrigin::unknown()
348+
or
349+
this.returnTypeUnknown() and
350+
obj = ObjectInternal::unknown() and
351+
origin = CfgOrigin::unknown()
333352
}
334353

335354
Builtin getReturnType() {
@@ -338,14 +357,13 @@ class BuiltinMethodObjectInternal extends CallableObjectInternal, TBuiltinMethod
338357
func = this.getBuiltin() |
339358
ext_rettype(func, result)
340359
)
341-
or
342-
/* Otherwise, if no such record exists, use `object` as the return type. */
343-
not exists(Builtin func |
344-
// We cannot do `this.getBuiltin()` here, as that would introduce negative recursion.
345-
// Instead, we appeal directly to the underlying IPA type.
346-
this = TBuiltinMethodObject(func) and
347-
ext_rettype(func, _))
348-
and result = Builtin::builtin("object")
360+
}
361+
362+
private predicate returnTypeUnknown() {
363+
exists(Builtin func |
364+
func = this.getBuiltin() |
365+
not ext_rettype(func, _)
366+
)
349367
}
350368

351369
override ControlFlowNode getOrigin() {

0 commit comments

Comments
 (0)