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/text.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'source/blender/blenkernel/intern/text.c') 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