From 4cf00a6125e828caef9851a4569646ff118e617f Mon Sep 17 00:00:00 2001 From: Emil Gydesen Date: Tue, 25 Jun 2024 13:08:08 +0200 Subject: [PATCH] zephyr: Add zero-len check for utf8_trunc The function did not check if the provided string had a zero length before starting to truncate, which meant that last_byte_p could possible have pointed to the value before the string. Signed-off-by: Emil Gydesen --- lib/utils/utf8.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/utils/utf8.c b/lib/utils/utf8.c index ff741806c03..f44ca27c0da 100644 --- a/lib/utils/utf8.c +++ b/lib/utils/utf8.c @@ -16,7 +16,14 @@ char *utf8_trunc(char *utf8_str) { - char *last_byte_p = utf8_str + strlen(utf8_str) - 1; + const size_t len = strlen(utf8_str); + + if (len == 0U) { + /* no-op */ + return utf8_str; + } + + char *last_byte_p = utf8_str + len - 1U; uint8_t bytes_truncated; char seq_start_byte;