Skip to content

Commit 2c2540e

Browse files
Thalleyhenrikbrixandersen
authored andcommitted
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 <[email protected]>
1 parent a0c8a43 commit 2c2540e

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

lib/utils/utf8.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,14 @@
1616

1717
char *utf8_trunc(char *utf8_str)
1818
{
19-
char *last_byte_p = utf8_str + strlen(utf8_str) - 1;
19+
const size_t len = strlen(utf8_str);
20+
21+
if (len == 0U) {
22+
/* no-op */
23+
return utf8_str;
24+
}
25+
26+
char *last_byte_p = utf8_str + len - 1U;
2027
uint8_t bytes_truncated;
2128
char seq_start_byte;
2229

0 commit comments

Comments
 (0)