Skip to content

Commit e89866a

Browse files
committed
(task_autodetect) reallocate_port_if_needed: get rid of sscanf
1 parent 0644bf9 commit e89866a

1 file changed

Lines changed: 34 additions & 15 deletions

File tree

tasks/task_autodetect.c

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -428,12 +428,12 @@ static void reallocate_port_if_needed(
428428
char settings_value[NAME_MAX_LENGTH];
429429
char settings_value_device_name[NAME_MAX_LENGTH];
430430
unsigned prev_assigned_player_slots[MAX_USERS] = {0};
431-
unsigned int settings_value_vendor_id;
432-
unsigned int settings_value_product_id;
433-
unsigned first_free_player_slot = MAX_USERS + 1;
434-
bool device_has_reserved_slot = false;
435-
bool no_reservation_at_all = true;
436-
settings_t *settings = config_get_ptr();
431+
unsigned int settings_value_vendor_id = 0;
432+
unsigned int settings_value_product_id = 0;
433+
unsigned first_free_player_slot = MAX_USERS + 1;
434+
bool device_has_reserved_slot = false;
435+
bool no_reservation_at_all = true;
436+
settings_t *settings = config_get_ptr();
437437

438438
for (player = 0; player < MAX_USERS; player++)
439439
{
@@ -473,25 +473,47 @@ static void reallocate_port_if_needed(
473473

474474
if (!string_is_empty(settings_value))
475475
{
476+
char *endptr;
477+
char *colon;
478+
unsigned long parsed_vid;
479+
unsigned long parsed_pid;
480+
476481
RARCH_DBG("[Autoconf] Examining reserved device for player %d "
477482
"type %d: \"%s\" against \"%04x:%04x\".\n",
478483
player+1,
479484
settings->uints.input_device_reservation_type[player],
480485
settings_value, vendor_id, product_id);
481486

482-
if (sscanf(settings_value, "%04x:%04x ",
483-
&settings_value_vendor_id,
484-
&settings_value_product_id) != 2)
487+
colon = strchr(settings_value, ':');
488+
parsed_vid = strtoul(settings_value, &endptr, 16);
489+
490+
if (colon && endptr == colon)
491+
{
492+
parsed_pid = strtoul(colon + 1, &endptr, 16);
493+
if (endptr != colon + 1 && (*endptr == ' ' || *endptr == '\0'))
494+
{
495+
settings_value_vendor_id = (unsigned int)parsed_vid;
496+
settings_value_product_id = (unsigned int)parsed_pid;
497+
device_has_reserved_slot = ( vendor_id == settings_value_vendor_id
498+
&& product_id == settings_value_product_id);
499+
}
500+
else
501+
{
502+
strlcpy(settings_value_device_name, settings_value,
503+
sizeof(settings_value_device_name));
504+
device_has_reserved_slot =
505+
string_is_equal(device_name, settings_value_device_name)
506+
|| string_is_equal(device_display_name, settings_value_device_name);
507+
}
508+
}
509+
else
485510
{
486511
strlcpy(settings_value_device_name, settings_value,
487512
sizeof(settings_value_device_name));
488513
device_has_reserved_slot =
489514
string_is_equal(device_name, settings_value_device_name)
490515
|| string_is_equal(device_display_name, settings_value_device_name);
491516
}
492-
else
493-
device_has_reserved_slot = ( vendor_id == settings_value_vendor_id
494-
&& product_id == settings_value_product_id);
495517

496518
if (device_has_reserved_slot)
497519
{
@@ -556,9 +578,6 @@ static void reallocate_port_if_needed(
556578
RARCH_DBG("[Autoconf] Device \"%s\" (%x:%x) is not reserved for "
557579
"any player slot.\n",
558580
device_name, vendor_id, product_id);
559-
/* Fallback in case no reservation is set up at all - to preserve
560-
* any previous setup where input_joypad_index may have been
561-
* customized. */
562581
if ( no_reservation_at_all
563582
|| prev_assigned_player_slots[detected_port] == first_free_player_slot)
564583
return;

0 commit comments

Comments
 (0)