Code

get_author_initials: various fixes master
authorPierre Habouzit <madcoder@debian.org>
Mon, 2 May 2011 11:10:28 +0000 (13:10 +0200)
committerJonas Fonseca <fonseca@diku.dk>
Tue, 3 May 2011 01:38:50 +0000 (21:38 -0400)
Do not pull partial multi-byte sequences.

Signed-off-by: Pierre Habouzit <madcoder@debian.org>
Signed-off-by: Jonas Fonseca <fonseca@diku.dk>
tig.c

diff --git a/tig.c b/tig.c
index d33d3eb528c8920328cb4b4e87d9b1fe6a9119a7..b0032ea67a9fd869b658fa3e984c38007a6250d6 100644 (file)
--- 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;