]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
sd-device: allow to read sysattr which contains embedded NUL 20039/head
authorYu Watanabe <watanabe.yu+github@gmail.com>
Sat, 26 Jun 2021 01:40:07 +0000 (10:40 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Sat, 26 Jun 2021 01:48:28 +0000 (10:48 +0900)
This effectively reverts the commit 2a394d0bf2f0afd8b9ed5faeb33f23459e3c6504.

But drop trailing '\r' of the read value, as sd_device_set_sysattr_value() drops it.

Fixes #20025.

src/libsystemd/sd-device/sd-device.c

index 0d04e16818a09a172102404784ecebd32329a51c..388128bf330857332dfe81b720ee84cd449c6beb 100644 (file)
@@ -2022,13 +2022,17 @@ _public_ int sd_device_get_sysattr_value(sd_device *device, const char *sysattr,
                 /* skip non-readable files */
                 return -EPERM;
         else {
-                /* read attribute value */
-                r = read_full_virtual_file(path, &value, NULL);
+                size_t size;
+
+                /* Read attribute value, Some attributes contain embedded '\0'. So, it is necessary to
+                 * also get the size of the result. See issue #20025. */
+                r = read_full_virtual_file(path, &value, &size);
                 if (r < 0)
                         return r;
 
                 /* drop trailing newlines */
-                delete_trailing_chars(value, "\n");
+                while (size > 0 && strchr(NEWLINE, value[--size]))
+                        value[size] = '\0';
         }
 
         /* Unfortunately, we need to return 'const char*' instead of 'char*'. Hence, failure in caching