]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libmount: improving robustness in reading kernel messages
authorKarel Zak <kzak@redhat.com>
Wed, 19 Jun 2024 09:19:49 +0000 (11:19 +0200)
committerKarel Zak <kzak@redhat.com>
Wed, 19 Jun 2024 09:23:18 +0000 (11:23 +0200)
* Ensure data termination from read() function
* Allocate space for terminator using "sizeof(buf)-1"

Signed-off-by: Karel Zak <kzak@redhat.com>
libmount/src/hook_mount.c

index 995de6144449227721b1f72bdb5243f500f5405c..f56479303186c2ab609f5c2f25226b8eb5d8c9e0 100644 (file)
@@ -72,9 +72,15 @@ static void save_fd_messages(struct libmnt_context *cxt, int fd)
 
        mnt_context_set_errmsg(cxt, NULL);
 
-       while ((rc = read(fd, buf, sizeof(buf))) != -1) {
-               if (rc > 0 && buf[rc - 1] == '\n')
-                       buf[rc - 1] = '\0';
+       while ((rc = read(fd, buf, sizeof(buf) - 1)) != -1) {
+
+               if (rc == 0)
+                       continue;
+               if (buf[rc - 1] == '\n')
+                       buf[--rc] = '\0';
+               else
+                       buf[rc] = '\0';
+
                DBG(CXT, ul_debug("message from kernel: \"%*s\"", rc, buf));
 
                if (rc < 3 || strncmp((char *) buf, "e ", 2) != 0)