]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
confile: cleanup set_config_proc()
authorChristian Brauner <christian.brauner@ubuntu.com>
Tue, 8 Dec 2020 14:19:33 +0000 (15:19 +0100)
committerChristian Brauner <christian.brauner@ubuntu.com>
Tue, 8 Dec 2020 14:40:50 +0000 (15:40 +0100)
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
src/lxc/conf.h
src/lxc/confile.c

index 1087a75329438cd00d484c3a447ff83d7a2366e2..116479df94006c6527561e0bd4b814632a78a04a 100644 (file)
@@ -142,6 +142,16 @@ struct lxc_proc {
        char *value;
 };
 
+static void free_lxc_proc(struct lxc_proc *ptr)
+{
+       if (ptr) {
+               free(ptr->filename);
+               free(ptr->value);
+               free_disarm(ptr);
+       }
+}
+define_cleanup_function(struct lxc_proc *, free_lxc_proc);
+
 /*
  * id_map is an id map entry.  Form in confile is:
  * lxc.idmap = u 0    9800 100
index 63435f56b1d2f4fef7f3129a9c1d97c6ce21f521..a737bd778fb9729a6e0f8371d65558ab999f58bb 100644 (file)
@@ -2002,9 +2002,9 @@ static int set_config_sysctl(const char *key, const char *value,
 static int set_config_proc(const char *key, const char *value,
                            struct lxc_conf *lxc_conf, void *data)
 {
+       __do_free struct lxc_list *proclist = NULL;
+       call_cleaner(free_lxc_proc) struct lxc_proc *procelem = NULL;
        const char *subkey;
-       struct lxc_list *proclist = NULL;
-       struct lxc_proc *procelem = NULL;
 
        if (lxc_config_value_empty(value))
                return clr_config_proc(key, lxc_conf, NULL);
@@ -2014,39 +2014,29 @@ static int set_config_proc(const char *key, const char *value,
 
        subkey = key + STRLITERALLEN("lxc.proc.");
        if (*subkey == '\0')
-               return -EINVAL;
+               return ret_errno(EINVAL);
 
        proclist = malloc(sizeof(*proclist));
        if (!proclist)
-               goto on_error;
+               return ret_errno(ENOMEM);
 
        procelem = malloc(sizeof(*procelem));
        if (!procelem)
-               goto on_error;
+               return ret_errno(ENOMEM);
        memset(procelem, 0, sizeof(*procelem));
 
        procelem->filename = strdup(subkey);
-       procelem->value = strdup(value);
-
-       if (!procelem->filename || !procelem->value)
-               goto on_error;
+       if (!procelem->filename)
+               return ret_errno(ENOMEM);
 
-       proclist->elem = procelem;
+       procelem->value = strdup(value);
+       if (!procelem->value)
+               return ret_errno(ENOMEM);
 
-       lxc_list_add_tail(&lxc_conf->procs, proclist);
+       proclist->elem = move_ptr(procelem);
+       lxc_list_add_tail(&lxc_conf->procs, move_ptr(proclist));
 
        return 0;
-
-on_error:
-       free(proclist);
-
-       if (procelem) {
-               free(procelem->filename);
-               free(procelem->value);
-               free(procelem);
-       }
-
-       return -1;
 }
 
 static int set_config_idmaps(const char *key, const char *value,