Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCampbell Barton <ideasman42@gmail.com>2021-08-27 09:42:31 +0300
committerCampbell Barton <ideasman42@gmail.com>2021-08-27 10:02:53 +0300
commit89dae554f9d5ae0204ad9c51c5ba00e14b16e858 (patch)
tree2dc6cc9151fe6eba664dfb5b39a14e7c620ce974 /source/blender/editors/interface/interface_widgets.c
parent523bc981cfeecead5050e7af44bbe252c166d718 (diff)
Cleanup: utf8 stepping functions
Various changes to reduce risk of out of bounds errors in utf8 seeking. - Remove BLI_str_prev_char_utf8 This function could potentially scan past the beginning of a string. Use BLI_str_find_prev_char_utf8 instead which takes a limiting string start argument. - Swap arguments for BLI_str_find_prev_char_utf8 so the stepping argument is first and the limiting argument is last. This matches BLI_str_find_next_char_utf8. - Change behavior of these functions to return it the start or end pointers instead of NULL, which complicated use of these functions to calculate offsets. Callers that need to check if the limits were reached can compare the return value with the start/end pointers. - Return 'const char *' from these functions so they don't remove const from the input arguments.
Diffstat (limited to 'source/blender/editors/interface/interface_widgets.c')
-rw-r--r--source/blender/editors/interface/interface_widgets.c17
1 files changed, 9 insertions, 8 deletions
diff --git a/source/blender/editors/interface/interface_widgets.c b/source/blender/editors/interface/interface_widgets.c
index 48f638dac33..a2b86ccd947 100644
--- a/source/blender/editors/interface/interface_widgets.c
+++ b/source/blender/editors/interface/interface_widgets.c
@@ -1498,15 +1498,15 @@ static void widget_draw_submenu_tria(const uiBut *but,
static void ui_text_clip_give_prev_off(uiBut *but, const char *str)
{
- const char *prev_utf8 = BLI_str_find_prev_char_utf8(str, str + but->ofs);
+ const char *prev_utf8 = BLI_str_find_prev_char_utf8(str + but->ofs, str);
const int bytes = str + but->ofs - prev_utf8;
but->ofs -= bytes;
}
-static void ui_text_clip_give_next_off(uiBut *but, const char *str)
+static void ui_text_clip_give_next_off(uiBut *but, const char *str, const char *str_end)
{
- const char *next_utf8 = BLI_str_find_next_char_utf8(str + but->ofs, NULL);
+ const char *next_utf8 = BLI_str_find_next_char_utf8(str + but->ofs, str_end);
const int bytes = next_utf8 - (str + but->ofs);
but->ofs += bytes;
@@ -1739,7 +1739,8 @@ static void ui_text_clip_cursor(const uiFontStyle *fstyle, uiBut *but, const rct
but->strwidth = BLF_width(fstyle->uifont_id, but->editstr + but->ofs, INT_MAX);
if (but->strwidth > okwidth) {
- int len = strlen(but->editstr);
+ const int editstr_len = strlen(but->editstr);
+ int len = editstr_len;
while (but->strwidth > okwidth) {
float width;
@@ -1749,7 +1750,7 @@ static void ui_text_clip_cursor(const uiFontStyle *fstyle, uiBut *but, const rct
/* if cursor is at 20 pixels of right side button we clip left */
if (width > okwidth - 20) {
- ui_text_clip_give_next_off(but, but->editstr);
+ ui_text_clip_give_next_off(but, but->editstr, but->editstr + editstr_len);
}
else {
int bytes;
@@ -1757,7 +1758,7 @@ static void ui_text_clip_cursor(const uiFontStyle *fstyle, uiBut *but, const rct
if (width < 20 && but->ofs > 0) {
ui_text_clip_give_prev_off(but, but->editstr);
}
- bytes = BLI_str_utf8_size(BLI_str_find_prev_char_utf8(but->editstr, but->editstr + len));
+ bytes = BLI_str_utf8_size(BLI_str_find_prev_char_utf8(but->editstr + len, but->editstr));
if (bytes == -1) {
bytes = 1;
}
@@ -1805,7 +1806,7 @@ static void ui_text_clip_right_label(const uiFontStyle *fstyle, uiBut *but, cons
/* chop off the leading text, starting from the right */
while (but->strwidth > okwidth && cp2 > but->drawstr) {
- const char *prev_utf8 = BLI_str_find_prev_char_utf8(but->drawstr, cp2);
+ const char *prev_utf8 = BLI_str_find_prev_char_utf8(cp2, but->drawstr);
const int bytes = cp2 - prev_utf8;
/* shift the text after and including cp2 back by 1 char,
@@ -1825,7 +1826,7 @@ static void ui_text_clip_right_label(const uiFontStyle *fstyle, uiBut *but, cons
/* after the leading text is gone, chop off the : and following space, with ofs */
while ((but->strwidth > okwidth) && (but->ofs < 2)) {
- ui_text_clip_give_next_off(but, but->drawstr);
+ ui_text_clip_give_next_off(but, but->drawstr, but->drawstr + drawstr_len);
but->strwidth = BLF_width(
fstyle->uifont_id, but->drawstr + but->ofs, sizeof(but->drawstr) - but->ofs);
if (but->strwidth < 10) {