Cleanup: add STRNLEN macros to prevent incorrect sizeof() use

This would have prevented the error fixed in
be53bab1cb.
This commit is contained in:
Campbell Barton
2025-04-12 13:03:01 +10:00
parent be53bab1cb
commit 2c8bb61187
9 changed files with 13 additions and 11 deletions

View File

@@ -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 {

View File

@@ -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;

View File

@@ -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))
/** \} */

View File

@@ -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))
/** \} */

View File

@@ -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));
}
}
}

View File

@@ -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;
}

View File

@@ -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)

View File

@@ -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);

View File

@@ -808,7 +808,7 @@ static int text_effect_font_init(const RenderData *context, const Strip *strip,
static Vector<CharInfo> build_character_info(const TextVars *data, int font)
{
Vector<CharInfo> 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;