libretro: Check 'environ_cb' in controller setters
authorAdrien Plazas <kekun.plazas@laposte.net>
Fri, 29 Jul 2016 22:56:59 +0000 (00:56 +0200)
committerAdrien Plazas <kekun.plazas@laposte.net>
Fri, 29 Jul 2016 23:03:27 +0000 (01:03 +0200)
Check that 'environ_cb' is valid before using it in update_multitap()
and update_controller_port_device() as they may be called from
retro_set_controller_port_device() before 'environ_cb' is set.

frontend/libretro.c

index b812d91..936776e 100644 (file)
@@ -383,6 +383,7 @@ static void update_controller_port_device(unsigned port, unsigned device)
                return;
 
        struct retro_variable var;
+       int default_case = 0;
 
        var.value = NULL;
        switch (port) {
@@ -412,7 +413,7 @@ static void update_controller_port_device(unsigned port, unsigned device)
                break;
        }
 
-       if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || var.value)
+       if (environ_cb && (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || var.value))
        {
                if (strcmp(var.value, "standard") == 0)
                        in_type[0] = PSE_PAD_TYPE_STANDARD;
@@ -423,65 +424,81 @@ static void update_controller_port_device(unsigned port, unsigned device)
                else if (strcmp(var.value, "none") == 0)
                        in_type[0] = PSE_PAD_TYPE_NONE;
                else // 'default' case
+                       default_case = 1;
+       }
+       else
+               default_case = 1;
+
+       if (default_case)
+               switch (device)
                {
-                       switch (device)
-                       {
-                       case RETRO_DEVICE_JOYPAD:
-                               in_type[port] = PSE_PAD_TYPE_STANDARD;
-                               break;
-                       case RETRO_DEVICE_ANALOG:
-                               in_type[port] = PSE_PAD_TYPE_ANALOGPAD;
-                               break;
-                       case RETRO_DEVICE_MOUSE:
-                               in_type[port] = PSE_PAD_TYPE_MOUSE;
-                               break;
-                       case RETRO_DEVICE_LIGHTGUN:
-                               in_type[port] = PSE_PAD_TYPE_GUN;
-                               break;
-                       case RETRO_DEVICE_NONE:
-                       default:
-                               in_type[port] = PSE_PAD_TYPE_NONE;
-                       }
+               case RETRO_DEVICE_JOYPAD:
+                       in_type[port] = PSE_PAD_TYPE_STANDARD;
+                       break;
+               case RETRO_DEVICE_ANALOG:
+                       in_type[port] = PSE_PAD_TYPE_ANALOGPAD;
+                       break;
+               case RETRO_DEVICE_MOUSE:
+                       in_type[port] = PSE_PAD_TYPE_MOUSE;
+                       break;
+               case RETRO_DEVICE_LIGHTGUN:
+                       in_type[port] = PSE_PAD_TYPE_GUN;
+                       break;
+               case RETRO_DEVICE_NONE:
+               default:
+                       in_type[port] = PSE_PAD_TYPE_NONE;
                }
-       }
 }
 
 static void update_multitap()
 {
        struct retro_variable var;
+       int auto_case;
 
        var.value = NULL;
        var.key = "pcsx_rearmed_multitap1";
-       if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || var.value)
+       auto_case = 0;
+       if (environ_cb && (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || var.value))
        {
                if (strcmp(var.value, "enabled") == 0)
                        multitap1 = 1;
                else if (strcmp(var.value, "disabled") == 0)
                        multitap1 = 0;
                else // 'auto' case
-               {
-                       // If a gamepad is plugged after port 2, we need a first multitap.
-                       multitap1 = 0;
-                       for (int port = 2; port < PORTS_NUMBER; port++)
-                               multitap1 |= in_type[port] != PSE_PAD_TYPE_NONE;
-               }
+                       auto_case = 1;
+       }
+       else
+               auto_case == 1;
+
+       if (auto_case)
+       {
+               // If a gamepad is plugged after port 2, we need a first multitap.
+               multitap1 = 0;
+               for (int port = 2; port < PORTS_NUMBER; port++)
+                       multitap1 |= in_type[port] != PSE_PAD_TYPE_NONE;
        }
 
        var.value = NULL;
        var.key = "pcsx_rearmed_multitap2";
-       if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || var.value)
+       auto_case = 0;
+       if (environ_cb && (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || var.value))
        {
                if (strcmp(var.value, "enabled") == 0)
                        multitap2 = 1;
                else if (strcmp(var.value, "disabled") == 0)
                        multitap2 = 0;
                else // 'auto' case
-               {
-                       // If a gamepad is plugged after port 4, we need a second multitap.
-                       multitap2 = 0;
-                       for (int port = 4; port < PORTS_NUMBER; port++)
-                               multitap2 |= in_type[port] != PSE_PAD_TYPE_NONE;
-               }
+                       auto_case = 1;
+       }
+       else
+               auto_case == 1;
+
+       if (auto_case)
+       {
+               // If a gamepad is plugged after port 4, we need a second multitap.
+               multitap2 = 0;
+               for (int port = 4; port < PORTS_NUMBER; port++)
+                       multitap2 |= in_type[port] != PSE_PAD_TYPE_NONE;
        }
 }