diff options
-rw-r--r-- | src/basic/calendarspec.c | 104 | ||||
-rw-r--r-- | src/basic/env-util.c | 13 | ||||
-rw-r--r-- | src/basic/strv.c | 12 | ||||
-rw-r--r-- | src/core/load-fragment.c | 8 | ||||
-rw-r--r-- | src/journal/sd-journal.c | 70 | ||||
-rw-r--r-- | src/libsystemd/sd-bus/bus-message.c | 86 |
6 files changed, 112 insertions, 181 deletions
diff --git a/src/basic/calendarspec.c b/src/basic/calendarspec.c index a30de78dc2..4cf21100cc 100644 --- a/src/basic/calendarspec.c +++ b/src/basic/calendarspec.c @@ -891,7 +891,7 @@ fail: int calendar_spec_from_string(const char *p, CalendarSpec **spec) { const char *utc; - CalendarSpec *c; + _cleanup_(calendar_spec_freep) CalendarSpec *c = NULL; int r; assert(p); @@ -939,58 +939,54 @@ int calendar_spec_from_string(const char *p, CalendarSpec **spec) { last_space = strrchr(p, ' '); if (last_space != NULL && timezone_is_valid(last_space + 1)) { c->timezone = strdup(last_space + 1); - if (!c->timezone) { - r = -ENOMEM; - goto fail; - } + if (!c->timezone) + return -ENOMEM; p = strndupa(p, last_space - p); } } } - if (isempty(p)) { - r = -EINVAL; - goto fail; - } + if (isempty(p)) + return -EINVAL; if (strcaseeq(p, "minutely")) { r = const_chain(0, &c->microsecond); if (r < 0) - goto fail; + return r; } else if (strcaseeq(p, "hourly")) { r = const_chain(0, &c->minute); if (r < 0) - goto fail; + return r; r = const_chain(0, &c->microsecond); if (r < 0) - goto fail; + return r; } else if (strcaseeq(p, "daily")) { r = const_chain(0, &c->hour); if (r < 0) - goto fail; + return r; r = const_chain(0, &c->minute); if (r < 0) - goto fail; + return r; r = const_chain(0, &c->microsecond); if (r < 0) - goto fail; + return r; } else if (strcaseeq(p, "monthly")) { r = const_chain(1, &c->day); if (r < 0) - goto fail; + return r; r = const_chain(0, &c->hour); if (r < 0) - goto fail; + return r; r = const_chain(0, &c->minute); if (r < 0) - goto fail; + return r; r = const_chain(0, &c->microsecond); if (r < 0) - goto fail; + return r; } else if (strcaseeq(p, "annually") || strcaseeq(p, "yearly") || @@ -998,19 +994,19 @@ int calendar_spec_from_string(const char *p, CalendarSpec **spec) { r = const_chain(1, &c->month); if (r < 0) - goto fail; + return r; r = const_chain(1, &c->day); if (r < 0) - goto fail; + return r; r = const_chain(0, &c->hour); if (r < 0) - goto fail; + return r; r = const_chain(0, &c->minute); if (r < 0) - goto fail; + return r; r = const_chain(0, &c->microsecond); if (r < 0) - goto fail; + return r; } else if (strcaseeq(p, "weekly")) { @@ -1018,40 +1014,40 @@ int calendar_spec_from_string(const char *p, CalendarSpec **spec) { r = const_chain(0, &c->hour); if (r < 0) - goto fail; + return r; r = const_chain(0, &c->minute); if (r < 0) - goto fail; + return r; r = const_chain(0, &c->microsecond); if (r < 0) - goto fail; + return r; } else if (strcaseeq(p, "quarterly")) { r = const_chain(1, &c->month); if (r < 0) - goto fail; + return r; r = const_chain(4, &c->month); if (r < 0) - goto fail; + return r; r = const_chain(7, &c->month); if (r < 0) - goto fail; + return r; r = const_chain(10, &c->month); if (r < 0) - goto fail; + return r; r = const_chain(1, &c->day); if (r < 0) - goto fail; + return r; r = const_chain(0, &c->hour); if (r < 0) - goto fail; + return r; r = const_chain(0, &c->minute); if (r < 0) - goto fail; + return r; r = const_chain(0, &c->microsecond); if (r < 0) - goto fail; + return r; } else if (strcaseeq(p, "biannually") || strcaseeq(p, "bi-annually") || @@ -1060,59 +1056,51 @@ int calendar_spec_from_string(const char *p, CalendarSpec **spec) { r = const_chain(1, &c->month); if (r < 0) - goto fail; + return r; r = const_chain(7, &c->month); if (r < 0) - goto fail; + return r; r = const_chain(1, &c->day); if (r < 0) - goto fail; + return r; r = const_chain(0, &c->hour); if (r < 0) - goto fail; + return r; r = const_chain(0, &c->minute); if (r < 0) - goto fail; + return r; r = const_chain(0, &c->microsecond); if (r < 0) - goto fail; + return r; } else { r = parse_weekdays(&p, c); if (r < 0) - goto fail; + return r; r = parse_date(&p, c); if (r < 0) - goto fail; + return r; if (r == 0) { r = parse_calendar_time(&p, c); if (r < 0) - goto fail; + return r; } - if (*p != 0) { - r = -EINVAL; - goto fail; - } + if (*p != 0) + return -EINVAL; } r = calendar_spec_normalize(c); if (r < 0) - goto fail; + return r; - if (!calendar_spec_valid(c)) { - r = -EINVAL; - goto fail; - } + if (!calendar_spec_valid(c)) + return -EINVAL; - *spec = c; + *spec = TAKE_PTR(c); return 0; - -fail: - calendar_spec_free(c); - return r; } static int find_end_of_month(struct tm *tm, bool utc, int day) { diff --git a/src/basic/env-util.c b/src/basic/env-util.c index 0ebf66c572..c17c5f7bae 100644 --- a/src/basic/env-util.c +++ b/src/basic/env-util.c @@ -417,7 +417,8 @@ int strv_env_replace(char ***l, char *p) { char **strv_env_set(char **x, const char *p) { - char **k, **r; + char **k; + _cleanup_strv_free_ char **r = NULL; char* m[2] = { (char*) p, NULL }; /* Overrides the env var setting of p, returns a new copy */ @@ -428,18 +429,14 @@ char **strv_env_set(char **x, const char *p) { k = r; if (env_append(r, &k, x) < 0) - goto fail; + return NULL; if (env_append(r, &k, m) < 0) - goto fail; + return NULL; *k = NULL; - return r; - -fail: - strv_free(r); - return NULL; + return TAKE_PTR(r); } char *strv_env_get_n(char **l, const char *name, size_t k, unsigned flags) { diff --git a/src/basic/strv.c b/src/basic/strv.c index cb91f239e8..47ab0ad07f 100644 --- a/src/basic/strv.c +++ b/src/basic/strv.c @@ -121,7 +121,7 @@ size_t strv_length(char * const *l) { char **strv_new_ap(const char *x, va_list ap) { const char *s; - char **a; + _cleanup_strv_free_ char **a = NULL; size_t n = 0, i = 0; va_list aq; @@ -152,7 +152,7 @@ char **strv_new_ap(const char *x, va_list ap) { if (x != STRV_IGNORE) { a[i] = strdup(x); if (!a[i]) - goto fail; + return NULL; i++; } @@ -163,7 +163,7 @@ char **strv_new_ap(const char *x, va_list ap) { a[i] = strdup(s); if (!a[i]) - goto fail; + return NULL; i++; } @@ -171,11 +171,7 @@ char **strv_new_ap(const char *x, va_list ap) { a[i] = NULL; - return a; - -fail: - strv_free(a); - return NULL; + return TAKE_PTR(a); } char **strv_new(const char *x, ...) { diff --git a/src/core/load-fragment.c b/src/core/load-fragment.c index d2a4c18bb8..a78039bb4f 100644 --- a/src/core/load-fragment.c +++ b/src/core/load-fragment.c @@ -1637,7 +1637,7 @@ int config_parse_timer(const char *unit, usec_t usec = 0; TimerValue *v; TimerBase b; - CalendarSpec *c = NULL; + _cleanup_(calendar_spec_freep) CalendarSpec *c = NULL; Unit *u = userdata; _cleanup_free_ char *k = NULL; int r; @@ -1678,14 +1678,12 @@ int config_parse_timer(const char *unit, } v = new0(TimerValue, 1); - if (!v) { - calendar_spec_free(c); + if (!v) return log_oom(); - } v->base = b; v->value = usec; - v->calendar_spec = c; + v->calendar_spec = TAKE_PTR(c); LIST_PREPEND(value, t->values, v); diff --git a/src/journal/sd-journal.c b/src/journal/sd-journal.c index f96d898b2d..2e81799add 100644 --- a/src/journal/sd-journal.c +++ b/src/journal/sd-journal.c @@ -1814,7 +1814,7 @@ static int allocate_inotify(sd_journal *j) { } static sd_journal *journal_new(int flags, const char *path) { - sd_journal *j; + _cleanup_(sd_journal_closep) sd_journal *j = NULL; j = new0(sd_journal, 1); if (!j) @@ -1831,7 +1831,7 @@ static sd_journal *journal_new(int flags, const char *path) { t = strdup(path); if (!t) - goto fail; + return NULL; if (flags & SD_JOURNAL_OS_ROOT) j->prefix = t; @@ -1841,19 +1841,15 @@ static sd_journal *journal_new(int flags, const char *path) { j->files = ordered_hashmap_new(&path_hash_ops); if (!j->files) - goto fail; + return NULL; j->files_cache = ordered_hashmap_iterated_cache_new(j->files); j->directories_by_path = hashmap_new(&path_hash_ops); j->mmap = mmap_cache_new(); if (!j->files_cache || !j->directories_by_path || !j->mmap) - goto fail; - - return j; + return NULL; -fail: - sd_journal_close(j); - return NULL; + return TAKE_PTR(j); } #define OPEN_ALLOWED_FLAGS \ @@ -1862,7 +1858,7 @@ fail: SD_JOURNAL_SYSTEM | SD_JOURNAL_CURRENT_USER) _public_ int sd_journal_open(sd_journal **ret, int flags) { - sd_journal *j; + _cleanup_(sd_journal_closep) sd_journal *j = NULL; int r; assert_return(ret, -EINVAL); @@ -1874,15 +1870,10 @@ _public_ int sd_journal_open(sd_journal **ret, int flags) { r = add_search_paths(j); if (r < 0) - goto fail; + return r; - *ret = j; + *ret = TAKE_PTR(j); return 0; - -fail: - sd_journal_close(j); - - return r; } #define OPEN_CONTAINER_ALLOWED_FLAGS \ @@ -1890,7 +1881,7 @@ fail: _public_ int sd_journal_open_container(sd_journal **ret, const char *machine, int flags) { _cleanup_free_ char *root = NULL, *class = NULL; - sd_journal *j; + _cleanup_(sd_journal_closep) sd_journal *j = NULL; char *p; int r; @@ -1920,14 +1911,10 @@ _public_ int sd_journal_open_container(sd_journal **ret, const char *machine, in r = add_search_paths(j); if (r < 0) - goto fail; + return r; - *ret = j; + *ret = TAKE_PTR(j); return 0; - -fail: - sd_journal_close(j); - return r; } #define OPEN_DIRECTORY_ALLOWED_FLAGS \ @@ -1935,7 +1922,7 @@ fail: SD_JOURNAL_SYSTEM | SD_JOURNAL_CURRENT_USER ) _public_ int sd_journal_open_directory(sd_journal **ret, const char *path, int flags) { - sd_journal *j; + _cleanup_(sd_journal_closep) sd_journal *j = NULL; int r; assert_return(ret, -EINVAL); @@ -1951,18 +1938,14 @@ _public_ int sd_journal_open_directory(sd_journal **ret, const char *path, int f else r = add_root_directory(j, path, false); if (r < 0) - goto fail; + return r; - *ret = j; + *ret = TAKE_PTR(j); return 0; - -fail: - sd_journal_close(j); - return r; } _public_ int sd_journal_open_files(sd_journal **ret, const char **paths, int flags) { - sd_journal *j; + _cleanup_(sd_journal_closep) sd_journal *j = NULL; const char **path; int r; @@ -1976,17 +1959,13 @@ _public_ int sd_journal_open_files(sd_journal **ret, const char **paths, int fla STRV_FOREACH(path, paths) { r = add_any_file(j, -1, *path); if (r < 0) - goto fail; + return r; } j->no_new_files = true; - *ret = j; + *ret = TAKE_PTR(j); return 0; - -fail: - sd_journal_close(j); - return r; } #define OPEN_DIRECTORY_FD_ALLOWED_FLAGS \ @@ -1994,7 +1973,7 @@ fail: SD_JOURNAL_SYSTEM | SD_JOURNAL_CURRENT_USER ) _public_ int sd_journal_open_directory_fd(sd_journal **ret, int fd, int flags) { - sd_journal *j; + _cleanup_(sd_journal_closep) sd_journal *j = NULL; struct stat st; int r; @@ -2019,20 +1998,16 @@ _public_ int sd_journal_open_directory_fd(sd_journal **ret, int fd, int flags) { else r = add_root_directory(j, NULL, false); if (r < 0) - goto fail; + return r; - *ret = j; + *ret = TAKE_PTR(j); return 0; - -fail: - sd_journal_close(j); - return r; } _public_ int sd_journal_open_files_fd(sd_journal **ret, int fds[], unsigned n_fds, int flags) { Iterator iterator; JournalFile *f; - sd_journal *j; + _cleanup_(sd_journal_closep) sd_journal *j = NULL; unsigned i; int r; @@ -2069,7 +2044,7 @@ _public_ int sd_journal_open_files_fd(sd_journal **ret, int fds[], unsigned n_fd j->no_new_files = true; j->no_inotify = true; - *ret = j; + *ret = TAKE_PTR(j); return 0; fail: @@ -2078,7 +2053,6 @@ fail: ORDERED_HASHMAP_FOREACH(f, j->files, iterator) f->close_fd = false; - sd_journal_close(j); return r; } diff --git a/src/libsystemd/sd-bus/bus-message.c b/src/libsystemd/sd-bus/bus-message.c index 61df50e43a..63e7c01ef1 100644 --- a/src/libsystemd/sd-bus/bus-message.c +++ b/src/libsystemd/sd-bus/bus-message.c @@ -96,7 +96,7 @@ static void message_reset_containers(sd_bus_message *m) { m->root_container.index = 0; } -static void message_free(sd_bus_message *m) { +static sd_bus_message* message_free(sd_bus_message *m) { assert(m); if (m->free_header) @@ -121,9 +121,11 @@ static void message_free(sd_bus_message *m) { free(m->root_container.peeked_signature); bus_creds_done(&m->creds); - free(m); + return mfree(m); } +DEFINE_TRIVIAL_CLEANUP_FUNC(sd_bus_message*, message_free); + static void *message_extend_fields(sd_bus_message *m, size_t align, size_t sz, bool add_offset) { void *op, *np; size_t old_size, new_size, start; @@ -514,7 +516,7 @@ int bus_message_from_malloc( const char *label, sd_bus_message **ret) { - sd_bus_message *m; + _cleanup_(message_freep) sd_bus_message *m = NULL; size_t sz; int r; @@ -545,18 +547,14 @@ int bus_message_from_malloc( r = bus_message_parse_fields(m); if (r < 0) - goto fail; + return r; /* We take possession of the memory and fds now */ m->free_header = true; m->free_fds = true; - *ret = m; + *ret = TAKE_PTR(m); return 0; - -fail: - message_free(m); - return r; } _public_ int sd_bus_message_new( @@ -598,7 +596,7 @@ _public_ int sd_bus_message_new_signal( const char *interface, const char *member) { - sd_bus_message *t; + _cleanup_(sd_bus_message_unrefp) sd_bus_message *t = NULL; int r; assert_return(bus, -ENOTCONN); @@ -618,20 +616,16 @@ _public_ int sd_bus_message_new_signal( r = message_append_field_string(t, BUS_MESSAGE_HEADER_PATH, SD_BUS_TYPE_OBJECT_PATH, path, &t->path); if (r < 0) - goto fail; + return r; r = message_append_field_string(t, BUS_MESSAGE_HEADER_INTERFACE, SD_BUS_TYPE_STRING, interface, &t->interface); if (r < 0) - goto fail; + return r; r = message_append_field_string(t, BUS_MESSAGE_HEADER_MEMBER, SD_BUS_TYPE_STRING, member, &t->member); if (r < 0) - goto fail; + return r; - *m = t; + *m = TAKE_PTR(t); return 0; - -fail: - sd_bus_message_unref(t); - return r; } _public_ int sd_bus_message_new_method_call( @@ -642,7 +636,7 @@ _public_ int sd_bus_message_new_method_call( const char *interface, const char *member) { - sd_bus_message *t; + _cleanup_(message_freep) sd_bus_message *t = NULL; int r; assert_return(bus, -ENOTCONN); @@ -661,29 +655,25 @@ _public_ int sd_bus_message_new_method_call( r = message_append_field_string(t, BUS_MESSAGE_HEADER_PATH, SD_BUS_TYPE_OBJECT_PATH, path, &t->path); if (r < 0) - goto fail; + return r; r = message_append_field_string(t, BUS_MESSAGE_HEADER_MEMBER, SD_BUS_TYPE_STRING, member, &t->member); if (r < 0) - goto fail; + return r; if (interface) { r = message_append_field_string(t, BUS_MESSAGE_HEADER_INTERFACE, SD_BUS_TYPE_STRING, interface, &t->interface); if (r < 0) - goto fail; + return r; } if (destination) { r = message_append_field_string(t, BUS_MESSAGE_HEADER_DESTINATION, SD_BUS_TYPE_STRING, destination, &t->destination); if (r < 0) - goto fail; + return r; } - *m = t; + *m = TAKE_PTR(t); return 0; - -fail: - message_free(t); - return r; } static int message_new_reply( @@ -691,7 +681,7 @@ static int message_new_reply( uint8_t type, sd_bus_message **m) { - sd_bus_message *t; + _cleanup_(message_freep) sd_bus_message *t = NULL; uint64_t cookie; int r; @@ -715,23 +705,19 @@ static int message_new_reply( t->reply_cookie = cookie; r = message_append_reply_cookie(t, t->reply_cookie); if (r < 0) - goto fail; + return r; if (call->sender) { r = message_append_field_string(t, BUS_MESSAGE_HEADER_DESTINATION, SD_BUS_TYPE_STRING, call->sender, &t->destination); if (r < 0) - goto fail; + return r; } t->dont_send = !!(call->header->flags & BUS_MESSAGE_NO_REPLY_EXPECTED); t->enforced_reply_signature = call->enforced_reply_signature; - *m = t; + *m = TAKE_PTR(t); return 0; - -fail: - message_free(t); - return r; } _public_ int sd_bus_message_new_method_return( @@ -746,7 +732,7 @@ _public_ int sd_bus_message_new_method_error( sd_bus_message **m, const sd_bus_error *e) { - sd_bus_message *t; + _cleanup_(message_freep) sd_bus_message *t = NULL; int r; assert_return(sd_bus_error_is_set(e), -EINVAL); @@ -758,22 +744,18 @@ _public_ int sd_bus_message_new_method_error( r = message_append_field_string(t, BUS_MESSAGE_HEADER_ERROR_NAME, SD_BUS_TYPE_STRING, e->name, &t->error.name); if (r < 0) - goto fail; + return r; if (e->message) { r = message_append_basic(t, SD_BUS_TYPE_STRING, e->message, (const void**) &t->error.message); if (r < 0) - goto fail; + return r; } t->error._need_free = -1; - *m = t; + *m = TAKE_PTR(t); return 0; - -fail: - message_free(t); - return r; } _public_ int sd_bus_message_new_method_errorf( @@ -853,7 +835,7 @@ int bus_message_new_synthetic_error( const sd_bus_error *e, sd_bus_message **m) { - sd_bus_message *t; + _cleanup_(message_freep) sd_bus_message *t = NULL; int r; assert(bus); @@ -871,34 +853,30 @@ int bus_message_new_synthetic_error( r = message_append_reply_cookie(t, t->reply_cookie); if (r < 0) - goto fail; + return r; if (bus && bus->unique_name) { r = message_append_field_string(t, BUS_MESSAGE_HEADER_DESTINATION, SD_BUS_TYPE_STRING, bus->unique_name, &t->destination); if (r < 0) - goto fail; + return r; } r = message_append_field_string(t, BUS_MESSAGE_HEADER_ERROR_NAME, SD_BUS_TYPE_STRING, e->name, &t->error.name); if (r < 0) - goto fail; + return r; if (e->message) { r = message_append_basic(t, SD_BUS_TYPE_STRING, e->message, (const void**) &t->error.message); if (r < 0) - goto fail; + return r; } t->error._need_free = -1; bus_message_set_sender_driver(bus, t); - *m = t; + *m = TAKE_PTR(t); return 0; - -fail: - message_free(t); - return r; } _public_ sd_bus_message* sd_bus_message_ref(sd_bus_message *m) { |