Skip to content

Conversation

oddbookworm
Copy link
Contributor

Fixes #3665 by raising a ValueError if the surface access is after closing the PixelArray

Copy link
Contributor

@Matiiss Matiiss left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fairly certain it would be important to add this check to other places where it hasn't been added:

pygame/src_c/pixelarray.c

Lines 432 to 438 in 25aac95

static PyObject *
_pxarray_get_itemsize(pgPixelArrayObject *self, void *closure)
{
SDL_Surface *surf = pgSurface_AsSurface(self->surface);
return PyLong_FromLong((long)surf->format->BytesPerPixel);
}

and I'm not sure about this but if shape also gets set to NULL (even if that doesn't happen, I suppose these should then still check if self->surface == NULL):

pygame/src_c/pixelarray.c

Lines 443 to 450 in 25aac95

static PyObject *
_pxarray_get_shape(pgPixelArrayObject *self, void *closure)
{
if (self->shape[1]) {
return Py_BuildValue("(nn)", self->shape[0], self->shape[1]);
}
return Py_BuildValue("(n)", self->shape[0]);
}

pygame/src_c/pixelarray.c

Lines 455 to 462 in 25aac95

static PyObject *
_pxarray_get_strides(pgPixelArrayObject *self, void *closure)
{
if (self->shape[1]) {
return Py_BuildValue("(nn)", self->strides[0], self->strides[1]);
}
return Py_BuildValue("(n)", self->strides[0]);
}

pygame/src_c/pixelarray.c

Lines 467 to 471 in 25aac95

static PyObject *
_pxarray_get_ndim(pgPixelArrayObject *self, void *closure)
{
return PyLong_FromLong(self->shape[1] ? 2L : 1L);
}

And I'm fairly certain there are a couple more functions that need this check, perhaps, also in pixelarray_methods.c

@MyreMylar
Copy link
Contributor

MyreMylar commented Jan 14, 2023

From my testing these are the PixelArray public functions and members that segfault after a first call to .close():

  • .surface
  • .itemsize
  • .close() - a second call to it.
  • transpose()

The others already say:

ValueError: Operation on closed PixelArray.

Or return some valid data. I didn't bother testing all the dunder methods because this is already a fairly dumb thing for a user to be doing.

Copy link
Contributor

@MyreMylar MyreMylar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a similar check to clear the other segfaults outlined above.

Copy link
Contributor

@MyreMylar MyreMylar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

@Starbuck5
Copy link
Contributor

This looks reasonable to me, thanks Andrew!

illume pushed a commit that referenced this pull request Apr 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
PixelArray pygame.PixelArray segfault
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Segfault in PixelArray when trying to access surface after closing the PixelArray
4 participants