Ensure that SDL_InitSubSystem quits subsystems after an error. (#4834) * Ensure that SDL_InitSubSystem quits subsystems after an error. * Fix unnecessary change.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
diff --git a/src/SDL.c b/src/SDL.c
index 73f1cd7..14066aa 100644
--- a/src/SDL.c
+++ b/src/SDL.c
@@ -150,6 +150,8 @@ SDL_SetMainReady(void)
int
SDL_InitSubSystem(Uint32 flags)
{
+ Uint32 flags_initialized = 0;
+
if (!SDL_MainIsReady) {
SDL_SetError("Application didn't initialize properly, did you include SDL_main.h in the file containing your main() function?");
return -1;
@@ -179,7 +181,7 @@ SDL_InitSubSystem(Uint32 flags)
#if SDL_VIDEO_DRIVER_WINDOWS
if ((flags & (SDL_INIT_HAPTIC|SDL_INIT_JOYSTICK))) {
if (SDL_HelperWindowCreate() < 0) {
- return -1;
+ goto quit_and_error;
}
}
#endif
@@ -193,12 +195,14 @@ SDL_InitSubSystem(Uint32 flags)
#if !SDL_EVENTS_DISABLED
if (SDL_PrivateShouldInitSubsystem(SDL_INIT_EVENTS)) {
if (SDL_EventsInit() < 0) {
- return (-1);
+ goto quit_and_error;
}
}
SDL_PrivateSubsystemRefCountIncr(SDL_INIT_EVENTS);
+ flags_initialized |= SDL_INIT_EVENTS;
#else
- return SDL_SetError("SDL not built with events support");
+ SDL_SetError("SDL not built with events support");
+ goto quit_and_error;
#endif
}
@@ -207,12 +211,14 @@ SDL_InitSubSystem(Uint32 flags)
#if !SDL_TIMERS_DISABLED
if (SDL_PrivateShouldInitSubsystem(SDL_INIT_TIMER)) {
if (SDL_TimerInit() < 0) {
- return (-1);
+ goto quit_and_error;
}
}
SDL_PrivateSubsystemRefCountIncr(SDL_INIT_TIMER);
+ flags_initialized |= SDL_INIT_TIMER;
#else
- return SDL_SetError("SDL not built with timer support");
+ SDL_SetError("SDL not built with timer support");
+ goto quit_and_error;
#endif
}
@@ -221,12 +227,14 @@ SDL_InitSubSystem(Uint32 flags)
#if !SDL_VIDEO_DISABLED
if (SDL_PrivateShouldInitSubsystem(SDL_INIT_VIDEO)) {
if (SDL_VideoInit(NULL) < 0) {
- return (-1);
+ goto quit_and_error;
}
}
SDL_PrivateSubsystemRefCountIncr(SDL_INIT_VIDEO);
+ flags_initialized |= SDL_INIT_VIDEO;
#else
- return SDL_SetError("SDL not built with video support");
+ SDL_SetError("SDL not built with video support");
+ goto quit_and_error;
#endif
}
@@ -235,12 +243,14 @@ SDL_InitSubSystem(Uint32 flags)
#if !SDL_AUDIO_DISABLED
if (SDL_PrivateShouldInitSubsystem(SDL_INIT_AUDIO)) {
if (SDL_AudioInit(NULL) < 0) {
- return (-1);
+ goto quit_and_error;
}
}
SDL_PrivateSubsystemRefCountIncr(SDL_INIT_AUDIO);
+ flags_initialized |= SDL_INIT_AUDIO;
#else
- return SDL_SetError("SDL not built with audio support");
+ SDL_SetError("SDL not built with audio support");
+ goto quit_and_error;
#endif
}
@@ -249,12 +259,14 @@ SDL_InitSubSystem(Uint32 flags)
#if !SDL_JOYSTICK_DISABLED
if (SDL_PrivateShouldInitSubsystem(SDL_INIT_JOYSTICK)) {
if (SDL_JoystickInit() < 0) {
- return (-1);
+ goto quit_and_error;
}
}
SDL_PrivateSubsystemRefCountIncr(SDL_INIT_JOYSTICK);
+ flags_initialized |= SDL_INIT_JOYSTICK;
#else
- return SDL_SetError("SDL not built with joystick support");
+ SDL_SetError("SDL not built with joystick support");
+ goto quit_and_error;
#endif
}
@@ -262,12 +274,14 @@ SDL_InitSubSystem(Uint32 flags)
#if !SDL_JOYSTICK_DISABLED
if (SDL_PrivateShouldInitSubsystem(SDL_INIT_GAMECONTROLLER)) {
if (SDL_GameControllerInit() < 0) {
- return (-1);
+ goto quit_and_error;
}
}
SDL_PrivateSubsystemRefCountIncr(SDL_INIT_GAMECONTROLLER);
+ flags_initialized |= SDL_INIT_GAMECONTROLLER;
#else
- return SDL_SetError("SDL not built with joystick support");
+ SDL_SetError("SDL not built with joystick support");
+ goto quit_and_error;
#endif
}
@@ -276,12 +290,14 @@ SDL_InitSubSystem(Uint32 flags)
#if !SDL_HAPTIC_DISABLED
if (SDL_PrivateShouldInitSubsystem(SDL_INIT_HAPTIC)) {
if (SDL_HapticInit() < 0) {
- return (-1);
+ goto quit_and_error;
}
}
SDL_PrivateSubsystemRefCountIncr(SDL_INIT_HAPTIC);
+ flags_initialized |= SDL_INIT_HAPTIC;
#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
}
@@ -290,16 +306,22 @@ SDL_InitSubSystem(Uint32 flags)
#if !SDL_SENSOR_DISABLED
if (SDL_PrivateShouldInitSubsystem(SDL_INIT_SENSOR)) {
if (SDL_SensorInit() < 0) {
- return (-1);
+ goto quit_and_error;
}
}
SDL_PrivateSubsystemRefCountIncr(SDL_INIT_SENSOR);
+ flags_initialized |= SDL_INIT_SENSOR;
#else
- return SDL_SetError("SDL not built with sensor support");
+ SDL_SetError("SDL not built with sensor support");
+ goto quit_and_error;
#endif
}
return (0);
+
+quit_and_error:
+ SDL_QuitSubSystem(flags_initialized);
+ return (-1);
}
int