]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
dmesg: fix wrong size calculation
authorKarel Zak <kzak@redhat.com>
Mon, 26 Feb 2024 13:30:55 +0000 (14:30 +0100)
committerKarel Zak <kzak@redhat.com>
Tue, 27 Feb 2024 15:11:12 +0000 (16:11 +0100)
* don't use string based function strspn() if work with non-zero terminated
  buffer

* make sure that message size is calculated from within buffer pointers

Fixes: https://github.com/util-linux/util-linux/issues/2807
Signed-off-by: Karel Zak <kzak@redhat.com>
(cherry picked from commit 0e5031a425c727b16831c108a92271e240c29595)

sys-utils/dmesg.c

index 48282f1e6cc8af02807d6c220b7763b69e408015..25c674be3b6aa1128c3aefc5eafda4181665ab8c 100644 (file)
@@ -939,13 +939,17 @@ static int get_next_syslog_record(struct dmesg_control *ctl,
                        const char *start = begin + 1;
                        size_t id_size;
 
-                       start = start + strspn(start, " ");
+                       while (start < end && *start == ' ')
+                               start++;
+
                        begin = skip_item(begin, end, "]");
                        id_size = begin - start;
+
                        if (id_size < sizeof(rec->caller_id))
                                xstrncpy(rec->caller_id, start, id_size);
-                       rec->mesg = begin + 1;
-                       rec->mesg_size = end - begin - 1;
+
+                       rec->mesg = begin < end ? begin + 1 : NULL;
+                       rec->mesg_size = begin < end ? end - begin - 1 : 0;
                } else {
                        rec->mesg = begin;
                        rec->mesg_size = end - begin;