Skip to content

Commit

Permalink
zephyr: Add zero-len check for utf8_trunc
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
Thalley authored and henrikbrixandersen committed Sep 18, 2024
1 parent a0c8a43 commit 2c2540e
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/utils/utf8.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit 2c2540e

Please sign in to comment.