Skip to content

Commit

Permalink
Fix memory leak in PR #15762
Browse files Browse the repository at this point in the history
  • Loading branch information
LibretroAdmin committed Oct 10, 2023
1 parent 21b3baf commit 58f7051
Showing 1 changed file with 23 additions and 14 deletions.
37 changes: 23 additions & 14 deletions retroarch.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 58f7051

Please sign in to comment.