Skip to content

Commit c6869ee

Browse files
improve error checking
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
1 parent 1732ed7 commit c6869ee

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

src_c/transform.c

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4244,21 +4244,33 @@ surf_pixelate(PyObject *self, PyObject *args, PyObject *kwargs)
42444244

42454245
if (pixel_size < 1) {
42464246
PyErr_SetString(PyExc_ValueError,
4247-
"Pixel size must be a nonnegative integer");
4247+
"pixel_size must be greater than 0");
42484248
return NULL;
42494249
}
42504250

4251-
int width = (int)round((double)src->surf->w / pixel_size);
4252-
int height = (int)round((double)src->surf->h / pixel_size);
4251+
SDL_Surface *src_surf = pgSurface_AsSurface(src);
4252+
SURF_INIT_CHECK(src_surf);
4253+
4254+
int width = (int)round((double)src_surf->w / pixel_size);
4255+
int height = (int)round((double)src_surf->h / pixel_size);
4256+
if (width < 1) {
4257+
width = 1;
4258+
}
4259+
if (height < 1) {
4260+
height = 1;
4261+
}
42534262

42544263
SDL_Surface *temp = scale_to(src, NULL, width, height);
4264+
if (!temp) {
4265+
return NULL; /* Exception already set in scale_to */
4266+
}
42554267
intermediate = pgSurface_New(temp);
42564268
if (intermediate == NULL) {
42574269
SDL_FreeSurface(temp);
42584270
return NULL; /* Exception already set in scale_to */
42594271
}
42604272

4261-
new_surf = scale_to(intermediate, dst, src->surf->w, src->surf->h);
4273+
new_surf = scale_to(intermediate, dst, src_surf->w, src_surf->h);
42624274
Py_DECREF(intermediate);
42634275

42644276
if (new_surf == NULL) {

0 commit comments

Comments
 (0)