Skip to content

Commit 5313095

Browse files
committed
fix enumeration, possible memory explosion
1 parent 47192eb commit 5313095

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

Diff for: ruby/input/joypad/sdl.cpp

+16-5
Original file line numberDiff line numberDiff line change
@@ -90,15 +90,26 @@ struct InputJoypadSDL {
9090
}
9191
joypads.reset();
9292
int num_joysticks;
93-
SDL_GetJoysticks(&num_joysticks);
94-
for(u32 id : range(num_joysticks)) {
93+
SDL_JoystickID *joysticks = SDL_GetJoysticks(&num_joysticks);
94+
for(int i = 0; i < num_joysticks; i++) {
95+
SDL_JoystickID id = joysticks[i];
9596
Joypad jp;
9697
jp.id = id;
9798
jp.handle = SDL_OpenJoystick(jp.id);
99+
if(!jp.handle) {
100+
const char *err = SDL_GetError();
101+
print("Error opening SDL joystick id ", id, ": ", err);
102+
continue;
103+
}
98104

99-
u32 axes = SDL_GetNumJoystickAxes(jp.handle);
100-
u32 hats = SDL_GetNumJoystickHats(jp.handle) * 2;
101-
u32 buttons = SDL_GetNumJoystickButtons(jp.handle);
105+
s32 axes = SDL_GetNumJoystickAxes(jp.handle);
106+
s32 hats = SDL_GetNumJoystickHats(jp.handle) * 2;
107+
s32 buttons = SDL_GetNumJoystickButtons(jp.handle);
108+
if(axes < 0 || hats < 0 || buttons < 0) {
109+
const char *err = SDL_GetError();
110+
print("Error retrieving SDL joystick information for device ", jp.handle, " at index ", id, ": ", err);
111+
continue;
112+
}
102113

103114
u16 vid = SDL_GetJoystickVendor(jp.handle);
104115
u16 pid = SDL_GetJoystickProduct(jp.handle);

0 commit comments

Comments
 (0)