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.
#define DEFAULT_SERVER_PATH PREFIX "/share/scrcpy/" SERVER_FILENAME
#define DEVICE_SERVER_PATH "/data/local/tmp/scrcpy-server.jar"
-static const char *
+static char *
get_server_path(void) {
const char *server_path_env = getenv("SCRCPY_SERVER_PATH");
if (server_path_env) {
- LOGD("Using SCRCPY_SERVER_PATH: %s", server_path_env);
// 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
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
- return DEFAULT_SERVER_PATH;
+ return server_path;
#else
+
// use scrcpy-server in the same directory as the executable
char *executable_path = get_executable_path();
if (!executable_path) {
@@ -67,12 +78,17 @@ get_server_path(void) {
static bool
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)) {
LOGE("'%s' does not exist or is not a regular file\n", server_path);
+ SDL_free(server_path);
return false;
}
process_t process = adb_push(serial, server_path, DEVICE_SERVER_PATH);
+ SDL_free(server_path);
return process_check_success(process, "adb push");
}
