mirror of
https://github.com/python/cpython.git
synced 2025-07-12 13:55:34 +00:00
Issue #24802: Merge null termination fixes from 3.5
This commit is contained in:
commit
9513ba3b8f
8 changed files with 165 additions and 36 deletions
|
@ -599,20 +599,37 @@ builtin_chr_impl(PyModuleDef *module, int i)
|
|||
|
||||
|
||||
static const char *
|
||||
source_as_string(PyObject *cmd, const char *funcname, const char *what, PyCompilerFlags *cf, Py_buffer *view)
|
||||
source_as_string(PyObject *cmd, const char *funcname, const char *what, PyCompilerFlags *cf, PyObject **cmd_copy)
|
||||
{
|
||||
const char *str;
|
||||
Py_ssize_t size;
|
||||
Py_buffer view;
|
||||
|
||||
*cmd_copy = NULL;
|
||||
if (PyUnicode_Check(cmd)) {
|
||||
cf->cf_flags |= PyCF_IGNORE_COOKIE;
|
||||
str = PyUnicode_AsUTF8AndSize(cmd, &size);
|
||||
if (str == NULL)
|
||||
return NULL;
|
||||
}
|
||||
else if (PyObject_GetBuffer(cmd, view, PyBUF_SIMPLE) == 0) {
|
||||
str = (const char *)view->buf;
|
||||
size = view->len;
|
||||
else if (PyBytes_Check(cmd)) {
|
||||
str = PyBytes_AS_STRING(cmd);
|
||||
size = PyBytes_GET_SIZE(cmd);
|
||||
}
|
||||
else if (PyByteArray_Check(cmd)) {
|
||||
str = PyByteArray_AS_STRING(cmd);
|
||||
size = PyByteArray_GET_SIZE(cmd);
|
||||
}
|
||||
else if (PyObject_GetBuffer(cmd, &view, PyBUF_SIMPLE) == 0) {
|
||||
/* Copy to NUL-terminated buffer. */
|
||||
*cmd_copy = PyBytes_FromStringAndSize(
|
||||
(const char *)view.buf, view.len);
|
||||
PyBuffer_Release(&view);
|
||||
if (*cmd_copy == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
str = PyBytes_AS_STRING(*cmd_copy);
|
||||
size = PyBytes_GET_SIZE(*cmd_copy);
|
||||
}
|
||||
else {
|
||||
PyErr_Format(PyExc_TypeError,
|
||||
|
@ -624,7 +641,7 @@ source_as_string(PyObject *cmd, const char *funcname, const char *what, PyCompil
|
|||
if (strlen(str) != (size_t)size) {
|
||||
PyErr_SetString(PyExc_ValueError,
|
||||
"source code string cannot contain null bytes");
|
||||
PyBuffer_Release(view);
|
||||
Py_CLEAR(*cmd_copy);
|
||||
return NULL;
|
||||
}
|
||||
return str;
|
||||
|
@ -660,7 +677,7 @@ builtin_compile_impl(PyModuleDef *module, PyObject *source,
|
|||
int dont_inherit, int optimize)
|
||||
/*[clinic end generated code: output=31881762c1bb90c4 input=9d53e8cfb3c86414]*/
|
||||
{
|
||||
Py_buffer view = {NULL, NULL};
|
||||
PyObject *source_copy;
|
||||
const char *str;
|
||||
int compile_mode = -1;
|
||||
int is_ast;
|
||||
|
@ -732,12 +749,12 @@ builtin_compile_impl(PyModuleDef *module, PyObject *source,
|
|||
goto finally;
|
||||
}
|
||||
|
||||
str = source_as_string(source, "compile", "string, bytes or AST", &cf, &view);
|
||||
str = source_as_string(source, "compile", "string, bytes or AST", &cf, &source_copy);
|
||||
if (str == NULL)
|
||||
goto error;
|
||||
|
||||
result = Py_CompileStringObject(str, filename, start[compile_mode], &cf, optimize);
|
||||
PyBuffer_Release(&view);
|
||||
Py_XDECREF(source_copy);
|
||||
goto finally;
|
||||
|
||||
error:
|
||||
|
@ -812,8 +829,7 @@ builtin_eval_impl(PyModuleDef *module, PyObject *source, PyObject *globals,
|
|||
PyObject *locals)
|
||||
/*[clinic end generated code: output=7284501fb7b4d666 input=11ee718a8640e527]*/
|
||||
{
|
||||
PyObject *result, *tmp = NULL;
|
||||
Py_buffer view = {NULL, NULL};
|
||||
PyObject *result, *source_copy;
|
||||
const char *str;
|
||||
PyCompilerFlags cf;
|
||||
|
||||
|
@ -861,7 +877,7 @@ builtin_eval_impl(PyModuleDef *module, PyObject *source, PyObject *globals,
|
|||
}
|
||||
|
||||
cf.cf_flags = PyCF_SOURCE_IS_UTF8;
|
||||
str = source_as_string(source, "eval", "string, bytes or code", &cf, &view);
|
||||
str = source_as_string(source, "eval", "string, bytes or code", &cf, &source_copy);
|
||||
if (str == NULL)
|
||||
return NULL;
|
||||
|
||||
|
@ -870,8 +886,7 @@ builtin_eval_impl(PyModuleDef *module, PyObject *source, PyObject *globals,
|
|||
|
||||
(void)PyEval_MergeCompilerFlags(&cf);
|
||||
result = PyRun_StringFlags(str, Py_eval_input, globals, locals, &cf);
|
||||
PyBuffer_Release(&view);
|
||||
Py_XDECREF(tmp);
|
||||
Py_XDECREF(source_copy);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
@ -942,12 +957,13 @@ builtin_exec_impl(PyModuleDef *module, PyObject *source, PyObject *globals,
|
|||
v = PyEval_EvalCode(source, globals, locals);
|
||||
}
|
||||
else {
|
||||
Py_buffer view = {NULL, NULL};
|
||||
PyObject *source_copy;
|
||||
const char *str;
|
||||
PyCompilerFlags cf;
|
||||
cf.cf_flags = PyCF_SOURCE_IS_UTF8;
|
||||
str = source_as_string(source, "exec",
|
||||
"string, bytes or code", &cf, &view);
|
||||
"string, bytes or code", &cf,
|
||||
&source_copy);
|
||||
if (str == NULL)
|
||||
return NULL;
|
||||
if (PyEval_MergeCompilerFlags(&cf))
|
||||
|
@ -955,7 +971,7 @@ builtin_exec_impl(PyModuleDef *module, PyObject *source, PyObject *globals,
|
|||
locals, &cf);
|
||||
else
|
||||
v = PyRun_String(str, Py_file_input, globals, locals);
|
||||
PyBuffer_Release(&view);
|
||||
Py_XDECREF(source_copy);
|
||||
}
|
||||
if (v == NULL)
|
||||
return NULL;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue