aboutsummaryrefslogtreecommitdiffstats
path: root/lib/misc.c
diff options
context:
space:
mode:
authordequis <dx@dxzone.com.ar>2016-11-12 00:38:34 -0300
committerdequis <dx@dxzone.com.ar>2016-11-12 00:38:34 -0300
commitfca468311f1fd9880ed2ae4991b2ecc261fd34d5 (patch)
tree63f970550d1a87fc50d438ba73c5fb409cf32c36 /lib/misc.c
parent727a68b876f575bd463d2f5e4b7f65ccee759b2b (diff)
word_wrap: truncate utf8 safely
Diffstat (limited to 'lib/misc.c')
-rw-r--r--lib/misc.c12
1 files changed, 9 insertions, 3 deletions
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);