Fix memory leak on portable builds

The function get_server_path() sometimes returned an owned string,
sometimes a non-owned string.

Always return an allocated (owned) string, and free it after usage.
This commit is contained in:
Romain Vimont 2019-12-14 18:13:56 +01:00
parent 31bd95022b
commit 7d5845196e

View file

@ -18,20 +18,31 @@
#define DEFAULT_SERVER_PATH PREFIX "/share/scrcpy/" SERVER_FILENAME #define DEFAULT_SERVER_PATH PREFIX "/share/scrcpy/" SERVER_FILENAME
#define DEVICE_SERVER_PATH "/data/local/tmp/scrcpy-server.jar" #define DEVICE_SERVER_PATH "/data/local/tmp/scrcpy-server.jar"
static const char * static char *
get_server_path(void) { get_server_path(void) {
const char *server_path_env = getenv("SCRCPY_SERVER_PATH"); const char *server_path_env = getenv("SCRCPY_SERVER_PATH");
if (server_path_env) { if (server_path_env) {
LOGD("Using SCRCPY_SERVER_PATH: %s", server_path_env);
// if the envvar is set, use it // if the envvar is set, use it
return server_path_env; char *server_path = SDL_strdup(server_path_env);
if (!server_path) {
LOGE("Could not allocate memory");
return NULL;
}
LOGD("Using SCRCPY_SERVER_PATH: %s", server_path);
return server_path;
} }
#ifndef PORTABLE #ifndef PORTABLE
LOGD("Using server: " DEFAULT_SERVER_PATH); LOGD("Using server: " DEFAULT_SERVER_PATH);
char *server_path = SDL_strdup(DEFAULT_SERVER_PATH);
if (!server_path) {
LOGE("Could not allocate memory");
return NULL;
}
// the absolute path is hardcoded // the absolute path is hardcoded
return DEFAULT_SERVER_PATH; return server_path;
#else #else
// use scrcpy-server in the same directory as the executable // use scrcpy-server in the same directory as the executable
char *executable_path = get_executable_path(); char *executable_path = get_executable_path();
if (!executable_path) { if (!executable_path) {
@ -67,12 +78,17 @@ get_server_path(void) {
static bool static bool
push_server(const char *serial) { push_server(const char *serial) {
const char *server_path = get_server_path(); char *server_path = get_server_path();
if (!server_path) {
return false;
}
if (!is_regular_file(server_path)) { if (!is_regular_file(server_path)) {
LOGE("'%s' does not exist or is not a regular file\n", server_path); LOGE("'%s' does not exist or is not a regular file\n", server_path);
SDL_free(server_path);
return false; return false;
} }
process_t process = adb_push(serial, server_path, DEVICE_SERVER_PATH); process_t process = adb_push(serial, server_path, DEVICE_SERVER_PATH);
SDL_free(server_path);
return process_check_success(process, "adb push"); return process_check_success(process, "adb push");
} }