Skip to content

Commit 0cc57b6

Browse files
authored
Merge pull request #1940 from oddbookworm/segfault_fixes
Surface, Draw, and Transform segfault fixes
2 parents 49f7c98 + 68886aa commit 0cc57b6

File tree

4 files changed

+97
-87
lines changed

4 files changed

+97
-87
lines changed

src_c/draw.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,9 @@ aaline(PyObject *self, PyObject *arg, PyObject *kwargs)
118118
return NULL; /* Exception already set. */
119119
}
120120

121+
surf = pgSurface_AsSurface(surfobj);
122+
SURF_INIT_CHECK(surf)
123+
121124
if (!blend) {
122125
if (PyErr_WarnEx(
123126
PyExc_DeprecationWarning,
@@ -128,8 +131,6 @@ aaline(PyObject *self, PyObject *arg, PyObject *kwargs)
128131
}
129132
}
130133

131-
surf = pgSurface_AsSurface(surfobj);
132-
133134
if (surf->format->BytesPerPixel <= 0 || surf->format->BytesPerPixel > 4) {
134135
return PyErr_Format(PyExc_ValueError,
135136
"unsupported surface bit depth (%d) for drawing",
@@ -191,6 +192,7 @@ line(PyObject *self, PyObject *arg, PyObject *kwargs)
191192
}
192193

193194
surf = pgSurface_AsSurface(surfobj);
195+
SURF_INIT_CHECK(surf)
194196

195197
if (surf->format->BytesPerPixel <= 0 || surf->format->BytesPerPixel > 4) {
196198
return PyErr_Format(PyExc_ValueError,
@@ -265,6 +267,7 @@ aalines(PyObject *self, PyObject *arg, PyObject *kwargs)
265267
}
266268

267269
surf = pgSurface_AsSurface(surfobj);
270+
SURF_INIT_CHECK(surf)
268271

269272
if (surf->format->BytesPerPixel <= 0 || surf->format->BytesPerPixel > 4) {
270273
return PyErr_Format(PyExc_ValueError,
@@ -398,6 +401,7 @@ lines(PyObject *self, PyObject *arg, PyObject *kwargs)
398401
}
399402

400403
surf = pgSurface_AsSurface(surfobj);
404+
SURF_INIT_CHECK(surf)
401405

402406
if (surf->format->BytesPerPixel <= 0 || surf->format->BytesPerPixel > 4) {
403407
return PyErr_Format(PyExc_ValueError,
@@ -520,6 +524,7 @@ arc(PyObject *self, PyObject *arg, PyObject *kwargs)
520524
}
521525

522526
surf = pgSurface_AsSurface(surfobj);
527+
SURF_INIT_CHECK(surf)
523528

524529
if (surf->format->BytesPerPixel <= 0 || surf->format->BytesPerPixel > 4) {
525530
return PyErr_Format(PyExc_ValueError,
@@ -595,6 +600,7 @@ ellipse(PyObject *self, PyObject *arg, PyObject *kwargs)
595600
}
596601

597602
surf = pgSurface_AsSurface(surfobj);
603+
SURF_INIT_CHECK(surf)
598604

599605
if (surf->format->BytesPerPixel <= 0 || surf->format->BytesPerPixel > 4) {
600606
return PyErr_Format(PyExc_ValueError,
@@ -678,6 +684,7 @@ circle(PyObject *self, PyObject *args, PyObject *kwargs)
678684
}
679685

680686
surf = pgSurface_AsSurface(surfobj);
687+
SURF_INIT_CHECK(surf)
681688

682689
if (surf->format->BytesPerPixel <= 0 || surf->format->BytesPerPixel > 4) {
683690
return PyErr_Format(PyExc_ValueError,
@@ -767,6 +774,7 @@ polygon(PyObject *self, PyObject *arg, PyObject *kwargs)
767774
}
768775

769776
surf = pgSurface_AsSurface(surfobj);
777+
SURF_INIT_CHECK(surf)
770778

771779
if (surf->format->BytesPerPixel <= 0 || surf->format->BytesPerPixel > 4) {
772780
return PyErr_Format(PyExc_ValueError,
@@ -884,6 +892,8 @@ rect(PyObject *self, PyObject *args, PyObject *kwargs)
884892
}
885893

886894
surf = pgSurface_AsSurface(surfobj);
895+
SURF_INIT_CHECK(surf)
896+
887897
if (surf->format->BytesPerPixel <= 0 || surf->format->BytesPerPixel > 4) {
888898
return PyErr_Format(PyExc_ValueError,
889899
"unsupported surface bit depth (%d) for drawing",

src_c/include/_pygame.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,13 @@ PYGAMEAPI_EXTERN_SLOTS(math);
480480
* functions in Python 3.
481481
*/
482482

483+
#define SURF_INIT_CHECK(surf) \
484+
{ \
485+
if (!surf) { \
486+
return RAISE(pgExc_SDLError, "Surface is not initialized"); \
487+
} \
488+
}
489+
483490
static PG_INLINE PyObject *
484491
pg_tuple_couple_from_values_int(int val1, int val2)
485492
{

0 commit comments

Comments
 (0)