From 58f7051c6188ee7529e1c21ebc4323d5d2ca4851 Mon Sep 17 00:00:00 2001 From: libretroadmin Date: Tue, 10 Oct 2023 15:38:07 +0200 Subject: [PATCH] Fix memory leak in PR #15762 --- retroarch.c | 37 +++++++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/retroarch.c b/retroarch.c index ddc0ca9eda0..a3960f07a76 100644 --- a/retroarch.c +++ b/retroarch.c @@ -3758,7 +3758,7 @@ bool command_event(enum event_command cmd, void *data) case CMD_EVENT_NETPLAY_INIT: { char tmp_netplay_server[256]; - char tmp_netplay_session[sizeof(tmp_netplay_server)]; + char tmp_netplay_session[256]; char *netplay_server = NULL; char *netplay_session = NULL; unsigned netplay_port = 0; @@ -3774,22 +3774,16 @@ bool command_event(enum event_command cmd, void *data) netplay_server = tmp_netplay_server; netplay_session = tmp_netplay_session; } - - if (p_rarch->connect_mitm_id) + + if (p_rarch->connect_mitm_id) netplay_session = strdup(p_rarch->connect_mitm_id); - - if (p_rarch->connect_host) + + if (p_rarch->connect_host) { free(p_rarch->connect_host); p_rarch->connect_host = NULL; } - if (p_rarch->connect_mitm_id) - { - free(p_rarch->connect_mitm_id); - p_rarch->connect_mitm_id = NULL; - } - if (string_is_empty(netplay_server)) netplay_server = settings->paths.netplay_server; if (!netplay_port) @@ -3798,9 +3792,24 @@ bool command_event(enum event_command cmd, void *data) if (!init_netplay(netplay_server, netplay_port, netplay_session)) { command_event(CMD_EVENT_NETPLAY_DEINIT, NULL); + if (p_rarch->connect_mitm_id) + { + free(p_rarch->connect_mitm_id); + free(netplay_session); + p_rarch->connect_mitm_id = NULL; + netplay_session = NULL; + } return false; } + if (p_rarch->connect_mitm_id) + { + free(p_rarch->connect_mitm_id); + free(netplay_session); + p_rarch->connect_mitm_id = NULL; + netplay_session = NULL; + } + /* Disable rewind & SRAM autosave if it was enabled * TODO/FIXME: Add a setting for these tweaks */ #ifdef HAVE_REWIND @@ -3816,7 +3825,7 @@ bool command_event(enum event_command cmd, void *data) case CMD_EVENT_NETPLAY_INIT_DIRECT: { char netplay_server[256]; - char netplay_session[sizeof(netplay_server)]; + char netplay_session[256]; unsigned netplay_port = 0; command_event(CMD_EVENT_NETPLAY_DEINIT, NULL); @@ -3853,7 +3862,7 @@ bool command_event(enum event_command cmd, void *data) case CMD_EVENT_NETPLAY_INIT_DIRECT_DEFERRED: { char netplay_server[256]; - char netplay_session[sizeof(netplay_server)]; + char netplay_session[256]; unsigned netplay_port = 0; command_event(CMD_EVENT_NETPLAY_DEINIT, NULL); @@ -6061,7 +6070,7 @@ static bool retroarch_parse_input_and_config( netplay_driver_ctl(RARCH_NETPLAY_CTL_ENABLE_CLIENT, NULL); p_rarch->connect_host = strdup(optarg); break; - + case 'T': p_rarch->connect_mitm_id = strdup(optarg); break;