diff options
author | Zdenek Kabelac <zkabelac@redhat.com> | 2023-02-14 20:59:56 +0100 |
---|---|---|
committer | Zdenek Kabelac <zkabelac@redhat.com> | 2023-02-17 00:00:04 +0100 |
commit | 7bc5c8ac3d4c6004de24050fd51d3eaa4732822d (patch) | |
tree | 976917cdb54bf6a3c8ae98c67b8b380645cd8bfd /lib | |
parent | fb930c2165c2b62daf2adc9d97d59f129132e5c3 (diff) | |
download | lvm2-7bc5c8ac3d4c6004de24050fd51d3eaa4732822d.tar.gz |
cov: avoid using strcpy
Coverity is complaining about unchecked strcpy here, which is
irelevant as we preallocate buffer to fit in copied string,
however we could actually reuse these size and use just memcpy().
So lets make some simple conversions.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/activate/fs.c | 5 | ||||
-rw-r--r-- | lib/commands/toolcontext.c | 8 | ||||
-rw-r--r-- | lib/format_text/format-text.c | 7 |
3 files changed, 10 insertions, 10 deletions
diff --git a/lib/activate/fs.c b/lib/activate/fs.c index 3e0692c9d..c8b304f9d 100644 --- a/lib/activate/fs.c +++ b/lib/activate/fs.c @@ -318,9 +318,10 @@ struct fs_op_parms { static void _store_str(char **pos, char **ptr, const char *str) { - strcpy(*pos, str); + size_t len = strlen(str) + 1; + memcpy(*pos, str, len); *ptr = *pos; - *pos += strlen(*ptr) + 1; + *pos += len; } static void _del_fs_op(struct fs_op_parms *fsp) diff --git a/lib/commands/toolcontext.c b/lib/commands/toolcontext.c index b630554a9..4e05f9078 100644 --- a/lib/commands/toolcontext.c +++ b/lib/commands/toolcontext.c @@ -1581,7 +1581,7 @@ struct cmd_context *create_config_context(void) if (!(cmd = zalloc(sizeof(*cmd)))) goto_out; - strcpy(cmd->system_dir, DEFAULT_SYS_DIR); + strncpy(cmd->system_dir, DEFAULT_SYS_DIR, sizeof(cmd->system_dir) - 1); if (!_get_env_vars(cmd)) goto_out; @@ -1713,10 +1713,8 @@ struct cmd_context *create_toolcontext(unsigned is_clvmd, /* * Environment variable LVM_SYSTEM_DIR overrides this below. */ - if (system_dir) - strncpy(cmd->system_dir, system_dir, sizeof(cmd->system_dir) - 1); - else - strcpy(cmd->system_dir, DEFAULT_SYS_DIR); + strncpy(cmd->system_dir, (system_dir) ? system_dir : DEFAULT_SYS_DIR, + sizeof(cmd->system_dir) - 1); if (!_get_env_vars(cmd)) goto_out; diff --git a/lib/format_text/format-text.c b/lib/format_text/format-text.c index c1ccdb031..3d4eac27b 100644 --- a/lib/format_text/format-text.c +++ b/lib/format_text/format-text.c @@ -1389,7 +1389,7 @@ static int _vg_commit_file(struct format_instance *fid, struct volume_group *vg, struct text_context *tc = (struct text_context *) mda->metadata_locn; const char *slash; char new_name[PATH_MAX]; - size_t len; + size_t len, vglen; if (!_vg_commit_file_backup(fid, vg, mda)) return 0; @@ -1401,14 +1401,15 @@ static int _vg_commit_file(struct format_instance *fid, struct volume_group *vg, slash = tc->path_live; if (strcmp(slash, vg->name)) { + vglen = strlen(vg->name) + 1; len = slash - tc->path_live; - if ((len + strlen(vg->name)) > (sizeof(new_name) - 1)) { + if ((len + vglen) > (sizeof(new_name) - 1)) { log_error("Renaming path %s is too long for VG %s.", tc->path_live, vg->name); return 0; } strncpy(new_name, tc->path_live, len); - strcpy(new_name + len, vg->name); + memcpy(new_name + len, vg->name, vglen); log_debug_metadata("Renaming %s to %s", tc->path_live, new_name); if (test_mode()) log_verbose("Test mode: Skipping rename"); |