Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 21, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions Modules/mmapmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 *
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down