From: Karel Zak Date: Mon, 26 Feb 2024 13:30:55 +0000 (+0100) Subject: dmesg: fix wrong size calculation X-Git-Tag: v2.40-rc2~24 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=0e78f7c1b25e0390befe0c1d252420dab44b3488;p=thirdparty%2Futil-linux.git 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) --- 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;