]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
tree-wide: add path_simplify_alloc() and use it
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Thu, 21 Sep 2023 15:38:17 +0000 (17:38 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Fri, 22 Sep 2023 06:13:34 +0000 (08:13 +0200)
path_simplify_full()/path_simplify() are changed to allow a NULL path, for
which a NULL is returned. Generally, callers have already asserted before that
the argument is nonnull. This way path_simplify_full()/path_simplify() and
path_simplify_alloc() behave consistently.

In sd-device.c, logging in device_set_syspath() is intentionally dropped: other
branches don't log.

In mount-tool.c, logging in parse_argv() is changed to log the user-specified
value, not the simplified string. In an error message, we should show the
actual argument we got, not some transformed version.

13 files changed:
src/basic/cgroup-util.c
src/basic/fileio.c
src/basic/path-util.c
src/basic/path-util.h
src/basic/unit-name.c
src/basic/user-util.c
src/core/dbus-path.c
src/core/dbus-socket.c
src/libsystemd/sd-device/sd-device.c
src/mount/mount-tool.c
src/portable/portable.c
src/shared/install.c
src/udev/udev-node.c

index b2b0ddd1907b3272f4772a7db41e7c4e2a53114e..c088a84532d20447548295ec652c4c9c0b0f6514 100644 (file)
@@ -947,6 +947,7 @@ int cg_is_empty_recursive(const char *controller, const char *path) {
 
 int cg_split_spec(const char *spec, char **ret_controller, char **ret_path) {
         _cleanup_free_ char *controller = NULL, *path = NULL;
+        int r;
 
         assert(spec);
 
@@ -955,11 +956,9 @@ int cg_split_spec(const char *spec, char **ret_controller, char **ret_path) {
                         return -EINVAL;
 
                 if (ret_path) {
-                        path = strdup(spec);
-                        if (!path)
-                                return -ENOMEM;
-
-                        path_simplify(path);
+                        r = path_simplify_alloc(spec, &path);
+                        if (r < 0)
+                                return r;
                 }
 
         } else {
@@ -1006,22 +1005,14 @@ int cg_split_spec(const char *spec, char **ret_controller, char **ret_path) {
 
 int cg_mangle_path(const char *path, char **result) {
         _cleanup_free_ char *c = NULL, *p = NULL;
-        char *t;
         int r;
 
         assert(path);
         assert(result);
 
         /* First, check if it already is a filesystem path */
-        if (path_startswith(path, "/sys/fs/cgroup")) {
-
-                t = strdup(path);
-                if (!t)
-                        return -ENOMEM;
-
-                *result = path_simplify(t);
-                return 0;
-        }
+        if (path_startswith(path, "/sys/fs/cgroup"))
+                return path_simplify_alloc(path, result);
 
         /* Otherwise, treat it as cg spec */
         r = cg_split_spec(path, &c, &p);
index 48ffb4e5e6e5b47d3ceea1d019ce47069f9bcd6f..ee1621822c3cb5ca5b6a00a574126cc7edc762ff 100644 (file)
@@ -1110,6 +1110,7 @@ int search_and_fopen(
                 char **ret_path) {
 
         _cleanup_strv_free_ char **copy = NULL;
+        int r;
 
         assert(filename);
         assert(mode);
@@ -1123,13 +1124,9 @@ int search_and_fopen(
                         return -errno;
 
                 if (ret_path) {
-                        char *p;
-
-                        p = strdup(filename);
-                        if (!p)
-                                return -ENOMEM;
-
-                        *ret_path = path_simplify(p);
+                        r = path_simplify_alloc(filename, ret_path);
+                        if (r < 0)
+                                return r;
                 }
 
                 *ret = TAKE_PTR(f);
@@ -1152,6 +1149,7 @@ int search_and_fopen_nulstr(
                 char **ret_path) {
 
         _cleanup_strv_free_ char **s = NULL;
+        int r;
 
         if (path_is_absolute(filename)) {
                 _cleanup_fclose_ FILE *f = NULL;
@@ -1161,13 +1159,9 @@ int search_and_fopen_nulstr(
                         return -errno;
 
                 if (ret_path) {
-                        char *p;
-
-                        p = strdup(filename);
-                        if (!p)
-                                return -ENOMEM;
-
-                        *ret_path = path_simplify(p);
+                        r = path_simplify_alloc(filename, ret_path);
+                        if (r < 0)
+                                return r;
                 }
 
                 *ret = TAKE_PTR(f);
index b036191144666112e7651a23397453b58f79a635..6810bf66aa32f8b7459fc8ab0c8f96fd4bf3e638 100644 (file)
@@ -132,11 +132,9 @@ int path_make_relative(const char *from, const char *to, char **ret) {
                                         return -ENOMEM;
                         } else {
                                 /* 'to' is inside of 'from'. */
-                                result = strdup(t);
-                                if (!result)
-                                        return -ENOMEM;
-
-                                path_simplify(result);
+                                r = path_simplify_alloc(t, &result);
+                                if (r < 0)
+                                        return r;
 
                                 if (!path_is_valid(result))
                                         return -EINVAL;
@@ -346,7 +344,7 @@ char** path_strv_resolve_uniq(char **l, const char *root) {
 
 char* path_simplify_full(char *path, PathSimplifyFlags flags) {
         bool add_slash = false, keep_trailing_slash, absolute, beginning = true;
-        char *f = ASSERT_PTR(path);
+        char *f = path;
         int r;
 
         /* Removes redundant inner and trailing slashes. Also removes unnecessary dots.
index 3f60f4d1330f42a0523000675694d00e36a1751c..6d943e967f27d044a26cfb5d39c69640e33dcc4d 100644 (file)
@@ -85,6 +85,22 @@ static inline char* path_simplify(char *path) {
         return path_simplify_full(path, 0);
 }
 
+static inline int path_simplify_alloc(const char *path, char **ret) {
+        assert(ret);
+
+        if (!path) {
+                *ret = NULL;
+                return 0;
+        }
+
+        char *t = strdup(path);
+        if (!t)
+                return -ENOMEM;
+
+        *ret = path_simplify(t);
+        return 0;
+}
+
 static inline bool path_equal_ptr(const char *a, const char *b) {
         return !!a == !!b && (!a || path_equal(a, b));
 }
index a6ef0c4321cca2b4b0bfb3cea0d0ec0561ef6231..06877227e424fb740e1f3191783f3e393102fbce 100644 (file)
@@ -389,15 +389,14 @@ int unit_name_unescape(const char *f, char **ret) {
 int unit_name_path_escape(const char *f, char **ret) {
         _cleanup_free_ char *p = NULL;
         char *s;
+        int r;
 
         assert(f);
         assert(ret);
 
-        p = strdup(f);
-        if (!p)
-                return -ENOMEM;
-
-        path_simplify(p);
+        r = path_simplify_alloc(f, &p);
+        if (r < 0)
+                return r;
 
         if (empty_or_root(p))
                 s = strdup("-");
index 5c39847733bb3dec1531a47c998990db68c6bc70..9ae8577238290588b2b66da27a0c538854406082 100644 (file)
@@ -589,7 +589,6 @@ int getgroups_alloc(gid_t** gids) {
 int get_home_dir(char **ret) {
         struct passwd *p;
         const char *e;
-        char *h;
         uid_t u;
 
         assert(ret);
@@ -622,18 +621,12 @@ int get_home_dir(char **ret) {
                 return -EINVAL;
 
  found:
-        h = strdup(e);
-        if (!h)
-                return -ENOMEM;
-
-        *ret = path_simplify(h);
-        return 0;
+        return path_simplify_alloc(e, ret);
 }
 
 int get_shell(char **ret) {
         struct passwd *p;
         const char *e;
-        char *s;
         uid_t u;
 
         assert(ret);
@@ -665,12 +658,7 @@ int get_shell(char **ret) {
                 return -EINVAL;
 
  found:
-        s = strdup(e);
-        if (!s)
-                return -ENOMEM;
-
-        *ret = path_simplify(s);
-        return 0;
+        return path_simplify_alloc(e, ret);
 }
 
 int reset_uid_gid(void) {
index ced40e70d09e7222327ddacbd3b7d453ffe3f777..8cb6a265faba261d583842e86b98c15bb2aab02e 100644 (file)
@@ -98,11 +98,9 @@ static int bus_path_set_transient_property(
                         if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
                                 _cleanup_free_ char *k = NULL;
 
-                                k = strdup(path);
-                                if (!k)
-                                        return -ENOMEM;
-
-                                path_simplify(k);
+                                r = path_simplify_alloc(path, &k);
+                                if (r < 0)
+                                        return r;
 
                                 PathSpec *s = new(PathSpec, 1);
                                 if (!s)
index 09a3a9502b33f98ddafc13b9622c1483f11bd8cb..f88ecbeae41c461d5412481e1ca1e81933462a3e 100644 (file)
@@ -383,11 +383,9 @@ static int bus_socket_set_transient_property(
                                 if (!path_is_absolute(a) || !path_is_valid(a))
                                         return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid socket path: %s", a);
 
-                                p->path = strdup(a);
-                                if (!p->path)
-                                        return log_oom();
-
-                                path_simplify(p->path);
+                                r = path_simplify_alloc(a, &p->path);
+                                if (r < 0)
+                                        return r;
 
                         } else if (streq(t, "Netlink")) {
                                 r = socket_address_parse_netlink(&p->address, a);
index 97e2e727c0b013d9a2d5eaf078800a043e6c19a8..82c2c6df1c378b577baa0aa838693f7a4b95e57e 100644 (file)
@@ -232,11 +232,9 @@ int device_set_syspath(sd_device *device, const char *_syspath, bool verify) {
                                                "sd-device: Syspath '%s' is not a subdirectory of /sys",
                                                _syspath);
 
-                syspath = strdup(_syspath);
-                if (!syspath)
-                        return log_oom_debug();
-
-                path_simplify(syspath);
+                r = path_simplify_alloc(_syspath, &syspath);
+                if (r < 0)
+                        return r;
         }
 
         assert_se(devpath = startswith(syspath, "/sys"));
index a0dcdf53b66c503764fa565322dd2b33bd5c9f97..90feb2f1979a2319a6c0206070fc075eba4d2209 100644 (file)
@@ -83,30 +83,26 @@ STATIC_DESTRUCTOR_REGISTER(arg_property, strv_freep);
 STATIC_DESTRUCTOR_REGISTER(arg_automount_property, strv_freep);
 
 static int parse_where(const char *input, char **ret_where) {
-        _cleanup_free_ char *where = NULL;
         int r;
 
         assert(input);
         assert(ret_where);
 
         if (arg_transport == BUS_TRANSPORT_LOCAL) {
-                r = chase(input, NULL, CHASE_NONEXISTENT, &where, NULL);
+                r = chase(input, NULL, CHASE_NONEXISTENT, ret_where, NULL);
                 if (r < 0)
                         return log_error_errno(r, "Failed to make path %s absolute: %m", input);
         } else {
-                where = strdup(input);
-                if (!where)
-                        return log_oom();
-
-                path_simplify(where);
-
-                if (!path_is_absolute(where))
+                if (!path_is_absolute(input))
                         return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
                                                "Path must be absolute when operating remotely: %s",
-                                               where);
+                                               input);
+
+                r = path_simplify_alloc(input, ret_where);
+                if (r < 0)
+                        return log_error_errno(r, "Failed to simplify path %s: %m", input);
         }
 
-        *ret_where = TAKE_PTR(where);
         return 0;
 }
 
@@ -441,17 +437,16 @@ static int parse_argv(int argc, char *argv[]) {
                                 r = chase(u, NULL, 0, &arg_mount_what, NULL);
                                 if (r < 0)
                                         return log_error_errno(r, "Failed to make path %s absolute: %m", u);
-                        } else {
-                                arg_mount_what = strdup(argv[optind]);
-                                if (!arg_mount_what)
-                                        return log_oom();
 
-                                path_simplify(arg_mount_what);
-
-                                if (!path_is_absolute(arg_mount_what))
+                        } else {
+                                if (!path_is_absolute(argv[optind]))
                                         return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
                                                                "Path must be absolute when operating remotely: %s",
-                                                               arg_mount_what);
+                                                               argv[optind]);
+
+                                r = path_simplify_alloc(argv[optind], &arg_mount_what);
+                                if (r < 0)
+                                        return log_error_errno(r, "Failed to simplify path: %m");
                         }
                 }
 
@@ -1054,11 +1049,9 @@ static int action_umount(
                 for (int i = optind; i < argc; i++) {
                         _cleanup_free_ char *p = NULL;
 
-                        p = strdup(argv[i]);
-                        if (!p)
-                                return log_oom();
-
-                        path_simplify(p);
+                        r = path_simplify_alloc(argv[i], &p);
+                        if (r < 0)
+                                return r;
 
                         r = stop_mounts(bus, p);
                         if (r < 0)
index 545a3ed5d22f9bebfdd616ae4246948e3d8376da..52f38dc83ecf97da83bd30d3dbea753167be8752 100644 (file)
@@ -838,6 +838,7 @@ static int portable_changes_add(
 
         _cleanup_free_ char *p = NULL, *s = NULL;
         PortableChange *c;
+        int r;
 
         assert(path);
         assert(!changes == !n_changes);
@@ -855,19 +856,13 @@ static int portable_changes_add(
                 return -ENOMEM;
         *changes = c;
 
-        p = strdup(path);
-        if (!p)
-                return -ENOMEM;
-
-        path_simplify(p);
-
-        if (source) {
-                s = strdup(source);
-                if (!s)
-                        return -ENOMEM;
+        r = path_simplify_alloc(path, &p);
+        if (r < 0)
+                return r;
 
-                path_simplify(s);
-        }
+        r = path_simplify_alloc(source, &s);
+        if (r < 0)
+                return r;
 
         c[(*n_changes)++] = (PortableChange) {
                 .type_or_errno = type_or_errno,
index c171a2330e8c3d37c06251c903540ddc1d2d0680..3186444d2f19b1b2d9d719bd4a8164bb6272a2c5 100644 (file)
@@ -285,6 +285,7 @@ InstallChangeType install_changes_add(
 
         _cleanup_free_ char *p = NULL, *s = NULL;
         InstallChange *c;
+        int r;
 
         assert(!changes == !n_changes);
         assert(INSTALL_CHANGE_TYPE_VALID(type));
@@ -303,21 +304,13 @@ InstallChangeType install_changes_add(
                 return -ENOMEM;
         *changes = c;
 
-        if (path) {
-                p = strdup(path);
-                if (!p)
-                        return -ENOMEM;
-
-                path_simplify(p);
-        }
-
-        if (source) {
-                s = strdup(source);
-                if (!s)
-                        return -ENOMEM;
+        r = path_simplify_alloc(path, &p);
+        if (r < 0)
+                return r;
 
-                path_simplify(s);
-        }
+        r = path_simplify_alloc(source, &s);
+        if (r < 0)
+                return r;
 
         c[(*n_changes)++] = (InstallChange) {
                 .type = type,
index 75845413cf0281b85ad23ae64fa83743a02783b8..6ba38087dcfaee2416707e2d7768d1d25cefa3a9 100644 (file)
@@ -331,15 +331,14 @@ static int stack_directory_get_name(const char *slink, char **ret) {
         _cleanup_free_ char *s = NULL, *dirname = NULL;
         char name_enc[NAME_MAX+1];
         const char *name;
+        int r;
 
         assert(slink);
         assert(ret);
 
-        s = strdup(slink);
-        if (!s)
-                return -ENOMEM;
-
-        path_simplify(s);
+        r = path_simplify_alloc(slink, &s);
+        if (r < 0)
+                return r;
 
         if (!path_is_normalized(s))
                 return -EINVAL;