]> granicus.if.org Git - python/commitdiff
Issue #6697: Check that _PyUnicode_AsString() result is not NULL in textio.c
authorVictor Stinner <victor.stinner@haypocalc.com>
Wed, 19 May 2010 01:17:01 +0000 (01:17 +0000)
committerVictor Stinner <victor.stinner@haypocalc.com>
Wed, 19 May 2010 01:17:01 +0000 (01:17 +0000)
The bug may occurs if locale.getpreferredencoding() returns an encoding with a
surrogate (very unlikely!).

Modules/_io/textio.c

index b039c2f6a38184f42c02723c17665226e65ae691..b659795448a27c6de17119f8f071a7544b0161ba 100644 (file)
@@ -905,8 +905,11 @@ textiowrapper_init(textio *self, PyObject *args, PyObject *kwds)
                 Py_CLEAR(self->encoding);
         }
     }
-    if (self->encoding != NULL)
+    if (self->encoding != NULL) {
         encoding = _PyUnicode_AsString(self->encoding);
+        if (encoding == NULL)
+            goto error;
+    }
     else if (encoding != NULL) {
         self->encoding = PyUnicode_FromString(encoding);
         if (self->encoding == NULL)
@@ -935,6 +938,8 @@ textiowrapper_init(textio *self, PyObject *args, PyObject *kwds)
     self->writetranslate = (newline == NULL || newline[0] != '\0');
     if (!self->readuniversal && self->readnl) {
         self->writenl = _PyUnicode_AsString(self->readnl);
+        if (self->writenl == NULL)
+            goto error;
         if (!strcmp(self->writenl, "\n"))
             self->writenl = NULL;
     }
@@ -2408,7 +2413,7 @@ textiowrapper_close(textio *self, PyObject *args)
     Py_DECREF(res);
     if (r < 0)
         return NULL;
-    
+
     if (r > 0) {
         Py_RETURN_NONE; /* stream already closed */
     }