From 1818d7a0a661294976399fdfb674ded492fb98d0 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 a88cab86a064..6be7c61557ee 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;