From fca468311f1fd9880ed2ae4991b2ecc261fd34d5 Mon Sep 17 00:00:00 2001 From: dequis Date: Sat, 12 Nov 2016 00:38:34 -0300 Subject: word_wrap: truncate utf8 safely --- lib/misc.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) (limited to 'lib/misc.c') diff --git a/lib/misc.c b/lib/misc.c index b4c02ed1..c611d99f 100644 --- a/lib/misc.c +++ b/lib/misc.c @@ -548,7 +548,6 @@ void srv_free(struct ns_srv_reply **srv) g_free(srv); } -/* Word wrapping. Yes, I know this isn't UTF-8 clean. I'm willing to take the risk. */ char *word_wrap(const char *msg, int line_len) { GString *ret = g_string_sized_new(strlen(msg) + 16); @@ -581,9 +580,16 @@ char *word_wrap(const char *msg, int line_len) } } if (i == 0) { - g_string_append_len(ret, msg, line_len); + const char *end; + size_t len; + + g_utf8_validate(msg, line_len, &end); + + len = (end != msg) ? end - msg : line_len; + + g_string_append_len(ret, msg, len); g_string_append_c(ret, '\n'); - msg += line_len; + msg += len; } } g_string_append(ret, msg); -- cgit v1.2.3