Skip to content

Commit 7c1f2b5

Browse files
authored
Merge pull request #3627 from pygame-community/ankith26-fix-no-pal-8bit
Fix no palette 8 bit surface gfxdraw segfault
2 parents 6f3b01a + 55ef0b9 commit 7c1f2b5

File tree

2 files changed

+49
-18
lines changed

2 files changed

+49
-18
lines changed

src_c/SDL_gfx/SDL_gfxPrimitives.c

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -293,17 +293,13 @@ _putPixelAlpha(SDL_Surface *dst, Sint16 x, Sint16 y, Uint32 color, Uint8 alpha)
293293
*((Uint8 *)dst->pixels + y * dst->pitch + x) = color;
294294
}
295295
else {
296+
/* Patched on pygame-ce end to fix segfault when no palette
297+
*/
296298
Uint8 *pixel = (Uint8 *)dst->pixels + y * dst->pitch + x;
297-
SDL_Palette *palette = format->palette;
298-
SDL_Color *colors = palette->colors;
299-
SDL_Color dColor = colors[*pixel];
300-
SDL_Color sColor = colors[color];
301-
Uint8 dR = dColor.r;
302-
Uint8 dG = dColor.g;
303-
Uint8 dB = dColor.b;
304-
Uint8 sR = sColor.r;
305-
Uint8 sG = sColor.g;
306-
Uint8 sB = sColor.b;
299+
Uint8 dR, dG, dB;
300+
Uint8 sR, sG, sB;
301+
SDL_GetRGB(*pixel, format, &dR, &dG, &dB);
302+
SDL_GetRGB(color, format, &sR, &sG, &sB);
307303

308304
dR = dR + ((sR - dR) * alpha >> 8);
309305
dG = dG + ((sG - dG) * alpha >> 8);
@@ -604,22 +600,17 @@ _filledRectAlpha(SDL_Surface *dst, Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2,
604600
format = dst->format;
605601
switch (GFX_FORMAT_BytesPerPixel(format)) {
606602
case 1: { /* Assuming 8-bpp */
603+
/* Patched on pygame-ce end to fix segfault when no palette */
607604
Uint8 *row, *pixel;
608605
Uint8 dR, dG, dB;
609-
SDL_Palette *palette = format->palette;
610-
SDL_Color *colors = palette->colors;
611-
sR = colors[color].r;
612-
sG = colors[color].g;
613-
sB = colors[color].b;
606+
SDL_GetRGB(color, format, &sR, &sG, &sB);
614607

615608
for (y = y1; y <= y2; y++) {
616609
row = (Uint8 *)dst->pixels + y * dst->pitch;
617610
for (x = x1; x <= x2; x++) {
618611
pixel = row + x;
619612

620-
dR = colors[*pixel].r;
621-
dG = colors[*pixel].g;
622-
dB = colors[*pixel].b;
613+
SDL_GetRGB(*pixel, format, &dR, &dG, &dB);
623614

624615
dR = dR + ((sR - dR) * alpha >> 8);
625616
dG = dG + ((sG - dG) * alpha >> 8);

test/gfxdraw_test.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -872,6 +872,46 @@ def test_bezier(self):
872872
for posn in bg_test_points:
873873
self.check_at(surf, posn, bg_adjusted)
874874

875+
def test_no_pal_8bit_surf(self):
876+
"""
877+
Test that gfxdraw methods do not segfault when passed with no palette
878+
8 bit surface.
879+
"""
880+
s = pygame.Surface(
881+
size=(512, 512), flags=0, depth=8, masks=(0xE0, 0x1C, 0x03, 0x00)
882+
)
883+
884+
fg = (255, 255, 255)
885+
points = [(10, 10), (100, 50), (50, 100)]
886+
for name, args in (
887+
("pixel", (s, 2, 2, fg)),
888+
("hline", (s, 5, 50, 10, fg)),
889+
("vline", (s, 10, 5, 50, fg)),
890+
("line", (s, 0, 0, 100, 100, fg)),
891+
("rectangle", (s, pygame.Rect(50, 50, 80, 40), fg)),
892+
("box", (s, pygame.Rect(60, 60, 80, 40), fg)),
893+
("circle", (s, 256, 256, 64, fg)),
894+
("aacircle", (s, 256, 256, 64, fg)),
895+
("filled_circle", (s, 256, 256, 32, fg)),
896+
("ellipse", (s, 200, 200, 50, 30, fg)),
897+
("aaellipse", (s, 200, 200, 50, 30, fg)),
898+
("filled_ellipse", (s, 200, 200, 50, 30, fg)),
899+
("arc", (s, 256, 256, 60, 0, 180, fg)),
900+
("pie", (s, 300, 300, 60, 0, 270, fg)),
901+
("trigon", (s, 100, 100, 150, 200, 50, 200, fg)),
902+
("aatrigon", (s, 100, 100, 150, 200, 50, 200, fg)),
903+
("filled_trigon", (s, 100, 100, 150, 200, 50, 200, fg)),
904+
("polygon", (s, points, fg)),
905+
("aapolygon", (s, points, fg)),
906+
("filled_polygon", (s, points, fg)),
907+
("textured_polygon", (s, points, s, 0, 0)), # use same surface as texture
908+
):
909+
try:
910+
func = getattr(pygame.gfxdraw, name)
911+
func(*args)
912+
except Exception as e:
913+
self.fail(f"gfxdraw.{name} raised an exception: {e}")
914+
875915

876916
if __name__ == "__main__":
877917
unittest.main()

0 commit comments

Comments
 (0)