]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
GH-94857: fix test_io refleak (GH-94858)
authorKumar Aditya <59607654+kumaraditya303@users.noreply.github.com>
Mon, 18 Jul 2022 13:48:04 +0000 (19:18 +0530)
committerGitHub <noreply@github.com>
Mon, 18 Jul 2022 13:48:04 +0000 (06:48 -0700)
Misc/NEWS.d/next/Library/2022-07-15-08-13-51.gh-issue-94857.9_KvZJ.rst [new file with mode: 0644]
Modules/_io/textio.c

diff --git a/Misc/NEWS.d/next/Library/2022-07-15-08-13-51.gh-issue-94857.9_KvZJ.rst b/Misc/NEWS.d/next/Library/2022-07-15-08-13-51.gh-issue-94857.9_KvZJ.rst
new file mode 100644 (file)
index 0000000..e684415
--- /dev/null
@@ -0,0 +1 @@
+Fix refleak in ``_io.TextIOWrapper.reconfigure``. Patch by Kumar Aditya.
index 660396b8b03ead065f9336a69bdbfecf4cad7611..89094d66f383435b5417c0394ad98e1d414fad6c 100644 (file)
@@ -1247,6 +1247,7 @@ textiowrapper_change_encoding(textio *self, PyObject *encoding,
         if (errors == Py_None) {
             errors = self->errors;
         }
+        Py_INCREF(encoding);
     }
     else {
         if (_PyUnicode_EqualToASCIIString(encoding, "locale")) {
@@ -1254,6 +1255,8 @@ textiowrapper_change_encoding(textio *self, PyObject *encoding,
             if (encoding == NULL) {
                 return -1;
             }
+        } else {
+            Py_INCREF(encoding);
         }
         if (errors == Py_None) {
             errors = &_Py_ID(strict);
@@ -1262,6 +1265,7 @@ textiowrapper_change_encoding(textio *self, PyObject *encoding,
 
     const char *c_errors = PyUnicode_AsUTF8(errors);
     if (c_errors == NULL) {
+        Py_DECREF(encoding);
         return -1;
     }
 
@@ -1269,16 +1273,17 @@ textiowrapper_change_encoding(textio *self, PyObject *encoding,
     PyObject *codec_info = _PyCodec_LookupTextEncoding(
         PyUnicode_AsUTF8(encoding), "codecs.open()");
     if (codec_info == NULL) {
+        Py_DECREF(encoding);
         return -1;
     }
     if (_textiowrapper_set_decoder(self, codec_info, c_errors) != 0 ||
             _textiowrapper_set_encoder(self, codec_info, c_errors) != 0) {
         Py_DECREF(codec_info);
+        Py_DECREF(encoding);
         return -1;
     }
     Py_DECREF(codec_info);
 
-    Py_INCREF(encoding);
     Py_INCREF(errors);
     Py_SETREF(self->encoding, encoding);
     Py_SETREF(self->errors, errors);