]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
Introduce _cleanup_(unit_freep)
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Fri, 9 Mar 2018 20:34:28 +0000 (21:34 +0100)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Sun, 11 Mar 2018 15:33:58 +0000 (16:33 +0100)
src/core/manager.c
src/core/unit.c
src/core/unit.h
src/test/test-unit-file.c

index c361a70b79da2b23db1389cebbf6cb493a37af06..fb0743b3d7628ade994736ff30266f6481fcbbb3 100644 (file)
@@ -1696,6 +1696,7 @@ int manager_load_unit_prepare(
                 sd_bus_error *e,
                 Unit **_ret) {
 
+        _cleanup_(unit_freep) Unit *cleanup_ret = NULL;
         Unit *ret;
         UnitType t;
         int r;
@@ -1728,29 +1729,26 @@ int manager_load_unit_prepare(
                 return 1;
         }
 
-        ret = unit_new(m, unit_vtable[t]->object_size);
+        ret = cleanup_ret = unit_new(m, unit_vtable[t]->object_size);
         if (!ret)
                 return -ENOMEM;
 
         if (path) {
                 ret->fragment_path = strdup(path);
-                if (!ret->fragment_path) {
-                        unit_free(ret);
+                if (!ret->fragment_path)
                         return -ENOMEM;
-                }
         }
 
         r = unit_add_name(ret, name);
-        if (r < 0) {
-                unit_free(ret);
+        if (r < 0)
                 return r;
-        }
 
         unit_add_to_load_queue(ret);
         unit_add_to_dbus_queue(ret);
         unit_add_to_gc_queue(ret);
 
         *_ret = ret;
+        cleanup_ret = NULL;
 
         return 0;
 }
index c3056624ef237192a69a7a706cab0201ab672790..815701ad4e99898b3c4a689727ad32acc5e7f98e 100644 (file)
@@ -128,7 +128,7 @@ Unit *unit_new(Manager *m, size_t size) {
 }
 
 int unit_new_for_name(Manager *m, size_t size, const char *name, Unit **ret) {
-        Unit *u;
+        _cleanup_(unit_freep) Unit *u = NULL;
         int r;
 
         u = unit_new(m, size);
@@ -136,12 +136,11 @@ int unit_new_for_name(Manager *m, size_t size, const char *name, Unit **ret) {
                 return -ENOMEM;
 
         r = unit_add_name(u, name);
-        if (r < 0) {
-                unit_free(u);
+        if (r < 0)
                 return r;
-        }
 
         *ret = u;
+        u = NULL;
         return r;
 }
 
index e903bf8ad7ef461481894bd04cdc14a366efb3b7..e9370a4b93c94c96e10754ed7a60008e656e1aa2 100644 (file)
@@ -610,6 +610,7 @@ DEFINE_CAST(SCOPE, Scope);
 
 Unit *unit_new(Manager *m, size_t size);
 void unit_free(Unit *u);
+DEFINE_TRIVIAL_CLEANUP_FUNC(Unit *, unit_free);
 
 int unit_new_for_name(Manager *m, size_t size, const char *name, Unit **ret);
 int unit_add_name(Unit *u, const char *name);
index f52a853c98015ca85aa97ed0ca16f74cc17f8932..beb41e97bdbfd463b91b282590c7819367669651 100644 (file)
@@ -114,7 +114,7 @@ static void test_config_parse_exec(void) {
         ExecCommand *c = NULL, *c1;
         const char *ccc;
         _cleanup_(manager_freep) Manager *m = NULL;
-        Unit *u = NULL;
+        _cleanup_(unit_freep) Unit *u = NULL;
 
         r = manager_new(UNIT_FILE_USER, MANAGER_TEST_RUN_MINIMAL, &m);
         if (MANAGER_SKIP_TEST(r)) {
@@ -441,8 +441,6 @@ static void test_config_parse_exec(void) {
         assert_se(c == NULL);
 
         exec_command_free_list(c);
-
-        unit_free(u);
 }
 
 static void test_config_parse_log_extra_fields(void) {
@@ -461,7 +459,7 @@ static void test_config_parse_log_extra_fields(void) {
         int r;
 
         _cleanup_(manager_freep) Manager *m = NULL;
-        Unit *u = NULL;
+        _cleanup_(unit_freep) Unit *u = NULL;
         ExecContext c = {};
 
         r = manager_new(UNIT_FILE_USER, MANAGER_TEST_RUN_MINIMAL, &m);
@@ -506,8 +504,6 @@ static void test_config_parse_log_extra_fields(void) {
 
         exec_context_free_log_extra_fields(&c);
 
-        unit_free(u);
-
         log_info("/* %s – bye */", __func__);
 }