Skip to content

Commit

Permalink
Fix segfault in surface.fblits
Browse files Browse the repository at this point in the history
XDECREF -> DECREF
  • Loading branch information
Matiiss committed Jan 14, 2024
1 parent 0b542bc commit a87d631
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
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);
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_DECREF(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

0 comments on commit a87d631

Please sign in to comment.