Skip to content

Commit 99b6418

Browse files
gh-104698: Fix reference leak in mmapmodule.c (#104700)
Co-authored-by: Jelle Zijlstra <[email protected]>
1 parent b870b1f commit 99b6418

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
@@ -227,6 +227,14 @@ do { \
227227
return err; \
228228
} \
229229
} while (0)
230+
#define CHECK_VALID_OR_RELEASE(err, buffer) \
231+
do { \
232+
if (self->map_handle == NULL) { \
233+
PyErr_SetString(PyExc_ValueError, "mmap closed or invalid"); \
234+
PyBuffer_Release(&(buffer)); \
235+
return (err); \
236+
} \
237+
} while (0)
230238
#endif /* MS_WINDOWS */
231239

232240
#ifdef UNIX
@@ -237,6 +245,14 @@ do { \
237245
return err; \
238246
} \
239247
} while (0)
248+
#define CHECK_VALID_OR_RELEASE(err, buffer) \
249+
do { \
250+
if (self->data == NULL) { \
251+
PyErr_SetString(PyExc_ValueError, "mmap closed or invalid"); \
252+
PyBuffer_Release(&(buffer)); \
253+
return (err); \
254+
} \
255+
} while (0)
240256
#endif /* UNIX */
241257

242258
static PyObject *
@@ -326,7 +342,7 @@ mmap_gfind(mmap_object *self,
326342
end = self->size;
327343

328344
Py_ssize_t res;
329-
CHECK_VALID(NULL);
345+
CHECK_VALID_OR_RELEASE(NULL, view);
330346
if (reverse) {
331347
res = _PyBytes_ReverseFind(
332348
self->data + start, end - start,
@@ -403,7 +419,7 @@ mmap_write_method(mmap_object *self,
403419
return NULL;
404420
}
405421

406-
CHECK_VALID(NULL);
422+
CHECK_VALID_OR_RELEASE(NULL, data);
407423
memcpy(&self->data[self->pos], data.buf, data.len);
408424
self->pos += data.len;
409425
PyBuffer_Release(&data);
@@ -1087,7 +1103,7 @@ mmap_ass_subscript(mmap_object *self, PyObject *item, PyObject *value)
10871103
return -1;
10881104
}
10891105

1090-
CHECK_VALID(-1);
1106+
CHECK_VALID_OR_RELEASE(-1, vbuf);
10911107
if (slicelen == 0) {
10921108
}
10931109
else if (step == 1) {

0 commit comments

Comments
 (0)