Ensure that SDL_InitSubSystem quits subsystems after an error. (#4834)

* Ensure that SDL_InitSubSystem quits subsystems after an error.

* Fix unnecessary change.
This commit is contained in:
Erik Soma 2021-10-12 18:55:31 -04:00 committed by GitHub
parent 6149e60136
commit 0e294e90ae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 39 additions and 17 deletions

View File

@ -150,6 +150,8 @@ SDL_SetMainReady(void)
int int
SDL_InitSubSystem(Uint32 flags) SDL_InitSubSystem(Uint32 flags)
{ {
Uint32 flags_initialized = 0;
if (!SDL_MainIsReady) { if (!SDL_MainIsReady) {
SDL_SetError("Application didn't initialize properly, did you include SDL_main.h in the file containing your main() function?"); SDL_SetError("Application didn't initialize properly, did you include SDL_main.h in the file containing your main() function?");
return -1; return -1;
@ -179,7 +181,7 @@ SDL_InitSubSystem(Uint32 flags)
#if SDL_VIDEO_DRIVER_WINDOWS #if SDL_VIDEO_DRIVER_WINDOWS
if ((flags & (SDL_INIT_HAPTIC|SDL_INIT_JOYSTICK))) { if ((flags & (SDL_INIT_HAPTIC|SDL_INIT_JOYSTICK))) {
if (SDL_HelperWindowCreate() < 0) { if (SDL_HelperWindowCreate() < 0) {
return -1; goto quit_and_error;
} }
} }
#endif #endif
@ -193,12 +195,14 @@ SDL_InitSubSystem(Uint32 flags)
#if !SDL_EVENTS_DISABLED #if !SDL_EVENTS_DISABLED
if (SDL_PrivateShouldInitSubsystem(SDL_INIT_EVENTS)) { if (SDL_PrivateShouldInitSubsystem(SDL_INIT_EVENTS)) {
if (SDL_EventsInit() < 0) { if (SDL_EventsInit() < 0) {
return (-1); goto quit_and_error;
} }
} }
SDL_PrivateSubsystemRefCountIncr(SDL_INIT_EVENTS); SDL_PrivateSubsystemRefCountIncr(SDL_INIT_EVENTS);
flags_initialized |= SDL_INIT_EVENTS;
#else #else
return SDL_SetError("SDL not built with events support"); SDL_SetError("SDL not built with events support");
goto quit_and_error;
#endif #endif
} }
@ -207,12 +211,14 @@ SDL_InitSubSystem(Uint32 flags)
#if !SDL_TIMERS_DISABLED #if !SDL_TIMERS_DISABLED
if (SDL_PrivateShouldInitSubsystem(SDL_INIT_TIMER)) { if (SDL_PrivateShouldInitSubsystem(SDL_INIT_TIMER)) {
if (SDL_TimerInit() < 0) { if (SDL_TimerInit() < 0) {
return (-1); goto quit_and_error;
} }
} }
SDL_PrivateSubsystemRefCountIncr(SDL_INIT_TIMER); SDL_PrivateSubsystemRefCountIncr(SDL_INIT_TIMER);
flags_initialized |= SDL_INIT_TIMER;
#else #else
return SDL_SetError("SDL not built with timer support"); SDL_SetError("SDL not built with timer support");
goto quit_and_error;
#endif #endif
} }
@ -221,12 +227,14 @@ SDL_InitSubSystem(Uint32 flags)
#if !SDL_VIDEO_DISABLED #if !SDL_VIDEO_DISABLED
if (SDL_PrivateShouldInitSubsystem(SDL_INIT_VIDEO)) { if (SDL_PrivateShouldInitSubsystem(SDL_INIT_VIDEO)) {
if (SDL_VideoInit(NULL) < 0) { if (SDL_VideoInit(NULL) < 0) {
return (-1); goto quit_and_error;
} }
} }
SDL_PrivateSubsystemRefCountIncr(SDL_INIT_VIDEO); SDL_PrivateSubsystemRefCountIncr(SDL_INIT_VIDEO);
flags_initialized |= SDL_INIT_VIDEO;
#else #else
return SDL_SetError("SDL not built with video support"); SDL_SetError("SDL not built with video support");
goto quit_and_error;
#endif #endif
} }
@ -235,12 +243,14 @@ SDL_InitSubSystem(Uint32 flags)
#if !SDL_AUDIO_DISABLED #if !SDL_AUDIO_DISABLED
if (SDL_PrivateShouldInitSubsystem(SDL_INIT_AUDIO)) { if (SDL_PrivateShouldInitSubsystem(SDL_INIT_AUDIO)) {
if (SDL_AudioInit(NULL) < 0) { if (SDL_AudioInit(NULL) < 0) {
return (-1); goto quit_and_error;
} }
} }
SDL_PrivateSubsystemRefCountIncr(SDL_INIT_AUDIO); SDL_PrivateSubsystemRefCountIncr(SDL_INIT_AUDIO);
flags_initialized |= SDL_INIT_AUDIO;
#else #else
return SDL_SetError("SDL not built with audio support"); SDL_SetError("SDL not built with audio support");
goto quit_and_error;
#endif #endif
} }
@ -249,12 +259,14 @@ SDL_InitSubSystem(Uint32 flags)
#if !SDL_JOYSTICK_DISABLED #if !SDL_JOYSTICK_DISABLED
if (SDL_PrivateShouldInitSubsystem(SDL_INIT_JOYSTICK)) { if (SDL_PrivateShouldInitSubsystem(SDL_INIT_JOYSTICK)) {
if (SDL_JoystickInit() < 0) { if (SDL_JoystickInit() < 0) {
return (-1); goto quit_and_error;
} }
} }
SDL_PrivateSubsystemRefCountIncr(SDL_INIT_JOYSTICK); SDL_PrivateSubsystemRefCountIncr(SDL_INIT_JOYSTICK);
flags_initialized |= SDL_INIT_JOYSTICK;
#else #else
return SDL_SetError("SDL not built with joystick support"); SDL_SetError("SDL not built with joystick support");
goto quit_and_error;
#endif #endif
} }
@ -262,12 +274,14 @@ SDL_InitSubSystem(Uint32 flags)
#if !SDL_JOYSTICK_DISABLED #if !SDL_JOYSTICK_DISABLED
if (SDL_PrivateShouldInitSubsystem(SDL_INIT_GAMECONTROLLER)) { if (SDL_PrivateShouldInitSubsystem(SDL_INIT_GAMECONTROLLER)) {
if (SDL_GameControllerInit() < 0) { if (SDL_GameControllerInit() < 0) {
return (-1); goto quit_and_error;
} }
} }
SDL_PrivateSubsystemRefCountIncr(SDL_INIT_GAMECONTROLLER); SDL_PrivateSubsystemRefCountIncr(SDL_INIT_GAMECONTROLLER);
flags_initialized |= SDL_INIT_GAMECONTROLLER;
#else #else
return SDL_SetError("SDL not built with joystick support"); SDL_SetError("SDL not built with joystick support");
goto quit_and_error;
#endif #endif
} }
@ -276,12 +290,14 @@ SDL_InitSubSystem(Uint32 flags)
#if !SDL_HAPTIC_DISABLED #if !SDL_HAPTIC_DISABLED
if (SDL_PrivateShouldInitSubsystem(SDL_INIT_HAPTIC)) { if (SDL_PrivateShouldInitSubsystem(SDL_INIT_HAPTIC)) {
if (SDL_HapticInit() < 0) { if (SDL_HapticInit() < 0) {
return (-1); goto quit_and_error;
} }
} }
SDL_PrivateSubsystemRefCountIncr(SDL_INIT_HAPTIC); SDL_PrivateSubsystemRefCountIncr(SDL_INIT_HAPTIC);
flags_initialized |= SDL_INIT_HAPTIC;
#else #else
return SDL_SetError("SDL not built with haptic (force feedback) support"); SDL_SetError("SDL not built with haptic (force feedback) support");
goto quit_and_error;
#endif #endif
} }
@ -290,16 +306,22 @@ SDL_InitSubSystem(Uint32 flags)
#if !SDL_SENSOR_DISABLED #if !SDL_SENSOR_DISABLED
if (SDL_PrivateShouldInitSubsystem(SDL_INIT_SENSOR)) { if (SDL_PrivateShouldInitSubsystem(SDL_INIT_SENSOR)) {
if (SDL_SensorInit() < 0) { if (SDL_SensorInit() < 0) {
return (-1); goto quit_and_error;
} }
} }
SDL_PrivateSubsystemRefCountIncr(SDL_INIT_SENSOR); SDL_PrivateSubsystemRefCountIncr(SDL_INIT_SENSOR);
flags_initialized |= SDL_INIT_SENSOR;
#else #else
return SDL_SetError("SDL not built with sensor support"); SDL_SetError("SDL not built with sensor support");
goto quit_and_error;
#endif #endif
} }
return (0); return (0);
quit_and_error:
SDL_QuitSubSystem(flags_initialized);
return (-1);
} }
int int