Skip to content

Commit aac875f

Browse files
[3.6] bpo-8256: Fixed possible failing or crashing input() (#641)
if attributes "encoding" or "errors" of sys.stdin or sys.stdout are not set or are not strings.
1 parent c609484 commit aac875f

2 files changed

Lines changed: 19 additions & 5 deletions

File tree

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ Core and Builtins
2424
Library
2525
-------
2626

27+
- bpo-8256: Fixed possible failing or crashing input() if attributes "encoding"
28+
or "errors" of sys.stdin or sys.stdout are not set or are not strings.
29+
2730
- bpo-28298: Fix a bug that prevented array 'Q', 'L' and 'I' from accepting big
2831
intables (objects that have __int__) as elements. Patch by Oren Milman.
2932

Python/bltinmodule.c

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1927,12 +1927,15 @@ builtin_input_impl(PyObject *module, PyObject *prompt)
19271927
PyObject *result;
19281928
size_t len;
19291929

1930+
/* stdin is a text stream, so it must have an encoding. */
19301931
stdin_encoding = _PyObject_GetAttrId(fin, &PyId_encoding);
19311932
stdin_errors = _PyObject_GetAttrId(fin, &PyId_errors);
1932-
if (!stdin_encoding || !stdin_errors)
1933-
/* stdin is a text stream, so it must have an
1934-
encoding. */
1933+
if (!stdin_encoding || !stdin_errors ||
1934+
!PyUnicode_Check(stdin_encoding) ||
1935+
!PyUnicode_Check(stdin_errors)) {
1936+
tty = 0;
19351937
goto _readline_errors;
1938+
}
19361939
stdin_encoding_str = PyUnicode_AsUTF8(stdin_encoding);
19371940
stdin_errors_str = PyUnicode_AsUTF8(stdin_errors);
19381941
if (!stdin_encoding_str || !stdin_errors_str)
@@ -1948,8 +1951,12 @@ builtin_input_impl(PyObject *module, PyObject *prompt)
19481951
PyObject *stringpo;
19491952
stdout_encoding = _PyObject_GetAttrId(fout, &PyId_encoding);
19501953
stdout_errors = _PyObject_GetAttrId(fout, &PyId_errors);
1951-
if (!stdout_encoding || !stdout_errors)
1954+
if (!stdout_encoding || !stdout_errors ||
1955+
!PyUnicode_Check(stdout_encoding) ||
1956+
!PyUnicode_Check(stdout_errors)) {
1957+
tty = 0;
19521958
goto _readline_errors;
1959+
}
19531960
stdout_encoding_str = PyUnicode_AsUTF8(stdout_encoding);
19541961
stdout_errors_str = PyUnicode_AsUTF8(stdout_errors);
19551962
if (!stdout_encoding_str || !stdout_errors_str)
@@ -2003,13 +2010,17 @@ builtin_input_impl(PyObject *module, PyObject *prompt)
20032010
Py_XDECREF(po);
20042011
PyMem_FREE(s);
20052012
return result;
2013+
20062014
_readline_errors:
20072015
Py_XDECREF(stdin_encoding);
20082016
Py_XDECREF(stdout_encoding);
20092017
Py_XDECREF(stdin_errors);
20102018
Py_XDECREF(stdout_errors);
20112019
Py_XDECREF(po);
2012-
return NULL;
2020+
if (tty)
2021+
return NULL;
2022+
2023+
PyErr_Clear();
20132024
}
20142025

20152026
/* Fallback if we're not interactive */

0 commit comments

Comments
 (0)