diff options
author | Martin Panter <vadmium+py@gmail.com> | 2015-11-07 02:56:11 (GMT) |
---|---|---|
committer | Martin Panter <vadmium+py@gmail.com> | 2015-11-07 02:56:11 (GMT) |
commit | 61d6e4ae9db80e3f87104a03499ff89d3c275b22 (patch) | |
tree | 43160e9ff162635bb549ad31b7be875935cf0b86 /Objects/abstract.c | |
parent | 9b566c324d2eb3ddbf00d5d7a78bea63cde4d15f (diff) | |
parent | eeb896c4116dd763efea45cb3c1b53257128f4e4 (diff) | |
download | cpython-61d6e4ae9db80e3f87104a03499ff89d3c275b22.zip cpython-61d6e4ae9db80e3f87104a03499ff89d3c275b22.tar.gz cpython-61d6e4ae9db80e3f87104a03499ff89d3c275b22.tar.bz2 |
Issue #24802: Merge null termination fixes from 3.4 into 3.5
Diffstat (limited to 'Objects/abstract.c')
-rw-r--r-- | Objects/abstract.c | 22 |
1 files changed, 20 insertions, 2 deletions
diff --git a/Objects/abstract.c b/Objects/abstract.c index 63fcf15..a0362e7 100644 --- a/Objects/abstract.c +++ b/Objects/abstract.c @@ -1309,12 +1309,30 @@ PyNumber_Long(PyObject *o) /* The below check is done in PyLong_FromUnicode(). */ return PyLong_FromUnicodeObject(o, 10); - if (PyObject_GetBuffer(o, &view, PyBUF_SIMPLE) == 0) { + if (PyBytes_Check(o)) /* need to do extra error checking that PyLong_FromString() * doesn't do. In particular int('9\x005') must raise an * exception, not truncate at the null. */ - PyObject *result = _PyLong_FromBytes(view.buf, view.len, 10); + return _PyLong_FromBytes(PyBytes_AS_STRING(o), + PyBytes_GET_SIZE(o), 10); + + if (PyByteArray_Check(o)) + return _PyLong_FromBytes(PyByteArray_AS_STRING(o), + PyByteArray_GET_SIZE(o), 10); + + if (PyObject_GetBuffer(o, &view, PyBUF_SIMPLE) == 0) { + PyObject *result, *bytes; + + /* Copy to NUL-terminated buffer. */ + bytes = PyBytes_FromStringAndSize((const char *)view.buf, view.len); + if (bytes == NULL) { + PyBuffer_Release(&view); + return NULL; + } + result = _PyLong_FromBytes(PyBytes_AS_STRING(bytes), + PyBytes_GET_SIZE(bytes), 10); + Py_DECREF(bytes); PyBuffer_Release(&view); return result; } |