os: avoid potential out-of-bounds access at logVHdrMessageVerb

The LogVHdrMessageVerb function may access an array out of bounds in a
specific edge case. Specifically, the line:

newline = (buf[len - 1] == '\n');

can result in accessing buf[-1] if len == 0, which is undefined behavior.

Commit adds check to avoid access out of bounds at pointed line.

Closes: https://gitlab.freedesktop.org/xorg/xserver/-/issues/1841
Signed-off-by: Mikhail Dmitrichenko <m.dmitrichenko222@gmail.com>
(cherry picked from commit 8d25a89143)

Part-of: <https://gitlab.freedesktop.org/xorg/xserver/-/merge_requests/2077>
This commit is contained in:
Mikhail Dmitrichenko 2025-09-17 17:29:49 +03:00 committed by Olivier Fourdan
parent b53d9fa417
commit c49bf5f7fd

View file

@ -834,7 +834,7 @@ LogVHdrMessageVerb(MessageType type, int verb, const char *msg_format,
if (size - len == 1)
buf[len - 1] = '\n';
newline = (buf[len - 1] == '\n');
newline = (len > 0 && buf[len - 1] == '\n');
LogSWrite(verb, buf, len, newline);
}