Cleanup: replace strncpy calls with BLI_strncpy

Prefer BLI_strncpy as it ensures the string is null terminated.
This commit is contained in:
Campbell Barton
2023-05-23 15:09:58 +10:00
parent d1cc16913d
commit 472e6563b0
5 changed files with 14 additions and 9 deletions
+9 -4
View File
@@ -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)
+1 -2
View File
@@ -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
@@ -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);
}
/**
@@ -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(
@@ -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);