summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2021-05-05 16:57:39 +0200
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2021-05-06 14:51:08 +0200
commit37eeb9c03f86227e94d8e1fa046ca0c0d2b6d237 (patch)
treed25015bb9478026b1cfb9ebbb492c7cbcac3e89d
parentd7ea7c486a0101dae06a9aca290bfafa46bc1fe2 (diff)
downloadsystemd-37eeb9c03f86227e94d8e1fa046ca0c0d2b6d237.tar.gz
sd-device: use memdupa_suffix0() where appropriate
No functional change intended. (cherry picked from commit 3e2d0c6ab2abc0ab85440580931b2462bb73cfda)
-rw-r--r--src/libsystemd/sd-device/sd-device.c27
1 files changed, 11 insertions, 16 deletions
diff --git a/src/libsystemd/sd-device/sd-device.c b/src/libsystemd/sd-device/sd-device.c
index da5fd2b734..c041660faf 100644
--- a/src/libsystemd/sd-device/sd-device.c
+++ b/src/libsystemd/sd-device/sd-device.c
@@ -271,20 +271,17 @@ _public_ int sd_device_new_from_subsystem_sysname(sd_device **ret, const char *s
return sd_device_new_from_syspath(ret, syspath);
} else if (streq(subsystem, "drivers")) {
- char subsys[PATH_MAX];
- char *driver;
+ const char *subsys, *sep;
- strscpy(subsys, sizeof(subsys), sysname);
- driver = strchr(subsys, ':');
- if (driver) {
- driver[0] = '\0';
- driver++;
+ sep = strchr(sysname, ':');
+ if (sep) {
+ subsys = memdupa_suffix0(sysname, sep - sysname);
- if (snprintf_ok(syspath, sizeof syspath, "/sys/subsystem/%s/drivers/%s", subsys, driver) &&
+ if (snprintf_ok(syspath, sizeof syspath, "/sys/subsystem/%s/drivers/%s", subsys, sep + 1) &&
access(syspath, F_OK) >= 0)
return sd_device_new_from_syspath(ret, syspath);
- if (snprintf_ok(syspath, sizeof syspath, "/sys/bus/%s/drivers/%s", subsys, driver) &&
+ if (snprintf_ok(syspath, sizeof syspath, "/sys/bus/%s/drivers/%s", subsys, sep + 1) &&
access(syspath, F_OK) >= 0)
return sd_device_new_from_syspath(ret, syspath);
}
@@ -653,17 +650,15 @@ _public_ int sd_device_new_from_device_id(sd_device **ret, const char *id) {
}
case '+': {
- char subsys[NAME_MAX+1]; /* NAME_MAX does not include the trailing NUL. */
- const char *sysname;
+ const char *subsys, *sep;
- sysname = strchr(id + 1, ':');
- if (!sysname)
+ sep = strchr(id + 1, ':');
+ if (!sep || sep - id - 1 > NAME_MAX)
return -EINVAL;
- (void) strnscpy(subsys, sizeof(subsys), id + 1, sysname - id - 1);
- sysname++;
+ subsys = memdupa_suffix0(id + 1, sep - id - 1);
- return sd_device_new_from_subsystem_sysname(ret, subsys, sysname);
+ return sd_device_new_from_subsystem_sysname(ret, subsys, sep + 1);
}
default: