mirror of
https://github.com/python/cpython.git
synced 2025-07-24 11:44:31 +00:00
Issue #6697: Fix a crash if sys.stdin or sys.stdout encoding contain a surrogate
This is *very* unlikely :-)
This commit is contained in:
parent
93b5513cf1
commit
306f0100f3
1 changed files with 16 additions and 7 deletions
|
@ -1610,6 +1610,7 @@ builtin_input(PyObject *self, PyObject *args)
|
||||||
char *prompt;
|
char *prompt;
|
||||||
char *s;
|
char *s;
|
||||||
PyObject *stdin_encoding;
|
PyObject *stdin_encoding;
|
||||||
|
char *stdin_encoding_str;
|
||||||
PyObject *result;
|
PyObject *result;
|
||||||
|
|
||||||
stdin_encoding = PyObject_GetAttrString(fin, "encoding");
|
stdin_encoding = PyObject_GetAttrString(fin, "encoding");
|
||||||
|
@ -1617,6 +1618,11 @@ builtin_input(PyObject *self, PyObject *args)
|
||||||
/* stdin is a text stream, so it must have an
|
/* stdin is a text stream, so it must have an
|
||||||
encoding. */
|
encoding. */
|
||||||
return NULL;
|
return NULL;
|
||||||
|
stdin_encoding_str = _PyUnicode_AsString(stdin_encoding);
|
||||||
|
if (stdin_encoding_str == NULL) {
|
||||||
|
Py_DECREF(stdin_encoding);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
tmp = PyObject_CallMethod(fout, "flush", "");
|
tmp = PyObject_CallMethod(fout, "flush", "");
|
||||||
if (tmp == NULL)
|
if (tmp == NULL)
|
||||||
PyErr_Clear();
|
PyErr_Clear();
|
||||||
|
@ -1625,12 +1631,18 @@ builtin_input(PyObject *self, PyObject *args)
|
||||||
if (promptarg != NULL) {
|
if (promptarg != NULL) {
|
||||||
PyObject *stringpo;
|
PyObject *stringpo;
|
||||||
PyObject *stdout_encoding;
|
PyObject *stdout_encoding;
|
||||||
stdout_encoding = PyObject_GetAttrString(fout,
|
char *stdout_encoding_str;
|
||||||
"encoding");
|
stdout_encoding = PyObject_GetAttrString(fout, "encoding");
|
||||||
if (stdout_encoding == NULL) {
|
if (stdout_encoding == NULL) {
|
||||||
Py_DECREF(stdin_encoding);
|
Py_DECREF(stdin_encoding);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
stdout_encoding_str = _PyUnicode_AsString(stdout_encoding);
|
||||||
|
if (stdout_encoding_str == NULL) {
|
||||||
|
Py_DECREF(stdin_encoding);
|
||||||
|
Py_DECREF(stdout_encoding);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
stringpo = PyObject_Str(promptarg);
|
stringpo = PyObject_Str(promptarg);
|
||||||
if (stringpo == NULL) {
|
if (stringpo == NULL) {
|
||||||
Py_DECREF(stdin_encoding);
|
Py_DECREF(stdin_encoding);
|
||||||
|
@ -1638,7 +1650,7 @@ builtin_input(PyObject *self, PyObject *args)
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
po = PyUnicode_AsEncodedString(stringpo,
|
po = PyUnicode_AsEncodedString(stringpo,
|
||||||
_PyUnicode_AsString(stdout_encoding), NULL);
|
stdout_encoding_str, NULL);
|
||||||
Py_DECREF(stdout_encoding);
|
Py_DECREF(stdout_encoding);
|
||||||
Py_DECREF(stringpo);
|
Py_DECREF(stringpo);
|
||||||
if (po == NULL) {
|
if (po == NULL) {
|
||||||
|
@ -1676,10 +1688,7 @@ builtin_input(PyObject *self, PyObject *args)
|
||||||
result = NULL;
|
result = NULL;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
result = PyUnicode_Decode
|
result = PyUnicode_Decode(s, len-1, stdin_encoding_str, NULL);
|
||||||
(s, len-1,
|
|
||||||
_PyUnicode_AsString(stdin_encoding),
|
|
||||||
NULL);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Py_DECREF(stdin_encoding);
|
Py_DECREF(stdin_encoding);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue