diff options
author | Armin Rigo <arigo@tunes.org> | 2004-10-28 16:32:00 (GMT) |
---|---|---|
committer | Armin Rigo <arigo@tunes.org> | 2004-10-28 16:32:00 (GMT) |
commit | 89a39461bff04b80bb4857790350e1ab30ff2df9 (patch) | |
tree | 54bc00a9ad30e8e49849874cfbca8543de62fa58 /Objects/funcobject.c | |
parent | 063e1e846dc5c3fe593cef5b14cc429369dcd2c2 (diff) | |
download | cpython-89a39461bff04b80bb4857790350e1ab30ff2df9.zip cpython-89a39461bff04b80bb4857790350e1ab30ff2df9.tar.gz cpython-89a39461bff04b80bb4857790350e1ab30ff2df9.tar.bz2 |
Wrote down the invariants of some common objects whose structure is
exposed in header files. Fixed a few comments in these headers.
As we might have expected, writing down invariants systematically exposed a
(minor) bug. In this case, function objects have a writeable func_code
attribute, which could be set to code objects with the wrong number of
free variables. Calling the resulting function segfaulted the interpreter.
Added a corresponding test.
Diffstat (limited to 'Objects/funcobject.c')
-rw-r--r-- | Objects/funcobject.c | 12 |
1 files changed, 12 insertions, 0 deletions
diff --git a/Objects/funcobject.c b/Objects/funcobject.c index c46887c..c7f7c9d 100644 --- a/Objects/funcobject.c +++ b/Objects/funcobject.c @@ -230,6 +230,7 @@ static int func_set_code(PyFunctionObject *op, PyObject *value) { PyObject *tmp; + int nfree, nclosure; if (restricted()) return -1; @@ -240,6 +241,17 @@ func_set_code(PyFunctionObject *op, PyObject *value) "func_code must be set to a code object"); return -1; } + nfree = PyCode_GetNumFree((PyCodeObject *)value); + nclosure = (op->func_closure == NULL ? 0 : + PyTuple_GET_SIZE(op->func_closure)); + if (nclosure != nfree) { + PyErr_Format(PyExc_ValueError, + "%s() requires a code object with %d free vars," + " not %d", + PyString_AsString(op->func_name), + nclosure, nfree); + return -1; + } tmp = op->func_code; Py_INCREF(value); op->func_code = value; |