truncate UTF-8 strings only at the boundary between two characters
authorMartin Kaiser <wireshark@kaiser.cx>
Sat, 1 Mar 2014 10:25:47 +0000 (13:25 +0300)
committerAnders Broman <a.broman58@gmail.com>
Mon, 3 Mar 2014 22:05:17 +0000 (22:05 +0000)
Change-Id: Ib3ffc1593e877f4f7c708712b82209cf969cecff
Reviewed-on: https://code.wireshark.org/review/464
Reviewed-by: Evan Huus <eapache@gmail.com>
Reviewed-by: Anders Broman <a.broman58@gmail.com>
epan/proto.c

index 22842273453be55a9c4ae74924787ea8235fac04..9158de24f73b8738a7813e16329503245d211e80 100644 (file)
@@ -5418,8 +5418,9 @@ label_concat(char *label_str, gsize pos, const char *str)
 static void
 label_mark_truncated(char *label_str, gsize name_pos)
 {
-       static const char trunc_str[] = " [truncated]";
-       const size_t trunc_len = sizeof(trunc_str)-1;
+       static const char  trunc_str[] = " [truncated]";
+       const size_t       trunc_len = sizeof(trunc_str)-1;
+       gchar             *last_char;
 
        /* ..... field_name: dataaaaaaaaaaaaa
         *                 |
@@ -5430,7 +5431,17 @@ label_mark_truncated(char *label_str, gsize name_pos)
        if (name_pos < ITEM_LABEL_LENGTH - trunc_len) {
                memmove(label_str + name_pos + trunc_len, label_str + name_pos, ITEM_LABEL_LENGTH - name_pos - trunc_len);
                memcpy(label_str + name_pos, trunc_str, trunc_len);
-               label_str[ITEM_LABEL_LENGTH-1] = '\0';
+
+               /* in general, label_str is UTF-8
+                  we can truncate it only at the beginning of a new character
+                  we go backwards from the byte right after our buffer and 
+                   find the next starting byte of a UTF-8 character, this is
+                   where we cut
+                  there's no need to use g_utf8_find_prev_char(), the search
+                   will always succeed since we copied trunc_str into the
+                   buffer */
+               last_char = g_utf8_prev_char(&label_str[ITEM_LABEL_LENGTH]);
+               *last_char = '\0';
 
        } else if (name_pos < ITEM_LABEL_LENGTH)
                g_strlcpy(label_str + name_pos, trunc_str, ITEM_LABEL_LENGTH - name_pos);