summaryrefslogtreecommitdiffstats
path: root/Modules
diff options
context:
space:
mode:
authorKirill Podoprigora <kirill.bast9@mail.ru>2023-05-21 00:19:56 (GMT)
committerGitHub <noreply@github.com>2023-05-21 00:19:56 (GMT)
commit99b641886a09252bbcf99a1d322fa8734f1ca30d (patch)
treeb74172d876e02fa4f103100ac5a0be09c2b6458d /Modules
parentb870b1fa755808977578e99bd42859c519e00bb7 (diff)
downloadcpython-99b641886a09252bbcf99a1d322fa8734f1ca30d.zip
cpython-99b641886a09252bbcf99a1d322fa8734f1ca30d.tar.gz
cpython-99b641886a09252bbcf99a1d322fa8734f1ca30d.tar.bz2
gh-104698: Fix reference leak in mmapmodule.c (#104700)
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
Diffstat (limited to 'Modules')
-rw-r--r--Modules/mmapmodule.c22
1 files changed, 19 insertions, 3 deletions
diff --git a/Modules/mmapmodule.c b/Modules/mmapmodule.c
index 8e5a0bd..6bde993 100644
--- a/Modules/mmapmodule.c
+++ b/Modules/mmapmodule.c
@@ -227,6 +227,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
@@ -237,6 +245,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 *
@@ -326,7 +342,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,
@@ -403,7 +419,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);
@@ -1087,7 +1103,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) {