Skip to content

Commit 095a2fe

Browse files
authored
Merge pull request #2276 from oddbookworm/zero_area_surface
fixed pixelarray indexing to prevent segfault reported in #2275
2 parents ab1f516 + 196eb3f commit 095a2fe

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

src_c/pixelarray.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,10 @@ _pxarray_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
354354
stride0 = (Py_ssize_t)surf->format->BytesPerPixel;
355355
stride1 = (Py_ssize_t)surf->pitch;
356356
pixels = surf->pixels;
357+
if (pixels == NULL) {
358+
return RAISE(PyExc_ValueError,
359+
"Cannot create pixelarray on zero-sized Surface");
360+
}
357361
if (stride0 < 1 || stride0 > 4) {
358362
return RAISE(PyExc_ValueError,
359363
"unsupported bit depth for reference array");

src_c/pixelarray_methods.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,6 @@ _get_single_pixel(pgPixelArrayObject *array, Py_ssize_t x, Py_ssize_t y)
102102
if (array->surface == NULL) {
103103
return RAISE(PyExc_ValueError, "Operation on closed PixelArray.");
104104
}
105-
106105
pixel_p = (array->pixels + x * array->strides[0] + y * array->strides[1]);
107106
surf = pgSurface_AsSurface(array->surface);
108107

test/pixelarray_test.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1309,6 +1309,16 @@ def test_repr(self):
13091309
pixel = sf.get_at_mapped((0, 0))
13101310
self.assertEqual(repr(ar), type(ar).__name__ + "([\n [42, 42, 42]]\n)")
13111311

1312+
def test_zero_size_surface(self):
1313+
"""Should not be able to create PixelArray on a surface with a width or height of 0
1314+
Regression test for pygame-ce issue 2275 (https://github.com/pygame-community/pygame-ce/issues/2275)
1315+
"""
1316+
weird_surface = pygame.Surface((5, 0))
1317+
self.assertRaises(ValueError, lambda: pygame.PixelArray(weird_surface))
1318+
1319+
weird_surface = pygame.Surface((0, 5))
1320+
self.assertRaises(ValueError, lambda: pygame.PixelArray(weird_surface))
1321+
13121322

13131323
@unittest.skipIf(IS_PYPY, "pypy having issues")
13141324
class PixelArrayArrayInterfaceTest(unittest.TestCase, TestMixin):

0 commit comments

Comments
 (0)