[Python-checkins] [3.11] gh-104698: Fix reference leak in mmapmodule.c (GH-104700) (#104710)

JelleZijlstra webhook-mailer at python.org
Sat May 20 21:44:00 EDT 2023


https://github.com/python/cpython/commit/b2e0201222dc993ce1813b2a92bda43b74f4262f
commit: b2e0201222dc993ce1813b2a92bda43b74f4262f
branch: 3.11
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: JelleZijlstra <jelle.zijlstra at gmail.com>
date: 2023-05-20T18:43:18-07:00
summary:

[3.11] gh-104698: Fix reference leak in mmapmodule.c (GH-104700) (#104710)

(cherry picked from commit 99b641886a09252bbcf99a1d322fa8734f1ca30d)

Co-authored-by: Kirill Podoprigora <kirill.bast9 at mail.ru>
Co-authored-by: Jelle Zijlstra <jelle.zijlstra at gmail.com>

files:
M Modules/mmapmodule.c

diff --git a/Modules/mmapmodule.c b/Modules/mmapmodule.c
index 39e56f290383..4b0c1e27e0c5 100644
--- a/Modules/mmapmodule.c
+++ b/Modules/mmapmodule.c
@@ -235,6 +235,14 @@ do {                                                                    \
     return err;                                                         \
     }                                                                   \
 } while (0)
+#define CHECK_VALID_OR_RELEASE(err, buffer)                             \
+do {                                                                    \
+    if (self->map_handle == NULL) {                                     \
+    PyErr_SetString(PyExc_ValueError, "mmap closed or invalid");        \
+    PyBuffer_Release(&(buffer));                                        \
+    return (err);                                                       \
+    }                                                                   \
+} while (0)
 #endif /* MS_WINDOWS */
 
 #ifdef UNIX
@@ -245,6 +253,14 @@ do {                                                                    \
     return err;                                                         \
     }                                                                   \
 } while (0)
+#define CHECK_VALID_OR_RELEASE(err, buffer)                             \
+do {                                                                    \
+    if (self->data == NULL) {                                           \
+    PyErr_SetString(PyExc_ValueError, "mmap closed or invalid");        \
+    PyBuffer_Release(&(buffer));                                        \
+    return (err);                                                       \
+    }                                                                   \
+} while (0)
 #endif /* UNIX */
 
 static PyObject *
@@ -334,7 +350,7 @@ mmap_gfind(mmap_object *self,
             end = self->size;
 
         Py_ssize_t res;
-        CHECK_VALID(NULL);
+        CHECK_VALID_OR_RELEASE(NULL, view);
         if (reverse) {
             res = _PyBytes_ReverseFind(
                 self->data + start, end - start,
@@ -411,7 +427,7 @@ mmap_write_method(mmap_object *self,
         return NULL;
     }
 
-    CHECK_VALID(NULL);
+    CHECK_VALID_OR_RELEASE(NULL, data);
     memcpy(&self->data[self->pos], data.buf, data.len);
     self->pos += data.len;
     PyBuffer_Release(&data);
@@ -1097,7 +1113,7 @@ mmap_ass_subscript(mmap_object *self, PyObject *item, PyObject *value)
             return -1;
         }
 
-        CHECK_VALID(-1);
+        CHECK_VALID_OR_RELEASE(-1, vbuf);
         if (slicelen == 0) {
         }
         else if (step == 1) {



More information about the Python-checkins mailing list