Skip to content
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

Fix segfault in surface.fblits #2667

Merged
merged 2 commits into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
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
10 changes: 7 additions & 3 deletions src_c/surface.c
Original file line number Diff line number Diff line change
Expand Up @@ -2145,6 +2145,7 @@ surf_fblits(pgSurfaceObject *self, PyObject *const *args, Py_ssize_t nargs)
PyObject *blit_sequence, *item, *src_surf, *blit_pos;
int blend_flags = 0; /* Default flag is 0, opaque */
int error = 0;
int is_generator = 0;

if (nargs == 0 || nargs > 2) {
error = FBLITS_ERR_INCORRECT_ARGS_NUM;
Expand Down Expand Up @@ -2216,11 +2217,11 @@ surf_fblits(pgSurfaceObject *self, PyObject *const *args, Py_ssize_t nargs)
}
/* Generator path */
else if (PyIter_Check(blit_sequence)) {
is_generator = 1;
while ((item = PyIter_Next(blit_sequence))) {
/* Check that the item is a tuple of length 2 */
if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) {
error = FBLITS_ERR_TUPLE_REQUIRED;
Py_DECREF(item);
ankith26 marked this conversation as resolved.
Show resolved Hide resolved
goto on_error;
}

Expand All @@ -2229,8 +2230,6 @@ surf_fblits(pgSurfaceObject *self, PyObject *const *args, Py_ssize_t nargs)
src_surf = PyTuple_GET_ITEM(item, 0);
blit_pos = PyTuple_GET_ITEM(item, 1);

Py_DECREF(item);

/* Check that the source is a Surface */
if (!pgSurface_Check(src_surf)) {
error = BLITS_ERR_SOURCE_NOT_SURFACE;
Expand Down Expand Up @@ -2262,6 +2261,8 @@ surf_fblits(pgSurfaceObject *self, PyObject *const *args, Py_ssize_t nargs)
error = BLITS_ERR_BLIT_FAIL;
goto on_error;
}

Py_DECREF(item);
}
}
else {
Expand All @@ -2272,6 +2273,9 @@ surf_fblits(pgSurfaceObject *self, PyObject *const *args, Py_ssize_t nargs)
Py_RETURN_NONE;

on_error:
if (is_generator) {
Py_XDECREF(item);
}
switch (error) {
case BLITS_ERR_SEQUENCE_REQUIRED:
return RAISE(
Expand Down
3 changes: 3 additions & 0 deletions test/blit_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,9 @@ def blits(blit_list):
self.assertEqual(dst.fblits(blit_list, 0), None)
self.assertEqual(dst.fblits(blit_list, 1), dst.blits(blit_list, doreturn=0))

# make sure this doesn't segfault
dst.fblits((dst, dst.get_rect().topleft) for _ in range(1))

t0 = time()
results = blits(blit_list)
t1 = time()
Expand Down
Loading