From 457302b67b9de6a92240c2736306cfa01187101d Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Sat, 28 Aug 2021 22:44:55 +1000 Subject: BLI_string_utf8: add buffer size arg to BLI_str_utf8_from_unicode Besides helping to avoid buffer overflow errors this reduces complexity of BLI_str_utf32_as_utf8 which needed a special loop for the last 6 characters to avoid writing past the buffer bounds. Also add BLI_str_utf8_from_unicode_len which only returns the length. --- source/blender/blenkernel/intern/object_dupli.cc | 4 ++-- source/blender/blenkernel/intern/text.c | 7 ++++--- 2 files changed, 6 insertions(+), 5 deletions(-) (limited to 'source/blender/blenkernel') diff --git a/source/blender/blenkernel/intern/object_dupli.cc b/source/blender/blenkernel/intern/object_dupli.cc index 141a9a25eca..a46ac4b1175 100644 --- a/source/blender/blenkernel/intern/object_dupli.cc +++ b/source/blender/blenkernel/intern/object_dupli.cc @@ -653,10 +653,10 @@ static Object *find_family_object( return *ob_pt; } - char ch_utf8[7]; + char ch_utf8[BLI_UTF8_MAX + 1]; size_t ch_utf8_len; - ch_utf8_len = BLI_str_utf8_from_unicode(ch, ch_utf8); + ch_utf8_len = BLI_str_utf8_from_unicode(ch, ch_utf8, sizeof(ch_utf8) - 1); ch_utf8[ch_utf8_len] = '\0'; ch_utf8_len += 1; /* Compare with null terminator. */ diff --git a/source/blender/blenkernel/intern/text.c b/source/blender/blenkernel/intern/text.c index bdc82fe626c..6b7b3213a83 100644 --- a/source/blender/blenkernel/intern/text.c +++ b/source/blender/blenkernel/intern/text.c @@ -329,7 +329,8 @@ int txt_extended_ascii_as_utf8(char **str) memcpy(newstr + mi, (*str) + i, bad_char); - BLI_str_utf8_from_unicode((*str)[i + bad_char], newstr + mi + bad_char); + const int mofs = mi + bad_char; + BLI_str_utf8_from_unicode((*str)[i + bad_char], newstr + mofs, (length + added) - mofs); i += bad_char + 1; mi += bad_char + 2; } @@ -2005,7 +2006,7 @@ static bool txt_add_char_intern(Text *text, unsigned int add, bool replace_tabs) txt_delete_sel(text); - add_len = BLI_str_utf8_from_unicode(add, ch); + add_len = BLI_str_utf8_from_unicode(add, ch, sizeof(ch)); tmp = MEM_mallocN(text->curl->len + add_len + 1, "textline_string"); @@ -2061,7 +2062,7 @@ bool txt_replace_char(Text *text, unsigned int add) del = BLI_str_utf8_as_unicode_step(text->curl->line, text->curl->len, &del_size); del_size -= text->curc; UNUSED_VARS(del); - add_size = BLI_str_utf8_from_unicode(add, ch); + add_size = BLI_str_utf8_from_unicode(add, ch, sizeof(ch)); if (add_size > del_size) { char *tmp = MEM_mallocN(text->curl->len + add_size - del_size + 1, "textline_string"); -- cgit v1.2.3