Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

module_get_message_part: Avoid splitting UTF-8 sequences #862

Merged
merged 1 commit into from
Oct 22, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/modules/module_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,17 @@ module_get_message_part(const char *message, char *part, unsigned int *pos,
}

for (i = 0; i <= maxlen - 1; i++) {
part[i] = message[*pos];
unsigned char c = message[*pos];

// Stop at UTF-8 boundaries
if (((c & 0xe0) == 0xc0 && i >= maxlen - 1) // Will need one more byte, can't put it.
|| ((c & 0xf0) == 0xe0 && i >= maxlen - 2) // Will need two more bytes, can't put it.
|| ((c & 0xf8) == 0xf0 && i >= maxlen - 3) // Will need three more bytes, can't put it.
|| ((c & 0xfc) == 0xf8 && i >= maxlen - 4) // Will need four more bytes, can't put it.
|| ((c & 0xfe) == 0xfc && i >= maxlen - 5)) // Will need five more bytes, can't put it.
c = 0;

part[i] = c;

if (part[i] == 0) {
return i;
Expand Down