Fix double-free on error

On error, server->serial was freed twice: immediately and in
server_destroy().

Refs #2292 <https://github.com/Genymobile/scrcpy/issues/2292#issuecomment-831424820>
This commit is contained in:
Romain Vimont 2021-05-03 20:39:49 +02:00
parent 233f8e6cc4
commit 1b9dcce23c

View file

@ -417,18 +417,19 @@ server_start(struct server *server, const char *serial,
} }
if (!push_server(serial)) { if (!push_server(serial)) {
goto error1; /* server->serial will be freed on server_destroy() */
return false;
} }
if (!enable_tunnel_any_port(server, params->port_range, if (!enable_tunnel_any_port(server, params->port_range,
params->force_adb_forward)) { params->force_adb_forward)) {
goto error1; return false;
} }
// server will connect to our server socket // server will connect to our server socket
server->process = execute_server(server, params); server->process = execute_server(server, params);
if (server->process == PROCESS_NONE) { if (server->process == PROCESS_NONE) {
goto error2; goto error;
} }
// If the server process dies before connecting to the server socket, then // If the server process dies before connecting to the server socket, then
@ -442,14 +443,14 @@ server_start(struct server *server, const char *serial,
if (!ok) { if (!ok) {
process_terminate(server->process); process_terminate(server->process);
process_wait(server->process, true); // ignore exit code process_wait(server->process, true); // ignore exit code
goto error2; goto error;
} }
server->tunnel_enabled = true; server->tunnel_enabled = true;
return true; return true;
error2: error:
if (!server->tunnel_forward) { if (!server->tunnel_forward) {
bool was_closed = bool was_closed =
atomic_flag_test_and_set(&server->server_socket_closed); atomic_flag_test_and_set(&server->server_socket_closed);
@ -459,8 +460,7 @@ error2:
close_socket(server->server_socket); close_socket(server->server_socket);
} }
disable_tunnel(server); disable_tunnel(server);
error1:
free(server->serial);
return false; return false;
} }