From 472e6563b05d0ca57ee97c3ebf5ad2fc549c3276 Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Tue, 23 May 2023 15:09:58 +1000 Subject: [PATCH] Cleanup: replace strncpy calls with BLI_strncpy Prefer BLI_strncpy as it ensures the string is null terminated. --- source/blender/blenkernel/intern/unit.c | 13 +++++++++---- source/blender/blenlib/intern/winstuff.c | 3 +-- source/blender/editors/interface/interface_panel.cc | 2 +- .../io/wavefront_obj/tests/obj_exporter_tests.cc | 2 +- .../io/wavefront_obj/tests/obj_importer_tests.cc | 3 ++- 5 files changed, 14 insertions(+), 9 deletions(-) diff --git a/source/blender/blenkernel/intern/unit.c b/source/blender/blenkernel/intern/unit.c index 47ab904198f..2f3f1f6769b 100644 --- a/source/blender/blenkernel/intern/unit.c +++ b/source/blender/blenkernel/intern/unit.c @@ -1113,7 +1113,7 @@ bool BKE_unit_replace_string( /* Apply the default unit on the whole expression, this allows to handle nasty cases like * '2+2in'. */ if (SNPRINTF(str_tmp, "(%s)*%.9g", str, default_unit->scalar) < sizeof(str_tmp)) { - strncpy(str, str_tmp, str_maxncpy); + BLI_strncpy(str, str_tmp, str_maxncpy); } else { /* BLI_snprintf would not fit into str_tmp, can't do much in this case. @@ -1190,8 +1190,13 @@ void BKE_unit_name_to_alt(char *str, int str_maxncpy, const char *orig_str, int int len_name = 0; /* Copy everything before the unit. */ - offset = (offset < str_maxncpy ? offset : str_maxncpy); - strncpy(str, orig_str, offset); + if (offset < str_maxncpy) { + memcpy(str, orig_str, offset); + } + else { + BLI_strncpy(str, orig_str, str_maxncpy); + offset = str_maxncpy; + } str += offset; orig_str += offset + strlen(unit->name_short); @@ -1213,7 +1218,7 @@ void BKE_unit_name_to_alt(char *str, int str_maxncpy, const char *orig_str, int } /* Finally copy the rest of the string. */ - strncpy(str, orig_str, str_maxncpy); + BLI_strncpy(str, orig_str, str_maxncpy); } double BKE_unit_closest_scalar(double value, int system, int type) diff --git a/source/blender/blenlib/intern/winstuff.c b/source/blender/blenlib/intern/winstuff.c index 835ba725562..0e006c8493d 100644 --- a/source/blender/blenlib/intern/winstuff.c +++ b/source/blender/blenlib/intern/winstuff.c @@ -160,8 +160,7 @@ bool BLI_windows_register_blend_extension(const bool background) BLI_windows_get_executable_dir(InstallDir); GetSystemDirectory(SysDir, FILE_MAXDIR); const char *ThumbHandlerDLL = "BlendThumb.dll"; - snprintf( - RegCmd, MAX_PATH * 2, "%s\\regsvr32 /s \"%s\\%s\"", SysDir, InstallDir, ThumbHandlerDLL); + SNPRINTF(RegCmd, "%s\\regsvr32 /s \"%s\\%s\"", SysDir, InstallDir, ThumbHandlerDLL); system(RegCmd); } # endif diff --git a/source/blender/editors/interface/interface_panel.cc b/source/blender/editors/interface/interface_panel.cc index 764854da930..0ee42f4b988 100644 --- a/source/blender/editors/interface/interface_panel.cc +++ b/source/blender/editors/interface/interface_panel.cc @@ -285,7 +285,7 @@ void UI_list_panel_unique_str(Panel *panel, char *r_name) { /* The panel sort-order will be unique for a specific panel type because the instanced * panel list is regenerated for every change in the data order / length. */ - snprintf(r_name, INSTANCED_PANEL_UNIQUE_STR_LEN, "%d", panel->sortorder); + BLI_snprintf(r_name, INSTANCED_PANEL_UNIQUE_STR_LEN, "%d", panel->sortorder); } /** diff --git a/source/blender/io/wavefront_obj/tests/obj_exporter_tests.cc b/source/blender/io/wavefront_obj/tests/obj_exporter_tests.cc index 059692ab768..d82cfa2d545 100644 --- a/source/blender/io/wavefront_obj/tests/obj_exporter_tests.cc +++ b/source/blender/io/wavefront_obj/tests/obj_exporter_tests.cc @@ -276,7 +276,7 @@ class obj_exporter_regression_test : public obj_exporter_test { BKE_tempdir_init(nullptr); std::string tempdir = std::string(BKE_tempdir_base()); std::string out_file_path = tempdir + BLI_path_basename(golden_obj.c_str()); - strncpy(params.filepath, out_file_path.c_str(), FILE_MAX - 1); + STRNCPY(params.filepath, out_file_path.c_str()); params.blen_filepath = bfile->main->filepath; std::string golden_file_path = blender::tests::flags_test_asset_dir() + SEP_STR + golden_obj; BLI_path_split_dir_part( diff --git a/source/blender/io/wavefront_obj/tests/obj_importer_tests.cc b/source/blender/io/wavefront_obj/tests/obj_importer_tests.cc index 020c59bbfdf..40e5bfc9178 100644 --- a/source/blender/io/wavefront_obj/tests/obj_importer_tests.cc +++ b/source/blender/io/wavefront_obj/tests/obj_importer_tests.cc @@ -16,6 +16,7 @@ #include "BLI_listbase.h" #include "BLI_math_base.hh" #include "BLI_math_vector_types.hh" +#include "BLI_string.h" #include "BLO_readfile.h" @@ -74,7 +75,7 @@ class obj_importer_test : public BlendfileLoadingBaseTest { std::string obj_path = blender::tests::flags_test_asset_dir() + SEP_STR "io_tests" SEP_STR "obj" SEP_STR + path; - strncpy(params.filepath, obj_path.c_str(), FILE_MAX - 1); + STRNCPY(params.filepath, obj_path.c_str()); const size_t read_buffer_size = 650; importer_main(bfile->main, bfile->curscene, bfile->cur_view_layer, params, read_buffer_size);