From 0e78f7c1b25e0390befe0c1d252420dab44b3488 Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Mon, 26 Feb 2024 14:30:55 +0100 Subject: [PATCH] dmesg: fix wrong size calculation * 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 (cherry picked from commit 0e5031a425c727b16831c108a92271e240c29595) --- sys-utils/dmesg.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/sys-utils/dmesg.c b/sys-utils/dmesg.c index 48282f1e6..25c674be3 100644 --- a/sys-utils/dmesg.c +++ b/sys-utils/dmesg.c @@ -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; -- 2.47.3