]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
dmesg: Only check for newline if input exists
authorTobias Stoeckmann <tobias@stoeckmann.org>
Mon, 2 Feb 2026 18:28:32 +0000 (19:28 +0100)
committerTobias Stoeckmann <tobias@stoeckmann.org>
Mon, 2 Feb 2026 18:32:44 +0000 (19:32 +0100)
If a memory mapped file does not end with a newline, do not check for
its existence. Accessing *end would lead to an out of boundary read.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
sys-utils/dmesg.c

index 12e40a3504493943f62cf4671f28061e4fec3018..3259f3d64775d8b705998f4a700d519f23eeb138 100644 (file)
@@ -946,15 +946,15 @@ static int get_next_syslog_record(struct dmesg_control *ctl,
                        rec->mesg_size = end - begin;
                }
 
-               /* Don't count \n from the last message to the message size */
-               if (*end != '\n' && *(end - 1) == '\n')
-                       rec->mesg_size--;
-
                rec->next_size -= end - rec->next;
                rec->next = rec->next_size > 0 ? end + 1 : NULL;
                if (rec->next_size > 0)
                        rec->next_size--;
 
+               /* Don't count \n from the last message to the message size */
+               if ((!rec->next || *end != '\n') && *(end - 1) == '\n')
+                       rec->mesg_size--;
+
                return 0;
        }