summaryrefslogtreecommitdiffstats
path: root/Python/bltinmodule.c
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2015-11-20 19:56:21 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2015-11-20 19:56:21 (GMT)
commit6156560e4b40ae81304d80b5a932fd90e6b4ba80 (patch)
treed8ed8e08a60b0c9ed7f3cb582d5765fa1f814643 /Python/bltinmodule.c
parent815ab140302a2f7a541d1bbda650875bd47f8ea2 (diff)
downloadcpython-6156560e4b40ae81304d80b5a932fd90e6b4ba80.zip
cpython-6156560e4b40ae81304d80b5a932fd90e6b4ba80.tar.gz
cpython-6156560e4b40ae81304d80b5a932fd90e6b4ba80.tar.bz2
Issue #25678: Copy buffer objects to null-terminated strings.
Avoid buffer overreads when int(), long(), float(), and compile() are passed buffer objects. Similar code is removed from the complex() constructor, where it was not reachable. Patch backported from issue #24802 by Eryk Sun.
Diffstat (limited to 'Python/bltinmodule.c')
-rw-r--r--Python/bltinmodule.c21
1 files changed, 16 insertions, 5 deletions
diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c
index f052574..d99b676 100644
--- a/Python/bltinmodule.c
+++ b/Python/bltinmodule.c
@@ -538,18 +538,29 @@ builtin_compile(PyObject *self, PyObject *args, PyObject *kwds)
}
return result;
}
-
+ if (PyString_Check(cmd)) {
+ str = PyString_AS_STRING(cmd);
+ length = PyString_GET_SIZE(cmd);
+ }
#ifdef Py_USING_UNICODE
- if (PyUnicode_Check(cmd)) {
+ else if (PyUnicode_Check(cmd)) {
tmp = PyUnicode_AsUTF8String(cmd);
if (tmp == NULL)
return NULL;
- cmd = tmp;
cf.cf_flags |= PyCF_SOURCE_IS_UTF8;
+ str = PyString_AS_STRING(tmp);
+ length = PyString_GET_SIZE(tmp);
}
#endif
-
- if (PyObject_AsReadBuffer(cmd, (const void **)&str, &length))
+ else if (!PyObject_AsReadBuffer(cmd, (const void **)&str, &length)) {
+ /* Copy to NUL-terminated buffer. */
+ tmp = PyString_FromStringAndSize(str, length);
+ if (tmp == NULL)
+ return NULL;
+ str = PyString_AS_STRING(tmp);
+ length = PyString_GET_SIZE(tmp);
+ }
+ else
goto cleanup;
if ((size_t)length != strlen(str)) {
PyErr_SetString(PyExc_TypeError,