From e9822fc6fa04a611e23503ae57da013e5a4c313b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20W=C3=BChrer?= Date: Sat, 2 Feb 2019 10:34:59 +0100 Subject: [PATCH] c-lib: strdup for service_address and service_binary MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The functions criu_(local_)set_service_address and criu_(local_)set_service_binary are the only functions that do not create a copy of the given string arguments. This may lead to problems, if the original string gets freed but criu relies on them. Additionally, the function criu_local_init_opts() assigns the default service_binary now to opt->service_binary instead of opt->service_address which is in my opinion the preferred way since both are types of an anonymous union. Signed-off-by: Martin Wührer --- lib/c/criu.c | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/lib/c/criu.c b/lib/c/criu.c index 31692c781..f53315894 100644 --- a/lib/c/criu.c +++ b/lib/c/criu.c @@ -35,6 +35,20 @@ struct criu_opts { static criu_opts *global_opts; static int saved_errno; +void criu_free_service(criu_opts *opts) +{ + switch(opts->service_comm) { + case CRIU_COMM_SK: + free((void*)(opts->service_address)); + break; + case CRIU_COMM_BIN: + free((void*)(opts->service_binary)); + break; + default: + break; + } +} + void criu_local_set_service_comm(criu_opts *opts, enum criu_service_comm comm) { opts->service_comm = comm; @@ -47,10 +61,11 @@ void criu_set_service_comm(enum criu_service_comm comm) void criu_local_set_service_address(criu_opts *opts, const char *path) { + criu_free_service(opts); if (path) - opts->service_address = path; + opts->service_address = strdup(path); else - opts->service_address = CR_DEFAULT_SERVICE_ADDRESS; + opts->service_address = strdup(CR_DEFAULT_SERVICE_ADDRESS); } void criu_set_service_address(const char *path) @@ -60,6 +75,7 @@ void criu_set_service_address(const char *path) void criu_local_set_service_fd(criu_opts *opts, int fd) { + criu_free_service(opts); opts->service_fd = fd; } @@ -70,10 +86,11 @@ void criu_set_service_fd(int fd) void criu_local_set_service_binary(criu_opts *opts, const char *path) { + criu_free_service(opts); if (path) - opts->service_binary = path; + opts->service_binary = strdup(path); else - opts->service_binary = CR_DEFAULT_SERVICE_BIN; + opts->service_binary = strdup(CR_DEFAULT_SERVICE_BIN); } void criu_set_service_binary(const char *path) @@ -205,6 +222,7 @@ void criu_local_free_opts(criu_opts *opts) free(opts->rpc->freeze_cgroup); free(opts->rpc->log_file); free(opts->rpc); + criu_free_service(opts); free(opts); } @@ -237,7 +255,7 @@ int criu_local_init_opts(criu_opts **o) opts->notify = NULL; opts->service_comm = CRIU_COMM_BIN; - opts->service_address = CR_DEFAULT_SERVICE_BIN; + opts->service_binary = strdup(CR_DEFAULT_SERVICE_BIN); *o = opts;