From 5fecf46d76b31eedb3132cd569cfb82295e132bc Mon Sep 17 00:00:00 2001 From: Daniel Lockyer Date: Fri, 24 Nov 2017 10:31:49 +0000 Subject: [PATCH 1/4] Replace free and return NULL with return mfree --- src/basic/khash.c | 4 +--- src/core/transaction.c | 3 +-- src/libsystemd/sd-bus/bus-objects.c | 3 +-- src/libudev/libudev-hwdb.c | 3 +-- src/libudev/libudev-monitor.c | 3 +-- src/libudev/libudev-queue.c | 3 +-- src/libudev/libudev.c | 3 +-- src/shared/condition.c | 3 +-- src/shared/dissect-image.c | 3 +-- src/shared/loop-util.c | 4 +--- 10 files changed, 10 insertions(+), 22 deletions(-) diff --git a/src/basic/khash.c b/src/basic/khash.c index 8122903b09c..694210512cf 100644 --- a/src/basic/khash.c +++ b/src/basic/khash.c @@ -127,9 +127,7 @@ khash* khash_unref(khash *h) { safe_close(h->fd); free(h->algorithm); - free(h); - - return NULL; + return mfree(h); } int khash_dup(khash *h, khash **ret) { diff --git a/src/core/transaction.c b/src/core/transaction.c index 52ae664faef..32ad6600268 100644 --- a/src/core/transaction.c +++ b/src/core/transaction.c @@ -346,8 +346,7 @@ static char* merge_unit_ids(const char* unit_log_field, char **pairs) { STRV_FOREACH_PAIR(unit_id, job_type, pairs) { next = strlen(unit_log_field) + strlen(*unit_id); if (!GREEDY_REALLOC(ans, alloc, size + next + 1)) { - free(ans); - return NULL; + return mfree(ans); } sprintf(ans + size, "%s%s", unit_log_field, *unit_id); diff --git a/src/libsystemd/sd-bus/bus-objects.c b/src/libsystemd/sd-bus/bus-objects.c index 9fb455a94e2..4c7dbaa2c9d 100644 --- a/src/libsystemd/sd-bus/bus-objects.c +++ b/src/libsystemd/sd-bus/bus-objects.c @@ -1476,8 +1476,7 @@ static struct node *bus_node_allocate(sd_bus *bus, const char *path) { r = hashmap_put(bus->nodes, n->path, n); if (r < 0) { free(n->path); - free(n); - return NULL; + return mfree(n); } if (parent) diff --git a/src/libudev/libudev-hwdb.c b/src/libudev/libudev-hwdb.c index 5edb0bea7e0..d2665278c1f 100644 --- a/src/libudev/libudev-hwdb.c +++ b/src/libudev/libudev-hwdb.c @@ -113,8 +113,7 @@ _public_ struct udev_hwdb *udev_hwdb_unref(struct udev_hwdb *hwdb) { return NULL; sd_hwdb_unref(hwdb->hwdb); udev_list_cleanup(&hwdb->properties_list); - free(hwdb); - return NULL; + return mfree(hwdb); } /** diff --git a/src/libudev/libudev-monitor.c b/src/libudev/libudev-monitor.c index 3c9f1f94967..ca14373e191 100644 --- a/src/libudev/libudev-monitor.c +++ b/src/libudev/libudev-monitor.c @@ -493,8 +493,7 @@ _public_ struct udev_monitor *udev_monitor_unref(struct udev_monitor *udev_monit close(udev_monitor->sock); udev_list_cleanup(&udev_monitor->filter_subsystem_list); udev_list_cleanup(&udev_monitor->filter_tag_list); - free(udev_monitor); - return NULL; + return mfree(udev_monitor); } /** diff --git a/src/libudev/libudev-queue.c b/src/libudev/libudev-queue.c index b1e41c086d2..b941afb7739 100644 --- a/src/libudev/libudev-queue.c +++ b/src/libudev/libudev-queue.c @@ -115,8 +115,7 @@ _public_ struct udev_queue *udev_queue_unref(struct udev_queue *udev_queue) safe_close(udev_queue->fd); - free(udev_queue); - return NULL; + return mfree(udev_queue); } /** diff --git a/src/libudev/libudev.c b/src/libudev/libudev.c index 475b94f9136..64904c5ffac 100644 --- a/src/libudev/libudev.c +++ b/src/libudev/libudev.c @@ -136,8 +136,7 @@ _public_ struct udev *udev_unref(struct udev *udev) { udev->refcount--; if (udev->refcount > 0) return udev; - free(udev); - return NULL; + return mfree(udev); } /** diff --git a/src/shared/condition.c b/src/shared/condition.c index 11555b474ce..f1e914cb2df 100644 --- a/src/shared/condition.c +++ b/src/shared/condition.c @@ -78,8 +78,7 @@ Condition* condition_new(ConditionType type, const char *parameter, bool trigger r = free_and_strdup(&c->parameter, parameter); if (r < 0) { - free(c); - return NULL; + return mfree(c); } return c; diff --git a/src/shared/dissect-image.c b/src/shared/dissect-image.c index 08f1e98640f..75e3431f283 100644 --- a/src/shared/dissect-image.c +++ b/src/shared/dissect-image.c @@ -648,8 +648,7 @@ DissectedImage* dissected_image_unref(DissectedImage *m) { strv_free(m->machine_info); strv_free(m->os_release); - free(m); - return NULL; + return mfree(m); } static int is_loop_device(const char *path) { diff --git a/src/shared/loop-util.c b/src/shared/loop-util.c index 31c5789a2ee..097de690e5b 100644 --- a/src/shared/loop-util.c +++ b/src/shared/loop-util.c @@ -152,9 +152,7 @@ LoopDevice* loop_device_unref(LoopDevice *d) { } free(d->node); - free(d); - - return NULL; + return mfree(d); } void loop_device_relinquish(LoopDevice *d) { From f9ecfd3bbed396d744373dd6c8876b7211eac594 Mon Sep 17 00:00:00 2001 From: Daniel Lockyer Date: Fri, 24 Nov 2017 10:33:41 +0000 Subject: [PATCH 2/4] Replace free and reassignment with free_and_replace --- src/cryptsetup/cryptsetup-generator.c | 4 +--- src/import/import-raw.c | 4 +--- src/import/pull-raw.c | 4 +--- src/libsystemd-network/dhcp-option.c | 4 +--- src/libsystemd-network/sd-dhcp-lease.c | 8 ++----- src/libsystemd/sd-bus/sd-bus.c | 4 +--- src/libsystemd/sd-device/device-private.c | 8 ++----- src/libsystemd/sd-device/sd-device.c | 28 ++++++----------------- src/locale/keymap-util.c | 4 +--- src/nspawn/nspawn-settings.c | 4 +--- src/shared/machine-image.c | 9 ++------ src/timedate/timedated.c | 4 +--- 12 files changed, 21 insertions(+), 64 deletions(-) diff --git a/src/cryptsetup/cryptsetup-generator.c b/src/cryptsetup/cryptsetup-generator.c index 7918c89564f..3f7222a8aac 100644 --- a/src/cryptsetup/cryptsetup-generator.c +++ b/src/cryptsetup/cryptsetup-generator.c @@ -337,9 +337,7 @@ static int parse_proc_cmdline_item(const char *key, const char *value, void *dat d->create = arg_whitelist = true; - free(d->name); - d->name = uuid_value; - uuid_value = NULL; + free_and_replace(d->name, uuid_value); } else log_warning("Failed to parse luks name switch %s. Ignoring.", value); } diff --git a/src/import/import-raw.c b/src/import/import-raw.c index 6f4313420a8..f117c94da23 100644 --- a/src/import/import-raw.c +++ b/src/import/import-raw.c @@ -207,9 +207,7 @@ static int raw_import_maybe_convert_qcow2(RawImport *i) { } (void) unlink(i->temp_path); - free(i->temp_path); - i->temp_path = t; - t = NULL; + free_and_replace(i->temp_path, t); safe_close(i->output_fd); i->output_fd = converted_fd; diff --git a/src/import/pull-raw.c b/src/import/pull-raw.c index 8cab72495c5..880cb84ba84 100644 --- a/src/import/pull-raw.c +++ b/src/import/pull-raw.c @@ -269,9 +269,7 @@ static int raw_pull_maybe_convert_qcow2(RawPull *i) { } (void) unlink(i->temp_path); - free(i->temp_path); - i->temp_path = t; - t = NULL; + free_and_replace(i->temp_path, t); safe_close(i->raw_job->disk_fd); i->raw_job->disk_fd = converted_fd; diff --git a/src/libsystemd-network/dhcp-option.c b/src/libsystemd-network/dhcp-option.c index 19fc525866b..0489579e7f0 100644 --- a/src/libsystemd-network/dhcp-option.c +++ b/src/libsystemd-network/dhcp-option.c @@ -191,9 +191,7 @@ static int parse_options(const uint8_t options[], size_t buflen, uint8_t *overlo if (!ascii_is_valid(string)) return -EINVAL; - free(*error_message); - *error_message = string; - string = NULL; + free_and_replace(*error_message, string); } break; diff --git a/src/libsystemd-network/sd-dhcp-lease.c b/src/libsystemd-network/sd-dhcp-lease.c index ec5ecaeabc1..7063bd986e8 100644 --- a/src/libsystemd-network/sd-dhcp-lease.c +++ b/src/libsystemd-network/sd-dhcp-lease.c @@ -393,9 +393,7 @@ static int lease_parse_domain(const uint8_t *option, size_t len, char **ret) { if (dns_name_is_root(normalized)) return -EINVAL; - free(*ret); - *ret = normalized; - normalized = NULL; + free_and_replace(*ret, normalized); return 0; } @@ -684,9 +682,7 @@ int dhcp_lease_parse_options(uint8_t code, uint8_t len, const void *option, void return 0; } - free(lease->timezone); - lease->timezone = tz; - tz = NULL; + free_and_replace(lease->timezone, tz); break; } diff --git a/src/libsystemd/sd-bus/sd-bus.c b/src/libsystemd/sd-bus/sd-bus.c index 26ca6696557..5fb15a7aaff 100644 --- a/src/libsystemd/sd-bus/sd-bus.c +++ b/src/libsystemd/sd-bus/sd-bus.c @@ -786,9 +786,7 @@ static int parse_container_unix_address(sd_bus *b, const char **p, char **guid) if (!machine_name_is_valid(machine)) return -EINVAL; - free(b->machine); - b->machine = machine; - machine = NULL; + free_and_replace(b->machine, machine); } else { b->machine = mfree(b->machine); } diff --git a/src/libsystemd/sd-device/device-private.c b/src/libsystemd/sd-device/device-private.c index 587908b1902..424965ecd58 100644 --- a/src/libsystemd/sd-device/device-private.c +++ b/src/libsystemd/sd-device/device-private.c @@ -678,13 +678,9 @@ static int device_update_properties_bufs(sd_device *device) { i++; } - free(device->properties_nulstr); - device->properties_nulstr = buf_nulstr; - buf_nulstr = NULL; + free_and_replace(device->properties_nulstr, buf_nulstr); device->properties_nulstr_len = nulstr_len; - free(device->properties_strv); - device->properties_strv = buf_strv; - buf_strv = NULL; + free_and_replace(device->properties_strv, buf_strv); device->properties_buf_outdated = false; diff --git a/src/libsystemd/sd-device/sd-device.c b/src/libsystemd/sd-device/sd-device.c index b278eefa205..f8b9f9e3b08 100644 --- a/src/libsystemd/sd-device/sd-device.c +++ b/src/libsystemd/sd-device/sd-device.c @@ -215,9 +215,7 @@ int device_set_syspath(sd_device *device, const char *_syspath, bool verify) { if (r < 0) return r; - free(device->syspath); - device->syspath = syspath; - syspath = NULL; + free_and_replace(device->syspath, syspath); device->devpath = devpath; @@ -347,9 +345,7 @@ int device_set_devtype(sd_device *device, const char *_devtype) { if (r < 0) return r; - free(device->devtype); - device->devtype = devtype; - devtype = NULL; + free_and_replace(device->devtype, devtype); return 0; } @@ -394,9 +390,7 @@ int device_set_devname(sd_device *device, const char *_devname) { if (r < 0) return r; - free(device->devname); - device->devname = devname; - devname = NULL; + free_and_replace(device->devname, devname); return 0; } @@ -761,9 +755,7 @@ int device_set_subsystem(sd_device *device, const char *_subsystem) { if (r < 0) return r; - free(device->subsystem); - device->subsystem = subsystem; - subsystem = NULL; + free_and_replace(device->subsystem, subsystem); device->subsystem_set = true; @@ -786,9 +778,7 @@ static int device_set_drivers_subsystem(sd_device *device, const char *_subsyste if (r < 0) return r; - free(device->driver_subsystem); - device->driver_subsystem = subsystem; - subsystem = NULL; + free_and_replace(device->driver_subsystem, subsystem); return 0; } @@ -936,9 +926,7 @@ int device_set_driver(sd_device *device, const char *_driver) { if (r < 0) return r; - free(device->driver); - device->driver = driver; - driver = NULL; + free_and_replace(device->driver, driver); device->driver_set = true; @@ -1045,9 +1033,7 @@ static int device_set_sysname(sd_device *device) { if (len == 0) sysnum = NULL; - free(device->sysname); - device->sysname = sysname; - sysname = NULL; + free_and_replace(device->sysname, sysname); device->sysnum = sysnum; diff --git a/src/locale/keymap-util.c b/src/locale/keymap-util.c index 1140ce299cc..2457f8705c6 100644 --- a/src/locale/keymap-util.c +++ b/src/locale/keymap-util.c @@ -201,9 +201,7 @@ static int x11_read_data(Context *c) { p = &c->x11_options; if (p) { - free(*p); - *p = a[2]; - a[2] = NULL; + free_and_replace(*p, a[2]); } } diff --git a/src/nspawn/nspawn-settings.c b/src/nspawn/nspawn-settings.c index 2c9a8f6b444..a1518a6e813 100644 --- a/src/nspawn/nspawn-settings.c +++ b/src/nspawn/nspawn-settings.c @@ -403,9 +403,7 @@ int config_parse_network_zone( return 0; } - free(settings->network_zone); - settings->network_zone = j; - j = NULL; + free_and_replace(settings->network_zone, j); return 0; } diff --git a/src/shared/machine-image.c b/src/shared/machine-image.c index 7676a93e8b7..5b6260cbb8a 100644 --- a/src/shared/machine-image.c +++ b/src/shared/machine-image.c @@ -649,13 +649,8 @@ int image_rename(Image *i, const char *new_name) { if (file_attr & FS_IMMUTABLE_FL) (void) chattr_path(new_path, FS_IMMUTABLE_FL, FS_IMMUTABLE_FL); - free(i->path); - i->path = new_path; - new_path = NULL; - - free(i->name); - i->name = nn; - nn = NULL; + free_and_replace(i->path, new_path); + free_and_replace(i->name, nn); STRV_FOREACH(j, settings) { r = rename_auxiliary_file(*j, new_name, ".nspawn"); diff --git a/src/timedate/timedated.c b/src/timedate/timedated.c index bd35451ecb0..a55a929a495 100644 --- a/src/timedate/timedated.c +++ b/src/timedate/timedated.c @@ -75,9 +75,7 @@ static int context_read_data(Context *c) { else if (r < 0) log_warning_errno(r, "Failed to get target of /etc/localtime: %m"); - free(c->zone); - c->zone = t; - t = NULL; + free_and_replace(c->zone, t); c->local_rtc = clock_is_localtime(NULL) > 0; From a7419dbc59da5c8cc9e90b3d96bc947cad91ae16 Mon Sep 17 00:00:00 2001 From: Daniel Lockyer Date: Fri, 24 Nov 2017 10:36:04 +0000 Subject: [PATCH 3/4] Replace use of snprintf with xsprintf --- src/basic/log.c | 4 ++-- src/core/job.c | 2 +- src/core/unit.c | 2 +- src/udev/collect/collect.c | 2 +- src/udev/udev-builtin-net_id.c | 4 ++-- 5 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/basic/log.c b/src/basic/log.c index 18a1ea0bdbf..1637a062d71 100644 --- a/src/basic/log.c +++ b/src/basic/log.c @@ -359,7 +359,7 @@ static int write_to_console( highlight = LOG_PRI(level) <= LOG_ERR && show_color; if (show_location) { - snprintf(location, sizeof(location), "(%s:%i) ", file, line); + xsprintf(location, "(%s:%i) ", file, line); iovec[n++] = IOVEC_MAKE_STRING(location); } @@ -798,7 +798,7 @@ static void log_assert( return; DISABLE_WARNING_FORMAT_NONLITERAL; - snprintf(buffer, sizeof buffer, format, text, file, line, func); + xsprintf(buffer, format, text, file, line, func); REENABLE_WARNING; log_abort_msg = buffer; diff --git a/src/core/job.c b/src/core/job.c index 46defae1bc8..2ea7834dfd2 100644 --- a/src/core/job.c +++ b/src/core/job.c @@ -777,7 +777,7 @@ static void job_log_status_message(Unit *u, JobType t, JobResult result) { /* The description might be longer than the buffer, but that's OK, we'll just truncate it here */ DISABLE_WARNING_FORMAT_NONLITERAL; - snprintf(buf, sizeof(buf), format, unit_description(u)); + xsprintf(buf, format, unit_description(u)); REENABLE_WARNING; switch (t) { diff --git a/src/core/unit.c b/src/core/unit.c index ae5b7b11f07..c419caef10e 100644 --- a/src/core/unit.c +++ b/src/core/unit.c @@ -1680,7 +1680,7 @@ static void unit_status_log_starting_stopping_reloading(Unit *u, JobType t) { format = unit_get_status_message_format(u, t); DISABLE_WARNING_FORMAT_NONLITERAL; - snprintf(buf, sizeof buf, format, unit_description(u)); + xsprintf(buf, format, unit_description(u)); REENABLE_WARNING; mid = t == JOB_START ? "MESSAGE_ID=" SD_MESSAGE_UNIT_STARTING_STR : diff --git a/src/udev/collect/collect.c b/src/udev/collect/collect.c index 7b5fdf50b7f..17e66166343 100644 --- a/src/udev/collect/collect.c +++ b/src/udev/collect/collect.c @@ -94,7 +94,7 @@ static int prepare(char *dir, char *filename) if (r < 0 && errno != EEXIST) return -errno; - snprintf(buf, sizeof buf, "%s/%s", dir, filename); + xsprintf(buf, "%s/%s", dir, filename); fd = open(buf, O_RDWR|O_CREAT|O_CLOEXEC, S_IRUSR|S_IWUSR); if (fd < 0) diff --git a/src/udev/udev-builtin-net_id.c b/src/udev/udev-builtin-net_id.c index 803c33fea3b..f3be27fdd03 100644 --- a/src/udev/udev-builtin-net_id.c +++ b/src/udev/udev-builtin-net_id.c @@ -275,7 +275,7 @@ static int dev_pci_slot(struct udev_device *dev, struct netnames *names) { goto out; } - snprintf(slots, sizeof slots, "%s/slots", udev_device_get_syspath(pci)); + xsprintf(slots, "%s/slots", udev_device_get_syspath(pci)); dir = opendir(slots); if (!dir) { err = -errno; @@ -294,7 +294,7 @@ static int dev_pci_slot(struct udev_device *dev, struct netnames *names) { if (i < 1) continue; - snprintf(str, sizeof str, "%s/%s/address", slots, dent->d_name); + xsprintf(str, "%s/%s/address", slots, dent->d_name); if (read_one_line_file(str, &address) >= 0) { /* match slot address with device by stripping the function */ if (strneq(address, udev_device_get_sysname(names->pcidev), strlen(address))) From 4600a396d5322dc7583f1af8d59576da7cf938fd Mon Sep 17 00:00:00 2001 From: Daniel Lockyer Date: Fri, 24 Nov 2017 10:37:08 +0000 Subject: [PATCH 4/4] Remove NULL as last parameter to strjoin --- src/coredump/coredump.c | 2 +- src/import/pull-common.c | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/coredump/coredump.c b/src/coredump/coredump.c index 5673aa0f785..d3533790a10 100644 --- a/src/coredump/coredump.c +++ b/src/coredump/coredump.c @@ -1215,7 +1215,7 @@ static int gather_pid_metadata( if (get_process_environ(pid, &t) >= 0) set_iovec_field_free(iovec, n_iovec, "COREDUMP_ENVIRON=", t); - t = strjoin("COREDUMP_TIMESTAMP=", context[CONTEXT_TIMESTAMP], "000000", NULL); + t = strjoin("COREDUMP_TIMESTAMP=", context[CONTEXT_TIMESTAMP], "000000"); if (t) iovec[(*n_iovec)++] = IOVEC_MAKE_STRING(t); diff --git a/src/import/pull-common.c b/src/import/pull-common.c index cb2e3d9be29..c2a3a6aa8b1 100644 --- a/src/import/pull-common.c +++ b/src/import/pull-common.c @@ -192,7 +192,7 @@ int pull_make_path(const char *url, const char *etag, const char *image_root, co } path = strjoin(image_root, "/", strempty(prefix), escaped_url, escaped_etag ? "." : "", - strempty(escaped_etag), strempty(suffix), NULL); + strempty(escaped_etag), strempty(suffix)); if (!path) return -ENOMEM; @@ -210,7 +210,7 @@ int pull_make_path(const char *url, const char *etag, const char *image_root, co return r; path = strjoin(image_root, "/", strempty(prefix), hash, escaped_etag ? "." : "", - strempty(escaped_etag), strempty(suffix), NULL); + strempty(escaped_etag), strempty(suffix)); if (!path) return -ENOMEM; }