summaryrefslogtreecommitdiffstats
path: root/Python
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2017-03-12 11:50:36 (GMT)
committerGitHub <noreply@github.com>2017-03-12 11:50:36 (GMT)
commitc2cf12857187aa147c268651f10acd6da2c9cb74 (patch)
tree19b0087a9bb6fb80ddfb4e82518b11e62e8083c0 /Python
parent4dadcd4ed7824c7904add78577e6a05864cfe493 (diff)
downloadcpython-c2cf12857187aa147c268651f10acd6da2c9cb74.zip
cpython-c2cf12857187aa147c268651f10acd6da2c9cb74.tar.gz
cpython-c2cf12857187aa147c268651f10acd6da2c9cb74.tar.bz2
bpo-8256: Fixed possible failing or crashing input() (#517)
if attributes "encoding" or "errors" of sys.stdin or sys.stdout are not set or are not strings.
Diffstat (limited to 'Python')
-rw-r--r--Python/bltinmodule.c21
1 files changed, 16 insertions, 5 deletions
diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c
index 62463af..8ae2303 100644
--- a/Python/bltinmodule.c
+++ b/Python/bltinmodule.c
@@ -1926,12 +1926,15 @@ builtin_input_impl(PyObject *module, PyObject *prompt)
PyObject *result;
size_t len;
+ /* stdin is a text stream, so it must have an encoding. */
stdin_encoding = _PyObject_GetAttrId(fin, &PyId_encoding);
stdin_errors = _PyObject_GetAttrId(fin, &PyId_errors);
- if (!stdin_encoding || !stdin_errors)
- /* stdin is a text stream, so it must have an
- encoding. */
+ if (!stdin_encoding || !stdin_errors ||
+ !PyUnicode_Check(stdin_encoding) ||
+ !PyUnicode_Check(stdin_errors)) {
+ tty = 0;
goto _readline_errors;
+ }
stdin_encoding_str = PyUnicode_AsUTF8(stdin_encoding);
stdin_errors_str = PyUnicode_AsUTF8(stdin_errors);
if (!stdin_encoding_str || !stdin_errors_str)
@@ -1947,8 +1950,12 @@ builtin_input_impl(PyObject *module, PyObject *prompt)
PyObject *stringpo;
stdout_encoding = _PyObject_GetAttrId(fout, &PyId_encoding);
stdout_errors = _PyObject_GetAttrId(fout, &PyId_errors);
- if (!stdout_encoding || !stdout_errors)
+ if (!stdout_encoding || !stdout_errors ||
+ !PyUnicode_Check(stdout_encoding) ||
+ !PyUnicode_Check(stdout_errors)) {
+ tty = 0;
goto _readline_errors;
+ }
stdout_encoding_str = PyUnicode_AsUTF8(stdout_encoding);
stdout_errors_str = PyUnicode_AsUTF8(stdout_errors);
if (!stdout_encoding_str || !stdout_errors_str)
@@ -2002,13 +2009,17 @@ builtin_input_impl(PyObject *module, PyObject *prompt)
Py_XDECREF(po);
PyMem_FREE(s);
return result;
+
_readline_errors:
Py_XDECREF(stdin_encoding);
Py_XDECREF(stdout_encoding);
Py_XDECREF(stdin_errors);
Py_XDECREF(stdout_errors);
Py_XDECREF(po);
- return NULL;
+ if (tty)
+ return NULL;
+
+ PyErr_Clear();
}
/* Fallback if we're not interactive */