From 97b11eedff9d2e17101ad453caf9e48b73246719 Mon Sep 17 00:00:00 2001 From: David Herrmann Date: Fri, 31 Jul 2015 19:56:38 +0200 Subject: tree-wide: introduce mfree() Pretty trivial helper which wraps free() but returns NULL, so we can simplify this: free(foobar); foobar = NULL; to this: foobar = mfree(foobar); --- src/basic/copy.c | 6 ++-- src/basic/util.h | 5 +++ src/bus-proxyd/bus-xml-policy.c | 6 ++-- src/core/load-fragment.c | 8 ++--- src/core/main.c | 6 ++-- src/core/snapshot.c | 3 +- src/cryptsetup/cryptsetup.c | 3 +- src/firstboot/firstboot.c | 6 ++-- src/import/pull-dkr.c | 6 ++-- src/journal/catalog.c | 3 +- src/journal/coredumpctl.c | 3 +- src/journal/journalctl.c | 3 +- src/libsystemd/sd-bus/bus-match.c | 3 +- src/libsystemd/sd-bus/busctl-introspect.c | 8 ++--- src/libsystemd/sd-netlink/test-local-addresses.c | 3 +- src/login/logind-dbus.c | 9 ++---- src/nspawn/nspawn.c | 6 ++-- src/nss-myhostname/nss-myhostname.c | 3 +- src/shared/conf-parser.c | 3 +- src/systemctl/systemctl.c | 3 +- src/sysusers/sysusers.c | 36 ++++++++-------------- src/test/test-util.c | 33 +++++++------------- .../tty-ask-password-agent.c | 3 +- src/udev/udev-builtin-path_id.c | 12 +++----- src/udev/udev-rules.c | 3 +- 25 files changed, 64 insertions(+), 119 deletions(-) diff --git a/src/basic/copy.c b/src/basic/copy.c index e2d356d676..33427c6a73 100644 --- a/src/basic/copy.c +++ b/src/basic/copy.c @@ -467,8 +467,7 @@ int copy_xattr(int fdf, int fdt) { sza *= 2; - free(bufa); - bufa = NULL; + bufa = mfree(bufa); } p = bufa; @@ -491,8 +490,7 @@ int copy_xattr(int fdf, int fdt) { if (m < 0) { if (errno == ERANGE) { szb *= 2; - free(bufb); - bufb = NULL; + bufb = mfree(bufb); continue; } diff --git a/src/basic/util.h b/src/basic/util.h index c2e5cc610b..06c82665be 100644 --- a/src/basic/util.h +++ b/src/basic/util.h @@ -84,6 +84,11 @@ bool streq_ptr(const char *a, const char *b) _pure_; #define malloc0(n) (calloc((n), 1)) +static inline void *mfree(void *memory) { + free(memory); + return NULL; +} + static inline const char* yes_no(bool b) { return b ? "yes" : "no"; } diff --git a/src/bus-proxyd/bus-xml-policy.c b/src/bus-proxyd/bus-xml-policy.c index dab5acbcb4..9a3b451c56 100644 --- a/src/bus-proxyd/bus-xml-policy.c +++ b/src/bus-proxyd/bus-xml-policy.c @@ -586,10 +586,8 @@ static int file_load(Policy *p, const char *path) { case POLICY_ITEM_SEND: case POLICY_ITEM_RECV: - if (streq(name, "*")) { - free(name); - name = NULL; - } + if (streq(name, "*")) + name = mfree(name); break; diff --git a/src/core/load-fragment.c b/src/core/load-fragment.c index a48cb4029a..ba73cc410e 100644 --- a/src/core/load-fragment.c +++ b/src/core/load-fragment.c @@ -3508,9 +3508,7 @@ static int load_from_path(Unit *u, const char *path) { r = open_follow(&filename, &f, symlink_names, &id); if (r < 0) { - free(filename); - filename = NULL; - + filename = mfree(filename); if (r != -ENOENT) return r; } @@ -3534,9 +3532,7 @@ static int load_from_path(Unit *u, const char *path) { r = open_follow(&filename, &f, symlink_names, &id); if (r < 0) { - free(filename); - filename = NULL; - + filename = mfree(filename); if (r != -ENOENT) return r; diff --git a/src/core/main.c b/src/core/main.c index 6ae8b51544..a66fb18418 100644 --- a/src/core/main.c +++ b/src/core/main.c @@ -1844,13 +1844,11 @@ finish: arg_default_rlimit[j] = NULL; } - free(arg_default_unit); - arg_default_unit = NULL; + arg_default_unit = mfree(arg_default_unit); free_join_controllers(); - strv_free(arg_default_environment); - arg_default_environment = NULL; + arg_default_environment = strv_free(arg_default_environment); set_free(arg_syscall_archs); arg_syscall_archs = NULL; diff --git a/src/core/snapshot.c b/src/core/snapshot.c index 1e634b9bc1..9518e21f36 100644 --- a/src/core/snapshot.c +++ b/src/core/snapshot.c @@ -217,8 +217,7 @@ int snapshot_create(Manager *m, const char *name, bool cleanup, sd_bus_error *e, break; } - free(n); - n = NULL; + n = mfree(n); } } diff --git a/src/cryptsetup/cryptsetup.c b/src/cryptsetup/cryptsetup.c index 5c6c7c0ed8..74fa90a233 100644 --- a/src/cryptsetup/cryptsetup.c +++ b/src/cryptsetup/cryptsetup.c @@ -333,8 +333,7 @@ static int get_password(const char *vol, const char *src, usec_t until, bool acc /* If the description string is simply the * volume name, then let's not show this * twice */ - free(description); - description = NULL; + description = mfree(description); } if (mount_point && description) diff --git a/src/firstboot/firstboot.c b/src/firstboot/firstboot.c index f06ab3da29..05f1ae2646 100644 --- a/src/firstboot/firstboot.c +++ b/src/firstboot/firstboot.c @@ -715,10 +715,8 @@ static int parse_argv(int argc, char *argv[]) { path_kill_slashes(arg_root); - if (path_equal(arg_root, "/")) { - free(arg_root); - arg_root = NULL; - } + if (path_equal(arg_root, "/")) + arg_root = mfree(arg_root); break; diff --git a/src/import/pull-dkr.c b/src/import/pull-dkr.c index 67ca1ce8e4..c918f43446 100644 --- a/src/import/pull-dkr.c +++ b/src/import/pull-dkr.c @@ -592,8 +592,7 @@ static int dkr_pull_pull_layer_v2(DkrPull *i) { i->current_ancestry++; - free(path); - path = NULL; + path = mfree(path); } log_info("Pulling layer %s...", layer); @@ -652,8 +651,7 @@ static int dkr_pull_pull_layer(DkrPull *i) { i->current_ancestry++; - free(path); - path = NULL; + path = mfree(path); } log_info("Pulling layer %s...", layer); diff --git a/src/journal/catalog.c b/src/journal/catalog.c index 33b0539315..a3e51e2f52 100644 --- a/src/journal/catalog.c +++ b/src/journal/catalog.c @@ -263,8 +263,7 @@ int catalog_import_file(Hashmap *h, struct strbuf *sb, const char *path) { if (r < 0) return r; - free(lang); - lang = NULL; + lang = mfree(lang); } if (with_language) { diff --git a/src/journal/coredumpctl.c b/src/journal/coredumpctl.c index fc49b2e174..098f62af50 100644 --- a/src/journal/coredumpctl.c +++ b/src/journal/coredumpctl.c @@ -587,8 +587,7 @@ static int save_core(sd_journal *j, int fd, char **path, bool *unlink_temp) { if (filename && access(filename, R_OK) < 0) { log_full(errno == ENOENT ? LOG_DEBUG : LOG_WARNING, "File %s is not readable: %m", filename); - free(filename); - filename = NULL; + filename = mfree(filename); } if (filename && !endswith(filename, ".xz") && !endswith(filename, ".lz4")) { diff --git a/src/journal/journalctl.c b/src/journal/journalctl.c index 073cc77711..355c4f1ee3 100644 --- a/src/journal/journalctl.c +++ b/src/journal/journalctl.c @@ -1259,8 +1259,7 @@ static int add_units(sd_journal *j) { } } - strv_free(patterns); - patterns = NULL; + patterns = strv_free(patterns); STRV_FOREACH(i, arg_user_units) { _cleanup_free_ char *u = NULL; diff --git a/src/libsystemd/sd-bus/bus-match.c b/src/libsystemd/sd-bus/bus-match.c index e4cbd793ed..18c36ce243 100644 --- a/src/libsystemd/sd-bus/bus-match.c +++ b/src/libsystemd/sd-bus/bus-match.c @@ -861,8 +861,7 @@ int bus_match_parse( if (r < 0) goto fail; - free(value); - value = NULL; + value = mfree(value); } else u = 0; diff --git a/src/libsystemd/sd-bus/busctl-introspect.c b/src/libsystemd/sd-bus/busctl-introspect.c index 15c10da7e9..03e83d08a1 100644 --- a/src/libsystemd/sd-bus/busctl-introspect.c +++ b/src/libsystemd/sd-bus/busctl-introspect.c @@ -462,9 +462,8 @@ static int parse_xml_node(Context *context, const char *prefix, unsigned n_depth } } - free(argument_type); - free(argument_direction); - argument_type = argument_direction = NULL; + argument_type = mfree(argument_type); + argument_direction = mfree(argument_direction); } state = STATE_METHOD; @@ -604,8 +603,7 @@ static int parse_xml_node(Context *context, const char *prefix, unsigned n_depth if (!strextend(&context->member_signature, argument_type, NULL)) return log_oom(); - free(argument_type); - argument_type = NULL; + argument_type = mfree(argument_type); } state = STATE_SIGNAL; diff --git a/src/libsystemd/sd-netlink/test-local-addresses.c b/src/libsystemd/sd-netlink/test-local-addresses.c index 38cbcfbccb..9867eec065 100644 --- a/src/libsystemd/sd-netlink/test-local-addresses.c +++ b/src/libsystemd/sd-netlink/test-local-addresses.c @@ -44,9 +44,8 @@ int main(int argc, char *argv[]) { printf("Local Addresses:\n"); print_local_addresses(a, (unsigned) n); - free(a); + a = mfree(a); - a = NULL; n = local_gateways(NULL, 0, AF_UNSPEC, &a); assert_se(n >= 0); diff --git a/src/login/logind-dbus.c b/src/login/logind-dbus.c index 397952e7e5..b0cd85e985 100644 --- a/src/login/logind-dbus.c +++ b/src/login/logind-dbus.c @@ -725,15 +725,13 @@ static int method_create_session(sd_bus_message *message, void *userdata, sd_bus log_warning("Existing logind session ID %s used by new audit session, ignoring", id); audit_id = 0; - free(id); - id = NULL; + id = mfree(id); } } if (!id) { do { - free(id); - id = NULL; + id = mfree(id); if (asprintf(&id, "c%lu", ++m->session_counter) < 0) return -ENOMEM; @@ -2355,8 +2353,7 @@ static int method_inhibit(sd_bus_message *message, void *userdata, sd_bus_error return r; do { - free(id); - id = NULL; + id = mfree(id); if (asprintf(&id, "%lu", ++m->inhibit_counter) < 0) return -ENOMEM; diff --git a/src/nspawn/nspawn.c b/src/nspawn/nspawn.c index d46f768cfa..e78b15a8b3 100644 --- a/src/nspawn/nspawn.c +++ b/src/nspawn/nspawn.c @@ -309,8 +309,7 @@ static void custom_mount_free_all(void) { strv_free(m->lower); } - free(arg_custom_mounts); - arg_custom_mounts = NULL; + arg_custom_mounts = mfree(arg_custom_mounts); arg_n_custom_mounts = 0; } @@ -561,8 +560,7 @@ static int parse_argv(int argc, char *argv[]) { case 'M': if (isempty(optarg)) { - free(arg_machine); - arg_machine = NULL; + arg_machine = mfree(arg_machine); } else { if (!machine_name_is_valid(optarg)) { log_error("Invalid machine name: %s", optarg); diff --git a/src/nss-myhostname/nss-myhostname.c b/src/nss-myhostname/nss-myhostname.c index 31db1aaf68..ab96cb231e 100644 --- a/src/nss-myhostname/nss-myhostname.c +++ b/src/nss-myhostname/nss-myhostname.c @@ -464,8 +464,7 @@ enum nss_status _nss_myhostname_gethostbyaddr2_r( } } - free(addresses); - addresses = NULL; + addresses = mfree(addresses); n_addresses = local_gateways(NULL, 0, AF_UNSPEC, &addresses); if (n_addresses > 0) { diff --git a/src/shared/conf-parser.c b/src/shared/conf-parser.c index 7370c786f9..d99aa1d6e9 100644 --- a/src/shared/conf-parser.c +++ b/src/shared/conf-parser.c @@ -333,8 +333,7 @@ int config_parse(const char *unit, return -ENOMEM; } - free(continuation); - continuation = NULL; + continuation = mfree(continuation); p = c; } else p = l; diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c index 6db4d6587a..4e850ea1cf 100644 --- a/src/systemctl/systemctl.c +++ b/src/systemctl/systemctl.c @@ -3327,8 +3327,7 @@ static void print_status_info( if (! dir || last) { printf(dir ? " " : " Drop-In: "); - free(dir); - dir = NULL; + dir = mfree(dir); if (path_get_parent(*dropin, &dir) < 0) { log_oom(); diff --git a/src/sysusers/sysusers.c b/src/sysusers/sysusers.c index b3fa29b84c..3a92d120d2 100644 --- a/src/sysusers/sysusers.c +++ b/src/sysusers/sysusers.c @@ -704,8 +704,7 @@ static int write_files(void) { goto finish; } - free(group_tmp); - group_tmp = NULL; + group_tmp = mfree(group_tmp); } if (gshadow) { if (rename(gshadow_tmp, gshadow_path) < 0) { @@ -713,8 +712,7 @@ static int write_files(void) { goto finish; } - free(gshadow_tmp); - gshadow_tmp = NULL; + gshadow_tmp = mfree(gshadow_tmp); } } @@ -724,8 +722,7 @@ static int write_files(void) { goto finish; } - free(passwd_tmp); - passwd_tmp = NULL; + passwd_tmp = mfree(passwd_tmp); } if (shadow) { if (rename(shadow_tmp, shadow_path) < 0) { @@ -733,8 +730,7 @@ static int write_files(void) { goto finish; } - free(shadow_tmp); - shadow_tmp = NULL; + shadow_tmp = mfree(shadow_tmp); } r = 0; @@ -1410,10 +1406,8 @@ static int parse_line(const char *fname, unsigned line, const char *buffer) { } /* Verify name */ - if (isempty(name) || streq(name, "-")) { - free(name); - name = NULL; - } + if (isempty(name) || streq(name, "-")) + name = mfree(name); if (name) { r = specifier_printf(name, specifier_table, NULL, &resolved_name); @@ -1429,10 +1423,8 @@ static int parse_line(const char *fname, unsigned line, const char *buffer) { } /* Verify id */ - if (isempty(id) || streq(id, "-")) { - free(id); - id = NULL; - } + if (isempty(id) || streq(id, "-")) + id = mfree(id); if (id) { r = specifier_printf(id, specifier_table, NULL, &resolved_id); @@ -1443,10 +1435,8 @@ static int parse_line(const char *fname, unsigned line, const char *buffer) { } /* Verify description */ - if (isempty(description) || streq(description, "-")) { - free(description); - description = NULL; - } + if (isempty(description) || streq(description, "-")) + description = mfree(description); if (description) { if (!valid_gecos(description)) { @@ -1456,10 +1446,8 @@ static int parse_line(const char *fname, unsigned line, const char *buffer) { } /* Verify home */ - if (isempty(home) || streq(home, "-")) { - free(home); - home = NULL; - } + if (isempty(home) || streq(home, "-")) + home = mfree(home); if (home) { if (!valid_home(home)) { diff --git a/src/test/test-util.c b/src/test/test-util.c index f43433baa1..fde3fa0988 100644 --- a/src/test/test-util.c +++ b/src/test/test-util.c @@ -714,45 +714,38 @@ static void test_cunescape(void) { assert_se(cunescape("abc\\\\\\\"\\b\\f\\a\\n\\r\\t\\v\\003\\177\\234\\313\\000\\x00", 0, &unescaped) < 0); assert_se(cunescape("abc\\\\\\\"\\b\\f\\a\\n\\r\\t\\v\\003\\177\\234\\313\\000\\x00", UNESCAPE_RELAX, &unescaped) >= 0); assert_se(streq_ptr(unescaped, "abc\\\"\b\f\a\n\r\t\v\003\177\234\313\\000\\x00")); - free(unescaped); - unescaped = NULL; + unescaped = mfree(unescaped); /* incomplete sequences */ assert_se(cunescape("\\x0", 0, &unescaped) < 0); assert_se(cunescape("\\x0", UNESCAPE_RELAX, &unescaped) >= 0); assert_se(streq_ptr(unescaped, "\\x0")); - free(unescaped); - unescaped = NULL; + unescaped = mfree(unescaped); assert_se(cunescape("\\x", 0, &unescaped) < 0); assert_se(cunescape("\\x", UNESCAPE_RELAX, &unescaped) >= 0); assert_se(streq_ptr(unescaped, "\\x")); - free(unescaped); - unescaped = NULL; + unescaped = mfree(unescaped); assert_se(cunescape("\\", 0, &unescaped) < 0); assert_se(cunescape("\\", UNESCAPE_RELAX, &unescaped) >= 0); assert_se(streq_ptr(unescaped, "\\")); - free(unescaped); - unescaped = NULL; + unescaped = mfree(unescaped); assert_se(cunescape("\\11", 0, &unescaped) < 0); assert_se(cunescape("\\11", UNESCAPE_RELAX, &unescaped) >= 0); assert_se(streq_ptr(unescaped, "\\11")); - free(unescaped); - unescaped = NULL; + unescaped = mfree(unescaped); assert_se(cunescape("\\1", 0, &unescaped) < 0); assert_se(cunescape("\\1", UNESCAPE_RELAX, &unescaped) >= 0); assert_se(streq_ptr(unescaped, "\\1")); - free(unescaped); - unescaped = NULL; + unescaped = mfree(unescaped); assert_se(cunescape("\\u0000", 0, &unescaped) < 0); assert_se(cunescape("\\u00DF\\U000000df\\u03a0\\U00000041", UNESCAPE_RELAX, &unescaped) >= 0); assert_se(streq_ptr(unescaped, "ßßΠA")); - free(unescaped); - unescaped = NULL; + unescaped = mfree(unescaped); assert_se(cunescape("\\073", 0, &unescaped) >= 0); assert_se(streq_ptr(unescaped, ";")); @@ -859,32 +852,28 @@ static void test_read_hostname_config(void) { write_string_file(path, "foo", WRITE_STRING_FILE_CREATE); assert_se(read_hostname_config(path, &hostname) == 0); assert_se(streq(hostname, "foo")); - free(hostname); - hostname = NULL; + hostname = mfree(hostname); /* with comment */ write_string_file(path, "# comment\nfoo", WRITE_STRING_FILE_CREATE); assert_se(read_hostname_config(path, &hostname) == 0); assert_se(hostname); assert_se(streq(hostname, "foo")); - free(hostname); - hostname = NULL; + hostname = mfree(hostname); /* with comment and extra whitespace */ write_string_file(path, "# comment\n\n foo ", WRITE_STRING_FILE_CREATE); assert_se(read_hostname_config(path, &hostname) == 0); assert_se(hostname); assert_se(streq(hostname, "foo")); - free(hostname); - hostname = NULL; + hostname = mfree(hostname); /* cleans up name */ write_string_file(path, "!foo/bar.com", WRITE_STRING_FILE_CREATE); assert_se(read_hostname_config(path, &hostname) == 0); assert_se(hostname); assert_se(streq(hostname, "foobar.com")); - free(hostname); - hostname = NULL; + hostname = mfree(hostname); /* no value set */ hostname = (char*) 0x1234; diff --git a/src/tty-ask-password-agent/tty-ask-password-agent.c b/src/tty-ask-password-agent/tty-ask-password-agent.c index 73b19d8e89..82cbf95f1e 100644 --- a/src/tty-ask-password-agent/tty-ask-password-agent.c +++ b/src/tty-ask-password-agent/tty-ask-password-agent.c @@ -162,8 +162,7 @@ static int ask_password_plymouth( /* Hmm, first try with cached * passwords failed, so let's retry * with a normal password request */ - free(packet); - packet = NULL; + packet = mfree(packet); if (asprintf(&packet, "*\002%c%s%n", (int) (strlen(message) + 1), message, &n) < 0) return -ENOMEM; diff --git a/src/udev/udev-builtin-path_id.c b/src/udev/udev-builtin-path_id.c index 4ca0a69d7d..aa5cda6fe7 100644 --- a/src/udev/udev-builtin-path_id.c +++ b/src/udev/udev-builtin-path_id.c @@ -674,20 +674,16 @@ static int builtin_path_id(struct udev_device *dev, int argc, char *argv[], bool * might produce conflicting IDs if the parent does not provide a * unique and predictable name. */ - if (!supported_parent) { - free(path); - path = NULL; - } + if (!supported_parent) + path = mfree(path); /* * Do not return block devices without a well-known transport. Some * devices do not expose their buses and do not provide a unique * and predictable name that way. */ - if (streq(udev_device_get_subsystem(dev), "block") && !supported_transport) { - free(path); - path = NULL; - } + if (streq(udev_device_get_subsystem(dev), "block") && !supported_transport) + path = mfree(path); out: if (path != NULL) { diff --git a/src/udev/udev-rules.c b/src/udev/udev-rules.c index bbb9f97226..368da9b1ae 100644 --- a/src/udev/udev-rules.c +++ b/src/udev/udev-rules.c @@ -2589,8 +2589,7 @@ int udev_rules_apply_static_dev_perms(struct udev_rules *rules) { uid = 0; gid = 0; mode = 0; - strv_free(tags); - tags = NULL; + tags = strv_free(tags); break; case TK_A_OWNER_ID: uid = cur->key.uid; -- cgit v1.2.3-54-g00ecf