From: Pierre Habouzit Date: Mon, 2 May 2011 11:10:28 +0000 (+0200) Subject: get_author_initials: various fixes X-Git-Url: https://git.tokkee.org/?p=tig.git;a=commitdiff_plain;h=e60ef00ee281973dee588598e199b5913b837050 get_author_initials: various fixes Do not pull partial multi-byte sequences. Signed-off-by: Pierre Habouzit Signed-off-by: Jonas Fonseca --- diff --git a/tig.c b/tig.c index d33d3eb..b0032ea 100644 --- a/tig.c +++ b/tig.c @@ -158,19 +158,27 @@ get_author_initials(const char *author) unsigned char bytes; size_t i; - while (is_initial_sep(*author)) + while (author < end && is_initial_sep(*author)) author++; bytes = utf8_char_length(author, end); - if (bytes < sizeof(initials) - 1 - pos) { - while (bytes--) { - initials[pos++] = *author++; - } + if (bytes >= sizeof(initials) - 1 - pos) + break; + while (bytes--) { + initials[pos++] = *author++; } - for (i = pos; author < end && !is_initial_sep(*author); author++) { - if (i < sizeof(initials) - 1) - initials[i++] = *author; + i = pos; + while (author < end && !is_initial_sep(*author)) { + bytes = utf8_char_length(author, end); + if (bytes >= sizeof(initials) - 1 - i) { + while (author < end && !is_initial_sep(*author)) + author++; + break; + } + while (bytes--) { + initials[i++] = *author++; + } } initials[i++] = 0;