Skip to content

Commit c2cf128

Browse files
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.
1 parent 4dadcd4 commit c2cf128

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,9 @@ Extension Modules
278278
Library
279279
-------
280280

281+
- bpo-8256: Fixed possible failing or crashing input() if attributes "encoding"
282+
or "errors" of sys.stdin or sys.stdout are not set or are not strings.
283+
281284
- bpo-28692: Using non-integer value for selecting a plural form in gettext is
282285
now deprecated.
283286

Python/bltinmodule.c

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

1929+
/* stdin is a text stream, so it must have an encoding. */
19291930
stdin_encoding = _PyObject_GetAttrId(fin, &PyId_encoding);
19301931
stdin_errors = _PyObject_GetAttrId(fin, &PyId_errors);
1931-
if (!stdin_encoding || !stdin_errors)
1932-
/* stdin is a text stream, so it must have an
1933-
encoding. */
1932+
if (!stdin_encoding || !stdin_errors ||
1933+
!PyUnicode_Check(stdin_encoding) ||
1934+
!PyUnicode_Check(stdin_errors)) {
1935+
tty = 0;
19341936
goto _readline_errors;
1937+
}
19351938
stdin_encoding_str = PyUnicode_AsUTF8(stdin_encoding);
19361939
stdin_errors_str = PyUnicode_AsUTF8(stdin_errors);
19371940
if (!stdin_encoding_str || !stdin_errors_str)
@@ -1947,8 +1950,12 @@ builtin_input_impl(PyObject *module, PyObject *prompt)
19471950
PyObject *stringpo;
19481951
stdout_encoding = _PyObject_GetAttrId(fout, &PyId_encoding);
19491952
stdout_errors = _PyObject_GetAttrId(fout, &PyId_errors);
1950-
if (!stdout_encoding || !stdout_errors)
1953+
if (!stdout_encoding || !stdout_errors ||
1954+
!PyUnicode_Check(stdout_encoding) ||
1955+
!PyUnicode_Check(stdout_errors)) {
1956+
tty = 0;
19511957
goto _readline_errors;
1958+
}
19521959
stdout_encoding_str = PyUnicode_AsUTF8(stdout_encoding);
19531960
stdout_errors_str = PyUnicode_AsUTF8(stdout_errors);
19541961
if (!stdout_encoding_str || !stdout_errors_str)
@@ -2002,13 +2009,17 @@ builtin_input_impl(PyObject *module, PyObject *prompt)
20022009
Py_XDECREF(po);
20032010
PyMem_FREE(s);
20042011
return result;
2012+
20052013
_readline_errors:
20062014
Py_XDECREF(stdin_encoding);
20072015
Py_XDECREF(stdout_encoding);
20082016
Py_XDECREF(stdin_errors);
20092017
Py_XDECREF(stdout_errors);
20102018
Py_XDECREF(po);
2011-
return NULL;
2019+
if (tty)
2020+
return NULL;
2021+
2022+
PyErr_Clear();
20122023
}
20132024

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

0 commit comments

Comments
 (0)