-
-
Notifications
You must be signed in to change notification settings - Fork 33.4k
Open
Labels
interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)
Description
The problem with PyStackRef_AsPyObjectBorrow is that it is not clear whether the reference is owned by the stack ref or the pointer.
Borrowing references across calls is fine but in less structured code, it is error prone and very hard to analyze.
The solution is change most, ideally all, uses of PyStackRef_AsPyObjectBorrow to PyStackRef_AsPyObjectSteal so that the ownership of the reference is clear.
E.g.
inst(UNARY_NEGATIVE, (value -- res)) {
PyObject *val_o = PyStackRef_AsPyObjectBorrow(value);
PyObject *res_o = PyNumber_Negative(val_o);
PyStackRef_CLOSE(value);
ERROR_IF(res_o == NULL, error);
res = PyStackRef_FromPyObjectSteal(res_o);
}
would become:
inst(UNARY_NEGATIVE, (value -- res)) {
PyObject *val_o = PyStackRef_AsPyObjectSteal(value);
PyObject *res_o = PyNumber_Negative(val_o);
Py_DECREF(val_o);
ERROR_IF(res_o == NULL, error);
res = PyStackRef_FromPyObjectSteal(res_o);
}
This ensures that during the escaping call to PyNumber_Negative, the reference to the value a PyObject *, so will not be reclaimed by the garbage collector.
Linked PRs
Metadata
Metadata
Assignees
Labels
interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)