Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f6c5783

Browse files
author
Victor Stinner
committed
Issue #6697: Check that _PyUnicode_AsString() result is not NULL in textio.c
The bug may occurs if locale.getpreferredencoding() returns an encoding with a surrogate (very unlikely!).
1 parent 306f010 commit f6c5783

1 file changed

Lines changed: 7 additions & 2 deletions

File tree

Modules/_io/textio.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -905,8 +905,11 @@ textiowrapper_init(textio *self, PyObject *args, PyObject *kwds)
905905
Py_CLEAR(self->encoding);
906906
}
907907
}
908-
if (self->encoding != NULL)
908+
if (self->encoding != NULL) {
909909
encoding = _PyUnicode_AsString(self->encoding);
910+
if (encoding == NULL)
911+
goto error;
912+
}
910913
else if (encoding != NULL) {
911914
self->encoding = PyUnicode_FromString(encoding);
912915
if (self->encoding == NULL)
@@ -935,6 +938,8 @@ textiowrapper_init(textio *self, PyObject *args, PyObject *kwds)
935938
self->writetranslate = (newline == NULL || newline[0] != '\0');
936939
if (!self->readuniversal && self->readnl) {
937940
self->writenl = _PyUnicode_AsString(self->readnl);
941+
if (self->writenl == NULL)
942+
goto error;
938943
if (!strcmp(self->writenl, "\n"))
939944
self->writenl = NULL;
940945
}
@@ -2408,7 +2413,7 @@ textiowrapper_close(textio *self, PyObject *args)
24082413
Py_DECREF(res);
24092414
if (r < 0)
24102415
return NULL;
2411-
2416+
24122417
if (r > 0) {
24132418
Py_RETURN_NONE; /* stream already closed */
24142419
}

0 commit comments

Comments
 (0)