fixed and added some range checks for joysticks
[rocksndiamonds.git] / src / libgame / sdl.c
index ee43dc05b0c1b1795328b129d1ca0a563f0398e2..04fa05e8c5151be5721ad9c07bcce1e1f06173ca 100644 (file)
@@ -2603,7 +2603,7 @@ static boolean sdl_is_controller[MAX_PLAYERS];
 
 static boolean SDLOpenJoystick(int nr)
 {
-  if (nr < 0 || nr > MAX_PLAYERS)
+  if (nr < 0 || nr >= MAX_PLAYERS)
     return FALSE;
 
 #if defined(TARGET_SDL2)
@@ -2631,7 +2631,7 @@ static boolean SDLOpenJoystick(int nr)
 
 static void SDLCloseJoystick(int nr)
 {
-  if (nr < 0 || nr > MAX_PLAYERS)
+  if (nr < 0 || nr >= MAX_PLAYERS)
     return;
 
 #if 1
@@ -2659,7 +2659,7 @@ static void SDLCloseJoystick(int nr)
 
 boolean SDLCheckJoystickOpened(int nr)
 {
-  if (nr < 0 || nr > MAX_PLAYERS)
+  if (nr < 0 || nr >= MAX_PLAYERS)
     return FALSE;
 
 #if defined(TARGET_SDL2)
@@ -2680,6 +2680,9 @@ static void setJoystickAxis(int nr, int axis_id_raw, int axis_value)
   int axis_id = axis_id_raw % 2;
 #endif
 
+  if (nr < 0 || nr >= MAX_PLAYERS)
+    return;
+
   if (axis_id == -1)
     return;
 
@@ -2723,6 +2726,9 @@ static void setJoystickButton(int nr, int button_id_raw, int button_state)
   int button_id = button_id_raw % 2;
 #endif
 
+  if (nr < 0 || nr >= MAX_PLAYERS)
+    return;
+
   if (button_id == -1)
     return;
 
@@ -2913,8 +2919,8 @@ void SDLInitJoysticks()
       joystick_nr = -1;
     }
 
-    /* misuse joystick file descriptor variable to store joystick number */
-    joystick.fd[i] = joystick_nr;
+    /* store configured joystick number for each player */
+    joystick.nr[i] = joystick_nr;
   }
 
   /* now open all connected joysticks (regardless if configured or not) */