Skip to content

Segmentation fault when converting an uninitialized surface with argument (3519) #1735

@GalacticEmperor1

Description

@GalacticEmperor1

Issue №3519 opened by Matiiss at 2022-10-26 13:26:27

Environment:

  • Windows 10
  • Python 3.10.6
  • pygame 2.1.3.dev9 (could reproduce on 2.1.2 and 2.1.0 too)

MRE:

import pygame


pygame.display.set_mode((1, 1))

pygame.Surface.__new__(pygame.Surface).convert(24)

It does not really matter what the argument is as long as it's a python object (except for pygame.Surface), it raises this Segmentation fault:

pygame 2.1.3.dev9 (SDL 2.0.22, Python 3.10.6)
Hello from the pygame community. https://www.pygame.org/contribute.html
Fatal Python error: pygame_parachute: (pygame parachute) Segmentation Fault
Python runtime state: initialized

Current thread 0x000017c8 (most recent call first):
  File "C:\Users\Matiiss\PycharmProjects\Something\for_so.py", line 6 in <module>

Extension modules: pygame.base, pygame.constants, pygame.rect, pygame.rwobject, pygame.surflock, pygame.bufferproxy, pygame.math, pygame.surface, pygame.display, pygame.draw, pygame.event, pygame.imageext, pygame.image, pygame.joystick, pygame.key, pygame.mouse, pygame.time, pygame.mask, pygame.pixelcopy, pygame.transform, pygame.font, pygame.mixer_music, pygame.mixer, pygame.scrap, numpy.core._multiarray_umath, numpy.core._multiarray_tests, numpy.linalg._umath_linalg, numpy.fft._pocketfft_internal, numpy.random._common, numpy.random.bit_generator, numpy.random._bounded_integers, numpy.random._mt19937, numpy.random.mtrand, numpy.random._philox, numpy.random._pcg64, numpy.random._sfc64, numpy.random._generator, win32api, _win32sysloader, pygame._freetype (total: 40)

The issue stems from this line https://github.com/pygame/pygame/blob/main/src_c/surface.c# L1449 where all it does is simply check if there is any argument given and then it checks if it is a surface and if it fails that, it tries to memcpy format which doesn't exist if the surface hasn't been initialized (at least how I understand it).

If it's nothing too complicated I'm down to fix this, I'd just like to know what the suggested way to do so would be.

Also I suppose the expected output should be the same as when doing it without an argument (or when it's a surface):

import pygame


pygame.display.set_mode((1, 1))

pygame.Surface.__new__(pygame.Surface).convert()
pygame 2.1.3.dev9 (SDL 2.0.22, Python 3.10.6)
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
  File "C:\Users\Matiiss\PycharmProjects\Something\for_so.py", line 6, in <module>
    pygame.Surface.__new__(pygame.Surface).convert()
pygame.error: Parameter 'surface' is invalid

Comments

# # Matiiss commented at 2022-11-12 23:54:38

This should also be fixed in pygame.transform and pygame.draw modules. Perhaps, other places where you pass a surface argument. As well as check if destination surfaces have been initialized.


# # Matiiss commented at 2022-11-13 08:05:47

Additionally, I suppose some test could be implemented that would iterate over all modules and pass an uninitialized (dead) surface to each method in that module (try/except those cases where such argument is not expected), special cases should be somehow added for when there is an optional destination surface (could just try simply passing as a keyword argument and hope that each method supporting a destination surface supports those), this would then allow to avoid such issues in the future when implementing new methods that deal with surfaces.


# # Starbuck5 commented at 2023-01-01 07:33:46

We've figured out that the display surface is set to NULL when the display is quit. So there is a time when a ->surf is supposed to be NULL, and that explains why some things have a check for this that emits "display surface quit"

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions