Skip to content

Commit b2e0201

Browse files
miss-islingtonEclips4JelleZijlstra
authored
[3.11] gh-104698: Fix reference leak in mmapmodule.c (GH-104700) (#104710)
(cherry picked from commit 99b6418) Co-authored-by: Kirill Podoprigora <kirill.bast9@mail.ru> Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
1 parent 2e457bc commit b2e0201

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

Modules/mmapmodule.c

+19-3
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,14 @@ do { \
235235
return err; \
236236
} \
237237
} while (0)
238+
#define CHECK_VALID_OR_RELEASE(err, buffer) \
239+
do { \
240+
if (self->map_handle == NULL) { \
241+
PyErr_SetString(PyExc_ValueError, "mmap closed or invalid"); \
242+
PyBuffer_Release(&(buffer)); \
243+
return (err); \
244+
} \
245+
} while (0)
238246
#endif /* MS_WINDOWS */
239247

240248
#ifdef UNIX
@@ -245,6 +253,14 @@ do { \
245253
return err; \
246254
} \
247255
} while (0)
256+
#define CHECK_VALID_OR_RELEASE(err, buffer) \
257+
do { \
258+
if (self->data == NULL) { \
259+
PyErr_SetString(PyExc_ValueError, "mmap closed or invalid"); \
260+
PyBuffer_Release(&(buffer)); \
261+
return (err); \
262+
} \
263+
} while (0)
248264
#endif /* UNIX */
249265

250266
static PyObject *
@@ -334,7 +350,7 @@ mmap_gfind(mmap_object *self,
334350
end = self->size;
335351

336352
Py_ssize_t res;
337-
CHECK_VALID(NULL);
353+
CHECK_VALID_OR_RELEASE(NULL, view);
338354
if (reverse) {
339355
res = _PyBytes_ReverseFind(
340356
self->data + start, end - start,
@@ -411,7 +427,7 @@ mmap_write_method(mmap_object *self,
411427
return NULL;
412428
}
413429

414-
CHECK_VALID(NULL);
430+
CHECK_VALID_OR_RELEASE(NULL, data);
415431
memcpy(&self->data[self->pos], data.buf, data.len);
416432
self->pos += data.len;
417433
PyBuffer_Release(&data);
@@ -1097,7 +1113,7 @@ mmap_ass_subscript(mmap_object *self, PyObject *item, PyObject *value)
10971113
return -1;
10981114
}
10991115

1100-
CHECK_VALID(-1);
1116+
CHECK_VALID_OR_RELEASE(-1, vbuf);
11011117
if (slicelen == 0) {
11021118
}
11031119
else if (step == 1) {

0 commit comments

Comments
 (0)