From 2c8bb611877cc894958252ac2c960893a5681435 Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Sat, 12 Apr 2025 13:03:01 +1000 Subject: [PATCH] Cleanup: add STRNLEN macros to prevent incorrect sizeof() use This would have prevented the error fixed in be53bab1cbcdfafe4757cae389cb07a13ae4d49b. --- source/blender/blenkernel/intern/cryptomatte.cc | 2 +- source/blender/blenkernel/intern/preferences.cc | 2 +- source/blender/blenlib/BLI_string.h | 1 + source/blender/blenlib/BLI_string_utf8.h | 3 +++ source/blender/blenloader/intern/versioning_400.cc | 2 +- .../editors/interface/eyedroppers/eyedropper_color.cc | 2 +- source/blender/editors/space_buttons/space_buttons.cc | 4 +--- .../blender/editors/space_sequencer/sequencer_text_edit.cc | 6 +++--- source/blender/sequencer/intern/effects/vse_effect_text.cc | 2 +- 9 files changed, 13 insertions(+), 11 deletions(-) diff --git a/source/blender/blenkernel/intern/cryptomatte.cc b/source/blender/blenkernel/intern/cryptomatte.cc index e89e24b9a16..e26e679828d 100644 --- a/source/blender/blenkernel/intern/cryptomatte.cc +++ b/source/blender/blenkernel/intern/cryptomatte.cc @@ -264,7 +264,7 @@ char *BKE_cryptomatte_entries_to_matte_id(NodeCryptomatte *node_storage) if (!first) { BLI_dynstr_append(matte_id, ","); } - if (BLI_strnlen(entry->name, sizeof(entry->name)) != 0) { + if (STRNLEN(entry->name) != 0) { BLI_dynstr_nappend(matte_id, entry->name, sizeof(entry->name)); } else { diff --git a/source/blender/blenkernel/intern/preferences.cc b/source/blender/blenkernel/intern/preferences.cc index 37c4ec94349..2b74dfdbe8d 100644 --- a/source/blender/blenkernel/intern/preferences.cc +++ b/source/blender/blenkernel/intern/preferences.cc @@ -292,7 +292,7 @@ bool BKE_preferences_extension_repo_module_is_valid(const bUserExtensionRepo *re if (module_len == 0) { return false; } - if (module_len != BLI_strnlen(repo->module, sizeof(repo->module))) { + if (module_len != STRNLEN(repo->module)) { return false; } return true; diff --git a/source/blender/blenlib/BLI_string.h b/source/blender/blenlib/BLI_string.h index 77b200439ae..6965f13f547 100644 --- a/source/blender/blenlib/BLI_string.h +++ b/source/blender/blenlib/BLI_string.h @@ -595,6 +595,7 @@ bool BLI_string_elem_split_by_delim(const char *haystack, len += BLI_strncpy_rlen(dst + len, suffix, ARRAY_SIZE(dst) - len) #define STR_CONCATF(dst, len, format, ...) \ len += BLI_snprintf_rlen(dst + len, ARRAY_SIZE(dst) - len, format, __VA_ARGS__) +#define STRNLEN(str) BLI_strnlen(str, ARRAY_SIZE(str)) /** \} */ diff --git a/source/blender/blenlib/BLI_string_utf8.h b/source/blender/blenlib/BLI_string_utf8.h index 2d1cf89523e..4dee8152cf3 100644 --- a/source/blender/blenlib/BLI_string_utf8.h +++ b/source/blender/blenlib/BLI_string_utf8.h @@ -263,4 +263,7 @@ int BLI_str_utf8_offset_from_column_with_tabs(const char *str, #define STRNCPY_UTF8(dst, src) BLI_strncpy_utf8(dst, src, ARRAY_SIZE(dst)) #define STRNCPY_UTF8_RLEN(dst, src) BLI_strncpy_utf8_rlen(dst, src, ARRAY_SIZE(dst)) +#define STRNLEN_UTF8(str) BLI_strnlen_utf8(str, ARRAY_SIZE(str)) + + /** \} */ diff --git a/source/blender/blenloader/intern/versioning_400.cc b/source/blender/blenloader/intern/versioning_400.cc index 9df566030b3..bd182855ecb 100644 --- a/source/blender/blenloader/intern/versioning_400.cc +++ b/source/blender/blenloader/intern/versioning_400.cc @@ -6774,7 +6774,7 @@ void blo_do_versions_400(FileData *fd, Library * /*lib*/, Main *bmain) LISTBASE_FOREACH (Image *, image, &bmain->images) { LISTBASE_FOREACH (RenderSlot *, slot, &image->renderslots) { if (slot->name[0]) { - BLI_str_utf8_invalid_strip(slot->name, BLI_strnlen(slot->name, sizeof(slot->name))); + BLI_str_utf8_invalid_strip(slot->name, STRNLEN(slot->name)); } } } diff --git a/source/blender/editors/interface/eyedroppers/eyedropper_color.cc b/source/blender/editors/interface/eyedroppers/eyedropper_color.cc index a231dbd668b..7c2d1369ece 100644 --- a/source/blender/editors/interface/eyedroppers/eyedropper_color.cc +++ b/source/blender/editors/interface/eyedroppers/eyedropper_color.cc @@ -229,7 +229,7 @@ static bool eyedropper_cryptomatte_sample_renderlayer_fl(RenderLayer *render_lay return false; } - const int render_layer_name_len = BLI_strnlen(render_layer->name, sizeof(render_layer->name)); + const int render_layer_name_len = STRNLEN(render_layer->name); if (strncmp(prefix, render_layer->name, render_layer_name_len) != 0) { return false; } diff --git a/source/blender/editors/space_buttons/space_buttons.cc b/source/blender/editors/space_buttons/space_buttons.cc index cbaea414896..c214839f495 100644 --- a/source/blender/editors/space_buttons/space_buttons.cc +++ b/source/blender/editors/space_buttons/space_buttons.cc @@ -319,9 +319,7 @@ const char *ED_buttons_search_string_get(SpaceProperties *sbuts) int ED_buttons_search_string_length(SpaceProperties *sbuts) { - return (sbuts->runtime) ? - BLI_strnlen(sbuts->runtime->search_string, sizeof(sbuts->runtime->search_string)) : - 0; + return (sbuts->runtime) ? STRNLEN(sbuts->runtime->search_string) : 0; } void ED_buttons_search_string_set(SpaceProperties *sbuts, const char *value) diff --git a/source/blender/editors/space_sequencer/sequencer_text_edit.cc b/source/blender/editors/space_sequencer/sequencer_text_edit.cc index 3f167149846..b430cee54cf 100644 --- a/source/blender/editors/space_sequencer/sequencer_text_edit.cc +++ b/source/blender/editors/space_sequencer/sequencer_text_edit.cc @@ -390,7 +390,7 @@ static bool text_insert(TextVars *data, const char *buf, const size_t buf_len) const bool selection_was_deleted = text_has_selection(data); delete_selected_text(data); - const size_t text_str_len = BLI_strnlen(data->text, sizeof(data->text)); + const size_t text_str_len = STRNLEN(data->text); if (text_str_len + buf_len + 1 > sizeof(data->text)) { return selection_was_deleted; @@ -415,7 +415,7 @@ static wmOperatorStatus sequencer_text_insert_exec(bContext *C, wmOperator *op) char str[512]; RNA_string_get(op->ptr, "string", str); - const size_t in_buf_len = BLI_strnlen(str, sizeof(str)); + const size_t in_buf_len = STRNLEN(str); if (in_buf_len == 0) { return OPERATOR_CANCELLED | OPERATOR_PASS_THROUGH; } @@ -830,7 +830,7 @@ static wmOperatorStatus sequencer_text_edit_paste_exec(bContext *C, wmOperator * } delete_selected_text(data); - const int max_str_len = sizeof(data->text) - (BLI_strnlen(data->text, sizeof(data->text)) + 1); + const int max_str_len = sizeof(data->text) - (STRNLEN(data->text) + 1); /* Maximum bytes that can be filled into `data->text`. */ const int fillable_len = std::min(clipboard_len, max_str_len); diff --git a/source/blender/sequencer/intern/effects/vse_effect_text.cc b/source/blender/sequencer/intern/effects/vse_effect_text.cc index a7e5259e45f..91834b38f89 100644 --- a/source/blender/sequencer/intern/effects/vse_effect_text.cc +++ b/source/blender/sequencer/intern/effects/vse_effect_text.cc @@ -808,7 +808,7 @@ static int text_effect_font_init(const RenderData *context, const Strip *strip, static Vector build_character_info(const TextVars *data, int font) { Vector characters; - const size_t len_max = BLI_strnlen(data->text, sizeof(data->text)); + const size_t len_max = STRNLEN(data->text); int byte_offset = 0; int char_index = 0;