Replace SDL_strdup() by strdup()

The functions SDL_malloc(), SDL_free() and SDL_strdup() were used only
because strdup() was not available everywhere.

Now that it is available, use the native version of these functions.
This commit is contained in:
Romain Vimont 2021-01-24 15:14:53 +01:00
parent c0dde0fade
commit 30e619d37f
16 changed files with 74 additions and 54 deletions

View file

@ -173,7 +173,7 @@ adb_push(const char *serial, const char *local, const char *remote) {
} }
remote = strquote(remote); remote = strquote(remote);
if (!remote) { if (!remote) {
SDL_free((void *) local); free((void *) local);
return PROCESS_NONE; return PROCESS_NONE;
} }
#endif #endif
@ -182,8 +182,8 @@ adb_push(const char *serial, const char *local, const char *remote) {
process_t proc = adb_execute(serial, adb_cmd, ARRAY_LEN(adb_cmd)); process_t proc = adb_execute(serial, adb_cmd, ARRAY_LEN(adb_cmd));
#ifdef __WINDOWS__ #ifdef __WINDOWS__
SDL_free((void *) remote); free((void *) remote);
SDL_free((void *) local); free((void *) local);
#endif #endif
return proc; return proc;
@ -204,7 +204,7 @@ adb_install(const char *serial, const char *local) {
process_t proc = adb_execute(serial, adb_cmd, ARRAY_LEN(adb_cmd)); process_t proc = adb_execute(serial, adb_cmd, ARRAY_LEN(adb_cmd));
#ifdef __WINDOWS__ #ifdef __WINDOWS__
SDL_free((void *) local); free((void *) local);
#endif #endif
return proc; return proc;

View file

@ -1,6 +1,7 @@
#include "control_msg.h" #include "control_msg.h"
#include <assert.h> #include <assert.h>
#include <stdlib.h>
#include <string.h> #include <string.h>
#include "util/buffer_util.h" #include "util/buffer_util.h"
@ -93,10 +94,10 @@ void
control_msg_destroy(struct control_msg *msg) { control_msg_destroy(struct control_msg *msg) {
switch (msg->type) { switch (msg->type) {
case CONTROL_MSG_TYPE_INJECT_TEXT: case CONTROL_MSG_TYPE_INJECT_TEXT:
SDL_free(msg->inject_text.text); free(msg->inject_text.text);
break; break;
case CONTROL_MSG_TYPE_SET_CLIPBOARD: case CONTROL_MSG_TYPE_SET_CLIPBOARD:
SDL_free(msg->set_clipboard.text); free(msg->set_clipboard.text);
break; break;
default: default:
// do nothing // do nothing

View file

@ -50,7 +50,7 @@ struct control_msg {
enum android_metastate metastate; enum android_metastate metastate;
} inject_keycode; } inject_keycode;
struct { struct {
char *text; // owned, to be freed by SDL_free() char *text; // owned, to be freed by free()
} inject_text; } inject_text;
struct { struct {
enum android_motionevent_action action; enum android_motionevent_action action;
@ -65,7 +65,7 @@ struct control_msg {
int32_t vscroll; int32_t vscroll;
} inject_scroll_event; } inject_scroll_event;
struct { struct {
char *text; // owned, to be freed by SDL_free() char *text; // owned, to be freed by free()
bool paste; bool paste;
} set_clipboard; } set_clipboard;
struct { struct {

View file

@ -1,5 +1,6 @@
#include "device_msg.h" #include "device_msg.h"
#include <stdlib.h>
#include <string.h> #include <string.h>
#include "util/buffer_util.h" #include "util/buffer_util.h"
@ -20,7 +21,7 @@ device_msg_deserialize(const unsigned char *buf, size_t len,
if (clipboard_len > len - 5) { if (clipboard_len > len - 5) {
return 0; // not available return 0; // not available
} }
char *text = SDL_malloc(clipboard_len + 1); char *text = malloc(clipboard_len + 1);
if (!text) { if (!text) {
LOGW("Could not allocate text for clipboard"); LOGW("Could not allocate text for clipboard");
return -1; return -1;
@ -42,6 +43,6 @@ device_msg_deserialize(const unsigned char *buf, size_t len,
void void
device_msg_destroy(struct device_msg *msg) { device_msg_destroy(struct device_msg *msg) {
if (msg->type == DEVICE_MSG_TYPE_CLIPBOARD) { if (msg->type == DEVICE_MSG_TYPE_CLIPBOARD) {
SDL_free(msg->clipboard.text); free(msg->clipboard.text);
} }
} }

View file

@ -19,7 +19,7 @@ struct device_msg {
enum device_msg_type type; enum device_msg_type type;
union { union {
struct { struct {
char *text; // owned, to be freed by SDL_free() char *text; // owned, to be freed by free()
} clipboard; } clipboard;
}; };
}; };

View file

@ -11,7 +11,7 @@
static void static void
file_handler_request_destroy(struct file_handler_request *req) { file_handler_request_destroy(struct file_handler_request *req) {
SDL_free(req->file); free(req->file);
} }
bool bool
@ -30,7 +30,7 @@ file_handler_init(struct file_handler *file_handler, const char *serial,
} }
if (serial) { if (serial) {
file_handler->serial = SDL_strdup(serial); file_handler->serial = strdup(serial);
if (!file_handler->serial) { if (!file_handler->serial) {
LOGW("Could not strdup serial"); LOGW("Could not strdup serial");
SDL_DestroyCond(file_handler->event_cond); SDL_DestroyCond(file_handler->event_cond);
@ -56,7 +56,7 @@ void
file_handler_destroy(struct file_handler *file_handler) { file_handler_destroy(struct file_handler *file_handler) {
SDL_DestroyCond(file_handler->event_cond); SDL_DestroyCond(file_handler->event_cond);
SDL_DestroyMutex(file_handler->mutex); SDL_DestroyMutex(file_handler->mutex);
SDL_free(file_handler->serial); free(file_handler->serial);
struct file_handler_request req; struct file_handler_request req;
while (cbuf_take(&file_handler->queue, &req)) { while (cbuf_take(&file_handler->queue, &req)) {

View file

@ -50,7 +50,7 @@ file_handler_stop(struct file_handler *file_handler);
void void
file_handler_join(struct file_handler *file_handler); file_handler_join(struct file_handler *file_handler);
// take ownership of file, and will SDL_free() it // take ownership of file, and will free() it
bool bool
file_handler_request(struct file_handler *file_handler, file_handler_request(struct file_handler *file_handler,
file_handler_action_t action, file_handler_action_t action,

View file

@ -190,13 +190,20 @@ set_device_clipboard(struct controller *controller, bool paste) {
return; return;
} }
char *text_dup = strdup(text);
SDL_free(text);
if (!text_dup) {
LOGW("Could not strdup input text");
return;
}
struct control_msg msg; struct control_msg msg;
msg.type = CONTROL_MSG_TYPE_SET_CLIPBOARD; msg.type = CONTROL_MSG_TYPE_SET_CLIPBOARD;
msg.set_clipboard.text = text; msg.set_clipboard.text = text_dup;
msg.set_clipboard.paste = paste; msg.set_clipboard.paste = paste;
if (!controller_push_msg(controller, &msg)) { if (!controller_push_msg(controller, &msg)) {
SDL_free(text); free(text_dup);
LOGW("Could not request 'set device clipboard'"); LOGW("Could not request 'set device clipboard'");
} }
} }
@ -242,11 +249,18 @@ clipboard_paste(struct controller *controller) {
return; return;
} }
char *text_dup = strdup(text);
SDL_free(text);
if (!text_dup) {
LOGW("Could not strdup input text");
return;
}
struct control_msg msg; struct control_msg msg;
msg.type = CONTROL_MSG_TYPE_INJECT_TEXT; msg.type = CONTROL_MSG_TYPE_INJECT_TEXT;
msg.inject_text.text = text; msg.inject_text.text = text_dup;
if (!controller_push_msg(controller, &msg)) { if (!controller_push_msg(controller, &msg)) {
SDL_free(text); free(text_dup);
LOGW("Could not request 'paste clipboard'"); LOGW("Could not request 'paste clipboard'");
} }
} }
@ -291,13 +305,13 @@ input_manager_process_text_input(struct input_manager *im,
struct control_msg msg; struct control_msg msg;
msg.type = CONTROL_MSG_TYPE_INJECT_TEXT; msg.type = CONTROL_MSG_TYPE_INJECT_TEXT;
msg.inject_text.text = SDL_strdup(event->text); msg.inject_text.text = strdup(event->text);
if (!msg.inject_text.text) { if (!msg.inject_text.text) {
LOGW("Could not strdup input text"); LOGW("Could not strdup input text");
return; return;
} }
if (!controller_push_msg(im->controller, &msg)) { if (!controller_push_msg(im->controller, &msg)) {
SDL_free(msg.inject_text.text); free(msg.inject_text.text);
LOGW("Could not request 'inject text'"); LOGW("Could not request 'inject text'");
} }
} }

View file

@ -27,7 +27,7 @@ find_muxer(const char *name) {
static struct record_packet * static struct record_packet *
record_packet_new(const AVPacket *packet) { record_packet_new(const AVPacket *packet) {
struct record_packet *rec = SDL_malloc(sizeof(*rec)); struct record_packet *rec = malloc(sizeof(*rec));
if (!rec) { if (!rec) {
return NULL; return NULL;
} }
@ -37,7 +37,7 @@ record_packet_new(const AVPacket *packet) {
av_init_packet(&rec->packet); av_init_packet(&rec->packet);
if (av_packet_ref(&rec->packet, packet)) { if (av_packet_ref(&rec->packet, packet)) {
SDL_free(rec); free(rec);
return NULL; return NULL;
} }
return rec; return rec;
@ -46,7 +46,7 @@ record_packet_new(const AVPacket *packet) {
static void static void
record_packet_delete(struct record_packet *rec) { record_packet_delete(struct record_packet *rec) {
av_packet_unref(&rec->packet); av_packet_unref(&rec->packet);
SDL_free(rec); free(rec);
} }
static void static void
@ -63,7 +63,7 @@ recorder_init(struct recorder *recorder,
const char *filename, const char *filename,
enum sc_record_format format, enum sc_record_format format,
struct size declared_frame_size) { struct size declared_frame_size) {
recorder->filename = SDL_strdup(filename); recorder->filename = strdup(filename);
if (!recorder->filename) { if (!recorder->filename) {
LOGE("Could not strdup filename"); LOGE("Could not strdup filename");
return false; return false;
@ -72,7 +72,7 @@ recorder_init(struct recorder *recorder,
recorder->mutex = SDL_CreateMutex(); recorder->mutex = SDL_CreateMutex();
if (!recorder->mutex) { if (!recorder->mutex) {
LOGC("Could not create mutex"); LOGC("Could not create mutex");
SDL_free(recorder->filename); free(recorder->filename);
return false; return false;
} }
@ -80,7 +80,7 @@ recorder_init(struct recorder *recorder,
if (!recorder->queue_cond) { if (!recorder->queue_cond) {
LOGC("Could not create cond"); LOGC("Could not create cond");
SDL_DestroyMutex(recorder->mutex); SDL_DestroyMutex(recorder->mutex);
SDL_free(recorder->filename); free(recorder->filename);
return false; return false;
} }
@ -99,7 +99,7 @@ void
recorder_destroy(struct recorder *recorder) { recorder_destroy(struct recorder *recorder) {
SDL_DestroyCond(recorder->queue_cond); SDL_DestroyCond(recorder->queue_cond);
SDL_DestroyMutex(recorder->mutex); SDL_DestroyMutex(recorder->mutex);
SDL_free(recorder->filename); free(recorder->filename);
} }
static const char * static const char *

View file

@ -226,13 +226,20 @@ handle_event(SDL_Event *event, const struct scrcpy_options *options) {
if (!options->control) { if (!options->control) {
break; break;
} }
char *file = strdup(event->drop.file);
SDL_free(event->drop.file);
if (!file) {
LOGW("Could not strdup drop filename\n");
break;
}
file_handler_action_t action; file_handler_action_t action;
if (is_apk(event->drop.file)) { if (is_apk(file)) {
action = ACTION_INSTALL_APK; action = ACTION_INSTALL_APK;
} else { } else {
action = ACTION_PUSH_FILE; action = ACTION_PUSH_FILE;
} }
file_handler_request(&file_handler, action, event->drop.file); file_handler_request(&file_handler, action, file);
break; break;
} }
} }
@ -286,7 +293,7 @@ av_log_callback(void *avcl, int level, const char *fmt, va_list vl) {
if (priority == 0) { if (priority == 0) {
return; return;
} }
char *local_fmt = SDL_malloc(strlen(fmt) + 10); char *local_fmt = malloc(strlen(fmt) + 10);
if (!local_fmt) { if (!local_fmt) {
LOGC("Could not allocate string"); LOGC("Could not allocate string");
return; return;
@ -295,7 +302,7 @@ av_log_callback(void *avcl, int level, const char *fmt, va_list vl) {
strcpy(local_fmt, "[FFmpeg] "); strcpy(local_fmt, "[FFmpeg] ");
strcpy(local_fmt + 9, fmt); strcpy(local_fmt + 9, fmt);
SDL_LogMessageV(SDL_LOG_CATEGORY_VIDEO, priority, local_fmt, vl); SDL_LogMessageV(SDL_LOG_CATEGORY_VIDEO, priority, local_fmt, vl);
SDL_free(local_fmt); free(local_fmt);
} }
bool bool

View file

@ -33,7 +33,7 @@ get_server_path(void) {
#ifdef __WINDOWS__ #ifdef __WINDOWS__
char *server_path = utf8_from_wide_char(server_path_env); char *server_path = utf8_from_wide_char(server_path_env);
#else #else
char *server_path = SDL_strdup(server_path_env); char *server_path = strdup(server_path_env);
#endif #endif
if (!server_path) { if (!server_path) {
LOGE("Could not allocate memory"); LOGE("Could not allocate memory");
@ -45,7 +45,7 @@ get_server_path(void) {
#ifndef PORTABLE #ifndef PORTABLE
LOGD("Using server: " DEFAULT_SERVER_PATH); LOGD("Using server: " DEFAULT_SERVER_PATH);
char *server_path = SDL_strdup(DEFAULT_SERVER_PATH); char *server_path = strdup(DEFAULT_SERVER_PATH);
if (!server_path) { if (!server_path) {
LOGE("Could not allocate memory"); LOGE("Could not allocate memory");
return NULL; return NULL;
@ -67,11 +67,11 @@ get_server_path(void) {
// sizeof(SERVER_FILENAME) gives statically the size including the null byte // sizeof(SERVER_FILENAME) gives statically the size including the null byte
size_t len = dirlen + 1 + sizeof(SERVER_FILENAME); size_t len = dirlen + 1 + sizeof(SERVER_FILENAME);
char *server_path = SDL_malloc(len); char *server_path = malloc(len);
if (!server_path) { if (!server_path) {
LOGE("Could not alloc server path string, " LOGE("Could not alloc server path string, "
"using " SERVER_FILENAME " from current directory"); "using " SERVER_FILENAME " from current directory");
SDL_free(executable_path); free(executable_path);
return SERVER_FILENAME; return SERVER_FILENAME;
} }
@ -80,7 +80,7 @@ get_server_path(void) {
memcpy(&server_path[dirlen + 1], SERVER_FILENAME, sizeof(SERVER_FILENAME)); memcpy(&server_path[dirlen + 1], SERVER_FILENAME, sizeof(SERVER_FILENAME));
// the final null byte has been copied with SERVER_FILENAME // the final null byte has been copied with SERVER_FILENAME
SDL_free(executable_path); free(executable_path);
LOGD("Using server (portable): %s", server_path); LOGD("Using server (portable): %s", server_path);
return server_path; return server_path;
@ -95,11 +95,11 @@ push_server(const char *serial) {
} }
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); 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); free(server_path);
return process_check_success(process, "adb push", true); return process_check_success(process, "adb push", true);
} }
@ -412,7 +412,7 @@ bool
server_start(struct server *server, const char *serial, server_start(struct server *server, const char *serial,
const struct server_params *params) { const struct server_params *params) {
if (serial) { if (serial) {
server->serial = SDL_strdup(serial); server->serial = strdup(serial);
if (!server->serial) { if (!server->serial) {
return false; return false;
} }
@ -462,7 +462,7 @@ error2:
} }
disable_tunnel(server); disable_tunnel(server);
error1: error1:
SDL_free(server->serial); free(server->serial);
return false; return false;
} }
@ -557,7 +557,7 @@ server_stop(struct server *server) {
void void
server_destroy(struct server *server) { server_destroy(struct server *server) {
SDL_free(server->serial); free(server->serial);
SDL_DestroyCond(server->process_terminated_cond); SDL_DestroyCond(server->process_terminated_cond);
SDL_DestroyMutex(server->mutex); SDL_DestroyMutex(server->mutex);
} }

View file

@ -156,7 +156,7 @@ get_executable_path(void) {
return NULL; return NULL;
} }
buf[len] = '\0'; buf[len] = '\0';
return SDL_strdup(buf); return strdup(buf);
#else #else
// in practice, we only need this feature for portable builds, only used on // in practice, we only need this feature for portable builds, only used on
// Windows, so we don't care implementing it for every platform // Windows, so we don't care implementing it for every platform

View file

@ -41,7 +41,7 @@ process_execute(const char *const argv[], HANDLE *handle) {
if (!CreateProcessW(NULL, wide, NULL, NULL, FALSE, 0, NULL, NULL, &si, if (!CreateProcessW(NULL, wide, NULL, NULL, FALSE, 0, NULL, NULL, &si,
&pi)) { &pi)) {
SDL_free(wide); free(wide);
*handle = NULL; *handle = NULL;
if (GetLastError() == ERROR_FILE_NOT_FOUND) { if (GetLastError() == ERROR_FILE_NOT_FOUND) {
return PROCESS_ERROR_MISSING_BINARY; return PROCESS_ERROR_MISSING_BINARY;
@ -49,7 +49,7 @@ process_execute(const char *const argv[], HANDLE *handle) {
return PROCESS_ERROR_GENERIC; return PROCESS_ERROR_GENERIC;
} }
SDL_free(wide); free(wide);
*handle = pi.hProcess; *handle = pi.hProcess;
return PROCESS_SUCCESS; return PROCESS_SUCCESS;
} }
@ -105,7 +105,7 @@ is_regular_file(const char *path) {
struct _stat path_stat; struct _stat path_stat;
int r = _wstat(wide_path, &path_stat); int r = _wstat(wide_path, &path_stat);
SDL_free(wide_path); free(wide_path);
if (r) { if (r) {
perror("stat"); perror("stat");

View file

@ -70,7 +70,7 @@ search_executable(const char *file);
#endif #endif
// return the absolute path of the executable (the scrcpy binary) // return the absolute path of the executable (the scrcpy binary)
// may be NULL on error; to be freed by SDL_free // may be NULL on error; to be freed by free()
char * char *
get_executable_path(void); get_executable_path(void);

View file

@ -10,8 +10,6 @@
# include <tchar.h> # include <tchar.h>
#endif #endif
#include <SDL2/SDL_stdinc.h>
size_t size_t
xstrncpy(char *dest, const char *src, size_t n) { xstrncpy(char *dest, const char *src, size_t n) {
size_t i; size_t i;
@ -49,7 +47,7 @@ truncated:
char * char *
strquote(const char *src) { strquote(const char *src) {
size_t len = strlen(src); size_t len = strlen(src);
char *quoted = SDL_malloc(len + 3); char *quoted = malloc(len + 3);
if (!quoted) { if (!quoted) {
return NULL; return NULL;
} }
@ -167,7 +165,7 @@ utf8_to_wide_char(const char *utf8) {
return NULL; return NULL;
} }
wchar_t *wide = SDL_malloc(len * sizeof(wchar_t)); wchar_t *wide = malloc(len * sizeof(wchar_t));
if (!wide) { if (!wide) {
return NULL; return NULL;
} }
@ -183,7 +181,7 @@ utf8_from_wide_char(const wchar_t *ws) {
return NULL; return NULL;
} }
char *utf8 = SDL_malloc(len); char *utf8 = malloc(len);
if (!utf8) { if (!utf8) {
return NULL; return NULL;
} }

View file

@ -4,7 +4,6 @@
#include <limits.h> #include <limits.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <SDL2/SDL.h>
#include "util/str_util.h" #include "util/str_util.h"
@ -138,7 +137,7 @@ static void test_strquote(void) {
// add '"' at the beginning and the end // add '"' at the beginning and the end
assert(!strcmp("\"abcde\"", out)); assert(!strcmp("\"abcde\"", out));
SDL_free(out); free(out);
} }
static void test_utf8_truncate(void) { static void test_utf8_truncate(void) {