Truncate UTF-8 properly

This will avoid to produce invalid UTF-8 results (although unlikely).
This commit is contained in:
Romain Vimont 2019-05-30 19:13:19 +02:00
parent 0a7fe7ad57
commit 6ec2ddd2d1

View file

@ -4,6 +4,7 @@
#include "buffer_util.h" #include "buffer_util.h"
#include "log.h" #include "log.h"
#include "str_util.h"
static void static void
write_position(uint8_t *buf, const struct position *position) { write_position(uint8_t *buf, const struct position *position) {
@ -24,11 +25,10 @@ control_event_serialize(const struct control_event *event, unsigned char *buf) {
return 10; return 10;
case CONTROL_EVENT_TYPE_TEXT: { case CONTROL_EVENT_TYPE_TEXT: {
// write length (2 bytes) + string (non nul-terminated) // write length (2 bytes) + string (non nul-terminated)
size_t len = strlen(event->text_event.text);
if (len > CONTROL_EVENT_TEXT_MAX_LENGTH) {
// injecting a text takes time, so limit the text length // injecting a text takes time, so limit the text length
len = CONTROL_EVENT_TEXT_MAX_LENGTH; size_t len = utf8_truncation_index(event->text_event.text,
} CONTROL_EVENT_TEXT_MAX_LENGTH);
buffer_write16be(&buf[1], (uint16_t) len); buffer_write16be(&buf[1], (uint16_t) len);
memcpy(&buf[3], event->text_event.text, len); memcpy(&buf[3], event->text_event.text, len);
return 3 + len; return 3 + len;