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 <emil.gydesen@nordicsemi.no>
This commit is contained in:
Emil Gydesen 2024-06-25 13:08:08 +02:00 committed by Anas Nashif
parent a895abab86
commit 4cf00a6125

View file

@ -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;