diff options
author | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2013-10-12 12:15:49 -0400 |
---|---|---|
committer | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2013-10-13 17:56:54 -0400 |
commit | b47d419c25ecc735615a1088060c1ec8bef1e41f (patch) | |
tree | 13940040841fbec3a928f82426390654e4b50fcc | |
parent | 7ff7394d9e4e9189c30fd018235e6b1728c6f2d0 (diff) |
Modernization
Fixes minor leak in error path in device.c.
-rw-r--r-- | src/core/device.c | 34 | ||||
-rw-r--r-- | src/gpt-auto-generator/gpt-auto-generator.c | 37 | ||||
-rw-r--r-- | src/locale/localed.c | 40 | ||||
-rw-r--r-- | src/readahead/readahead-collect.c | 10 | ||||
-rw-r--r-- | src/shared/cgroup-show.c | 15 | ||||
-rw-r--r-- | src/shared/efivars.c | 10 | ||||
-rw-r--r-- | src/shared/install.c | 4 | ||||
-rw-r--r-- | src/shared/socket-util.c | 17 | ||||
-rw-r--r-- | src/shared/util.c | 4 |
9 files changed, 70 insertions, 101 deletions
diff --git a/src/core/device.c b/src/core/device.c index 9fca82ab16..25af2cb2d5 100644 --- a/src/core/device.c +++ b/src/core/device.c @@ -189,10 +189,12 @@ static int device_update_unit(Manager *m, struct udev_device *dev, const char *p assert(m); - if (!(sysfs = udev_device_get_syspath(dev))) + sysfs = udev_device_get_syspath(dev); + if (!sysfs) return -ENOMEM; - if ((r = device_find_escape_name(m, path, &u)) < 0) + r = device_find_escape_name(m, path, &u); + if (r < 0) return r; if (u && DEVICE(u)->sysfs && !path_equal(DEVICE(u)->sysfs, sysfs)) @@ -234,17 +236,21 @@ static int device_update_unit(Manager *m, struct udev_device *dev, const char *p first = hashmap_get(m->devices_by_sysfs, sysfs); LIST_PREPEND(Device, same_sysfs, first, DEVICE(u)); - if ((r = hashmap_replace(m->devices_by_sysfs, DEVICE(u)->sysfs, first)) < 0) + r = hashmap_replace(m->devices_by_sysfs, DEVICE(u)->sysfs, first); + if (r < 0) goto fail; } if ((model = udev_device_get_property_value(dev, "ID_MODEL_FROM_DATABASE")) || (model = udev_device_get_property_value(dev, "ID_MODEL"))) { - if ((r = unit_set_description(u, model)) < 0) + r = unit_set_description(u, model); + if (r < 0) goto fail; - } else - if ((r = unit_set_description(u, path)) < 0) + } else { + r = unit_set_description(u, path); + if (r < 0) goto fail; + } if (main) { /* The additional systemd udev properties we only @@ -257,7 +263,7 @@ static int device_update_unit(Manager *m, struct udev_device *dev, const char *p size_t l; FOREACH_WORD_QUOTED(w, l, alias, state) { - char *e; + _cleanup_free_ char *e; e = strndup(w, l); if (!e) { @@ -265,13 +271,10 @@ static int device_update_unit(Manager *m, struct udev_device *dev, const char *p goto fail; } - if (!is_path(e)) { - log_warning("SYSTEMD_ALIAS for %s is not a path, ignoring: %s", sysfs, e); - free(e); - } else { + if (is_path(e)) device_update_unit(m, dev, e, false); - free(e); - } + else + log_warning("SYSTEMD_ALIAS for %s is not a path, ignoring: %s", sysfs, e); } } @@ -281,22 +284,21 @@ static int device_update_unit(Manager *m, struct udev_device *dev, const char *p size_t l; FOREACH_WORD_QUOTED(w, l, wants, state) { - char *e, *n; + _cleanup_free_ char *e, *n = NULL; e = strndup(w, l); if (!e) { r = -ENOMEM; goto fail; } + n = unit_name_mangle(e); if (!n) { r = -ENOMEM; goto fail; } - free(e); r = unit_add_dependency_by_name(u, UNIT_WANTS, n, NULL, true); - free(n); if (r < 0) goto fail; } diff --git a/src/gpt-auto-generator/gpt-auto-generator.c b/src/gpt-auto-generator/gpt-auto-generator.c index d2b4213af2..55fd6d6a7d 100644 --- a/src/gpt-auto-generator/gpt-auto-generator.c +++ b/src/gpt-auto-generator/gpt-auto-generator.c @@ -48,6 +48,9 @@ * */ +#define GPT_SWAP SD_ID128_MAKE(06,57,fd,6d,a4,ab,43,c4,84,e5,09,33,c8,4b,4f,4f) +#define GPT_HOME SD_ID128_MAKE(93,3a,c7,e1,2e,b4,4f,13,b8,44,0e,14,e2,ae,f9,15) + static const char *arg_dest = "/tmp"; static inline void blkid_free_probep(blkid_probe *b) { @@ -113,13 +116,13 @@ static int verify_gpt_partition(const char *node, sd_id128_t *type, unsigned *nr if (fstype) { - char *fst; - errno = 0; r = blkid_probe_lookup_value(b, "TYPE", &v, NULL); if (r != 0) *fstype = NULL; else { + char *fst; + fst = strdup(v); if (!fst) return -ENOMEM; @@ -242,10 +245,8 @@ static int enumerate_partitions(struct udev *udev, dev_t dev) { int r; e = udev_enumerate_new(udev); - if (!e) { - r = log_oom(); - goto finish; - } + if (!e) + return log_oom(); d = udev_device_new_from_devnum(udev, 'b', dev); if (!d) { @@ -314,10 +315,9 @@ static int enumerate_partitions(struct udev *udev, dev_t dev) { if (r == 0) goto skip; - if (sd_id128_equal(type_id, SD_ID128_MAKE(06,57,fd,6d,a4,ab,43,c4,84,e5,09,33,c8,4b,4f,4f))) + if (sd_id128_equal(type_id, GPT_SWAP)) add_swap(node, fstype); - else if (sd_id128_equal(type_id, SD_ID128_MAKE(93,3a,c7,e1,2e,b4,4f,13,b8,44,0e,14,e2,ae,f9,15))) { - + else if (sd_id128_equal(type_id, GPT_HOME)) { if (!home || nr < home_nr) { free(home); home = strdup(node); @@ -353,7 +353,7 @@ finish: } static int get_btrfs_block_device(const char *path, dev_t *dev) { - struct btrfs_ioctl_fs_info_args fsi; + struct btrfs_ioctl_fs_info_args fsi = {}; _cleanup_close_ int fd = -1; uint64_t id; @@ -364,7 +364,6 @@ static int get_btrfs_block_device(const char *path, dev_t *dev) { if (fd < 0) return -errno; - zero(fsi); if (ioctl(fd, BTRFS_IOC_FS_INFO, &fsi) < 0) return -errno; @@ -373,12 +372,11 @@ static int get_btrfs_block_device(const char *path, dev_t *dev) { return 0; for (id = 1; id <= fsi.max_id; id++) { - struct btrfs_ioctl_dev_info_args di; + struct btrfs_ioctl_dev_info_args di = { + .devid = id, + }; struct stat st; - zero(di); - di.devid = id; - if (ioctl(fd, BTRFS_IOC_DEV_INFO, &di) < 0) { if (errno == ENODEV) continue; @@ -433,10 +431,8 @@ static int devno_to_devnode(struct udev *udev, dev_t devno, char **ret) { int r; d = udev_device_new_from_devnum(udev, 'b', devno); - if (!d) { - r = log_oom(); - goto finish; - } + if (!d) + return log_oom(); t = udev_device_get_devnode(d); if (!t) { @@ -454,8 +450,7 @@ static int devno_to_devnode(struct udev *udev, dev_t devno, char **ret) { r = 0; finish: - if (d) - udev_device_unref(d); + udev_device_unref(d); return r; } diff --git a/src/locale/localed.c b/src/locale/localed.c index e160c046a4..3cac634a4c 100644 --- a/src/locale/localed.c +++ b/src/locale/localed.c @@ -805,7 +805,7 @@ static int convert_x11_to_vconsole(DBusConnection *connection) { free_data_x11(); } else { - FILE *f; + _cleanup_fclose_ FILE *f; unsigned n = 0; unsigned best_matching = 0; char *new_keymap = NULL; @@ -815,16 +815,13 @@ static int convert_x11_to_vconsole(DBusConnection *connection) { return -errno; for (;;) { - char **a; + _cleanup_strv_free_ char **a = NULL; unsigned matching = 0; int r; r = read_next_mapping(f, &n, &a); - if (r < 0) { - fclose(f); + if (r < 0) return r; - } - if (r == 0) break; @@ -879,19 +876,11 @@ static int convert_x11_to_vconsole(DBusConnection *connection) { free(new_keymap); new_keymap = strdup(a[0]); - - if (!new_keymap) { - strv_free(a); - fclose(f); + if (!new_keymap) return -ENOMEM; - } } - - strv_free(a); } - fclose(f); - if (!streq_ptr(state.vc_keymap, new_keymap)) { free(state.vc_keymap); state.vc_keymap = new_keymap; @@ -906,7 +895,8 @@ static int convert_x11_to_vconsole(DBusConnection *connection) { if (modified) { dbus_bool_t b; - DBusMessage *changed; + + _cleanup_dbus_message_unref_ DBusMessage *changed = NULL; int r; r = write_data_vconsole(); @@ -918,13 +908,10 @@ static int convert_x11_to_vconsole(DBusConnection *connection) { "org.freedesktop.locale1", "VConsoleKeymap\0" "VConsoleKeymapToggle\0"); - if (!changed) return -ENOMEM; b = dbus_connection_send(connection, changed, NULL); - dbus_message_unref(changed); - if (!b) return -ENOMEM; @@ -935,31 +922,26 @@ static int convert_x11_to_vconsole(DBusConnection *connection) { } static int append_locale(DBusMessageIter *i, const char *property, void *userdata) { - int r, c = 0, p; - char **l; + int c, p; + _cleanup_strv_free_ char **l = NULL; l = new0(char*, _PROP_MAX+1); if (!l) return -ENOMEM; - for (p = 0; p < _PROP_MAX; p++) { + for (p = 0, c = 0; p < _PROP_MAX; p++) { char *t; if (isempty(data[p])) continue; - if (asprintf(&t, "%s=%s", names[p], data[p]) < 0) { - strv_free(l); + if (asprintf(&t, "%s=%s", names[p], data[p]) < 0) return -ENOMEM; - } l[c++] = t; } - r = bus_property_append_strv(i, property, (void*) l); - strv_free(l); - - return r; + return bus_property_append_strv(i, property, (void*) l); } static const BusProperty bus_locale_properties[] = { diff --git a/src/readahead/readahead-collect.c b/src/readahead/readahead-collect.c index 6b748664c3..58b77bdcf7 100644 --- a/src/readahead/readahead-collect.c +++ b/src/readahead/readahead-collect.c @@ -415,7 +415,8 @@ static int collect(const char *root) { } } - if ((n = read(fanotify_fd, &data, sizeof(data))) < 0) { + n = read(fanotify_fd, &data, sizeof(data)); + if (n < 0) { if (errno == EINTR || errno == EAGAIN) continue; @@ -436,7 +437,7 @@ static int collect(const char *root) { } for (m = &data.metadata; FAN_EVENT_OK(m, n); m = FAN_EVENT_NEXT(m, n)) { - char fn[PATH_MAX]; + char fn[sizeof("/proc/self/fd/") + DECIMAL_STR_MAX(int)]; int k; if (m->fd < 0) @@ -450,9 +451,8 @@ static int collect(const char *root) { goto next_iteration; snprintf(fn, sizeof(fn), "/proc/self/fd/%i", m->fd); - char_array_0(fn); - - if ((k = readlink_malloc(fn, &p)) >= 0) { + k = readlink_malloc(fn, &p); + if (k >= 0) { if (startswith(p, "/tmp") || endswith(p, " (deleted)") || hashmap_get(files, p)) diff --git a/src/shared/cgroup-show.c b/src/shared/cgroup-show.c index cc44ab4ea9..979fa8fddf 100644 --- a/src/shared/cgroup-show.c +++ b/src/shared/cgroup-show.c @@ -67,7 +67,7 @@ static void show_pid_array(int pids[], unsigned n_pids, const char *prefix, unsi /* And sort */ qsort(pids, n_pids, sizeof(pid_t), compare); - if(flags & OUTPUT_FULL_WIDTH) + if (flags & OUTPUT_FULL_WIDTH) n_columns = 0; else { if (n_columns > pid_width+2) @@ -76,7 +76,7 @@ static void show_pid_array(int pids[], unsigned n_pids, const char *prefix, unsi n_columns = 20; } for (i = 0; i < n_pids; i++) { - char *t = NULL; + _cleanup_free_ char *t = NULL; get_process_cmdline(pids[i], n_columns, true, &t); @@ -87,8 +87,6 @@ static void show_pid_array(int pids[], unsigned n_pids, const char *prefix, unsi pid_width, (unsigned long) pids[i], strna(t)); - - free(t); } } @@ -98,7 +96,7 @@ static int show_cgroup_one_by_path(const char *path, const char *prefix, unsigne _cleanup_fclose_ FILE *f = NULL; size_t n = 0, n_allocated = 0; _cleanup_free_ pid_t *pids = NULL; - char *p = NULL; + _cleanup_free_ char *p = NULL; pid_t pid; int r; @@ -106,13 +104,8 @@ static int show_cgroup_one_by_path(const char *path, const char *prefix, unsigne if (r < 0) return r; - fn = strappend(p, "/cgroup.procs"); - free(p); - if (!fn) - return -ENOMEM; - + fn = strappenda(p, "/cgroup.procs"); f = fopen(fn, "re"); - free(fn); if (!f) return -errno; diff --git a/src/shared/efivars.c b/src/shared/efivars.c index f3eb6a6e5d..000dae9de0 100644 --- a/src/shared/efivars.c +++ b/src/shared/efivars.c @@ -37,7 +37,7 @@ bool is_efi_boot(void) { static int read_flag(const char *varname) { int r; - void *v; + _cleanup_free_ void *v = NULL; size_t s; uint8_t b; @@ -45,15 +45,11 @@ static int read_flag(const char *varname) { if (r < 0) return r; - if (s != 1) { - r = -EINVAL; - goto finish; - } + if (s != 1) + return -EINVAL; b = *(uint8_t *)v; r = b > 0; -finish: - free(v); return r; } diff --git a/src/shared/install.c b/src/shared/install.c index 9722ed4e1c..5a780fe915 100644 --- a/src/shared/install.c +++ b/src/shared/install.c @@ -1874,6 +1874,7 @@ static void unitfilelist_free(UnitFileList **f) { free((*f)->path); free(*f); } +#define _cleanup_unitfilelist_free_ _cleanup_(unitfilelist_free) int unit_file_get_list( UnitFileScope scope, @@ -1925,8 +1926,7 @@ int unit_file_get_list( for (;;) { struct dirent *de; union dirent_storage buffer; - UnitFileList __attribute__((cleanup(unitfilelist_free))) - *f = NULL; + _cleanup_unitfilelist_free_ UnitFileList *f = NULL; r = readdir_r(d, &buffer.de, &de); if (r != 0) diff --git a/src/shared/socket-util.c b/src/shared/socket-util.c index 9224208244..954686f974 100644 --- a/src/shared/socket-util.c +++ b/src/shared/socket-util.c @@ -300,7 +300,8 @@ int socket_address_print(const SocketAddress *a, char **p) { case AF_INET: { char *ret; - if (!(ret = new(char, INET_ADDRSTRLEN+1+5+1))) + ret = new(char, INET_ADDRSTRLEN+1+5+1); + if (!ret) return -ENOMEM; if (!inet_ntop(AF_INET, &a->sockaddr.in4.sin_addr, ret, INET_ADDRSTRLEN)) { @@ -316,7 +317,8 @@ int socket_address_print(const SocketAddress *a, char **p) { case AF_INET6: { char *ret; - if (!(ret = new(char, 1+INET6_ADDRSTRLEN+2+5+1))) + ret = new(char, 1+INET6_ADDRSTRLEN+2+5+1); + if (!ret) return -ENOMEM; ret[0] = '['; @@ -334,8 +336,8 @@ int socket_address_print(const SocketAddress *a, char **p) { char *ret; if (a->size <= offsetof(struct sockaddr_un, sun_path)) { - - if (!(ret = strdup("<unnamed>"))) + ret = strdup("<unnamed>"); + if (!ret) return -ENOMEM; } else if (a->sockaddr.un.sun_path[0] == 0) { @@ -346,7 +348,8 @@ int socket_address_print(const SocketAddress *a, char **p) { * more than one NUL byte. That is * actually an invalid assumption */ - if (!(ret = new(char, sizeof(a->sockaddr.un.sun_path)+1))) + ret = new(char, sizeof(a->sockaddr.un.sun_path)+1); + if (!ret) return -ENOMEM; ret[0] = '@'; @@ -354,8 +357,8 @@ int socket_address_print(const SocketAddress *a, char **p) { ret[sizeof(a->sockaddr.un.sun_path)] = 0; } else { - - if (!(ret = strdup(a->sockaddr.un.sun_path))) + ret = strdup(a->sockaddr.un.sun_path); + if (!ret) return -ENOMEM; } diff --git a/src/shared/util.c b/src/shared/util.c index e18421e520..54dbace193 100644 --- a/src/shared/util.c +++ b/src/shared/util.c @@ -643,7 +643,7 @@ int get_process_cmdline(pid_t pid, size_t max_length, bool comm_fallback, char * /* Kernel threads have no argv[] */ if (r == NULL || r[0] == 0) { - char *t; + _cleanup_free_ char *t = NULL; int h; free(r); @@ -656,8 +656,6 @@ int get_process_cmdline(pid_t pid, size_t max_length, bool comm_fallback, char * return h; r = strjoin("[", t, "]", NULL); - free(t); - if (!r) return -ENOMEM; } |