From: Yu Watanabe Date: Wed, 30 Mar 2022 19:11:30 +0000 (+0900) Subject: sd-device: use path_extract_directory() at one more place X-Git-Tag: v251-rc2~243^2~3 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=07c90f02d22eb346a9f315faaf440ba54cdffb1f;p=thirdparty%2Fsystemd.git sd-device: use path_extract_directory() at one more place --- diff --git a/src/libsystemd/sd-device/sd-device.c b/src/libsystemd/sd-device/sd-device.c index 545a0f5a5d7..d2fd322e2a1 100644 --- a/src/libsystemd/sd-device/sd-device.c +++ b/src/libsystemd/sd-device/sd-device.c @@ -735,7 +735,7 @@ _public_ int sd_device_get_syspath(sd_device *device, const char **ret) { static int device_new_from_child(sd_device **ret, sd_device *child) { _cleanup_free_ char *path = NULL; - const char *subdir, *syspath; + const char *syspath; int r; assert(ret); @@ -745,25 +745,21 @@ static int device_new_from_child(sd_device **ret, sd_device *child) { if (r < 0) return r; - path = strdup(syspath); - if (!path) - return -ENOMEM; - subdir = path + STRLEN("/sys"); - for (;;) { - char *pos; + _cleanup_free_ char *p = NULL; - pos = strrchr(subdir, '/'); - if (!pos || pos < subdir + 2) - return -ENODEV; + r = path_extract_directory(path ?: syspath, &p); + if (r < 0) + return r; - *pos = '\0'; + if (path_equal(p, "/sys")) + return -ENODEV; - r = sd_device_new_from_syspath(ret, path); - if (r < 0) - continue; + r = sd_device_new_from_syspath(ret, p); + if (r != -ENODEV) + return r; - return 0; + free_and_replace(path, p); } }