diff options
author | Martin Kaiser <wireshark@kaiser.cx> | 2014-03-01 13:25:47 +0300 |
---|---|---|
committer | Anders Broman <a.broman58@gmail.com> | 2014-03-03 22:05:17 +0000 |
commit | f94674d2fb3e14ce2da1ca5dbce7bf6eacfe7586 (patch) | |
tree | d0856b1c2232bdea4b2088a07f2af4c19b5ed3f3 | |
parent | 4ddd7a637e8715f099ef804d08b169e0df0f0b7a (diff) |
truncate UTF-8 strings only at the boundary between two characters
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>
-rw-r--r-- | epan/proto.c | 17 |
1 files changed, 14 insertions, 3 deletions
diff --git a/epan/proto.c b/epan/proto.c index 2284227345..9158de24f7 100644 --- a/epan/proto.c +++ b/epan/proto.c @@ -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); |