]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
lxclock: cleanup lxc_newlock()
authorChristian Brauner <christian.brauner@ubuntu.com>
Wed, 9 Dec 2020 10:16:10 +0000 (11:16 +0100)
committerChristian Brauner <christian.brauner@ubuntu.com>
Thu, 10 Dec 2020 19:39:54 +0000 (20:39 +0100)
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
src/lxc/lxclock.c

index a77951a5b4d74fab9bd133f756759eb5d0ed87d7..a1361be40f08ab03875db0b69227432e4667b788 100644 (file)
@@ -17,6 +17,7 @@
 #include "config.h"
 #include "log.h"
 #include "lxclock.h"
+#include "memory_utils.h"
 #include "utils.h"
 
 #ifdef MUTEX_DEBUGGING
@@ -149,35 +150,26 @@ static sem_t *lxc_new_unnamed_sem(void)
 
 struct lxc_lock *lxc_newlock(const char *lxcpath, const char *name)
 {
-       struct lxc_lock *l;
+       __do_free struct lxc_lock *l = NULL;
 
-       l = malloc(sizeof(*l));
+       l = zalloc(sizeof(*l));
        if (!l)
-               goto on_error;
-
-       if (!name) {
+               return ret_set_errno(NULL, ENOMEM);
+
+       if (name) {
+               l->type = LXC_LOCK_FLOCK;
+               l->u.f.fname = lxclock_name(lxcpath, name);
+               if (!l->u.f.fname)
+                       return ret_set_errno(NULL, ENOMEM);
+               l->u.f.fd = -EBADF;
+       } else {
                l->type = LXC_LOCK_ANON_SEM;
                l->u.sem = lxc_new_unnamed_sem();
-               if (!l->u.sem) {
-                       free(l);
-                       l = NULL;
-               }
-
-               goto on_error;
+               if (!l->u.sem)
+                       return ret_set_errno(NULL, ENOMEM);
        }
 
-       l->type = LXC_LOCK_FLOCK;
-       l->u.f.fname = lxclock_name(lxcpath, name);
-       if (!l->u.f.fname) {
-               free(l);
-               l = NULL;
-               goto on_error;
-       }
-
-       l->u.f.fd = -1;
-
-on_error:
-       return l;
+       return move_ptr(l);
 }
 
 int lxclock(struct lxc_lock *l, int timeout)