1
0
mirror of git://sourceware.org/git/lvm2.git synced 2024-12-21 13:34:40 +03:00

cov: use memccpy

When we want to copy string which may not be null terminated,
replace strncpy with more correct memccpy.
This commit is contained in:
Zdenek Kabelac 2024-04-09 00:31:57 +02:00
parent f27273e1f0
commit 22a0cfdc05
4 changed files with 10 additions and 19 deletions

View File

@ -2981,7 +2981,7 @@ static int add_lockspace_thread(const char *ls_name,
if (vg_uuid) if (vg_uuid)
/* coverity[buffer_size_warning] */ /* coverity[buffer_size_warning] */
strncpy(ls->vg_uuid, vg_uuid, 64); memccpy(ls->vg_uuid, vg_uuid, 0, 64);
if (vg_name) if (vg_name)
dm_strncpy(ls->vg_name, vg_name, sizeof(ls->vg_name)); dm_strncpy(ls->vg_name, vg_name, sizeof(ls->vg_name));
@ -4909,7 +4909,7 @@ static void client_recv_action(struct client *cl)
dm_strncpy(act->vg_name, vg_name, sizeof(act->vg_name)); dm_strncpy(act->vg_name, vg_name, sizeof(act->vg_name));
if (vg_uuid && strcmp(vg_uuid, "none")) if (vg_uuid && strcmp(vg_uuid, "none"))
strncpy(act->vg_uuid, vg_uuid, 64); memccpy(act->vg_uuid, vg_uuid, 0, 64);
if (vg_sysid && strcmp(vg_sysid, "none")) if (vg_sysid && strcmp(vg_sysid, "none"))
dm_strncpy(act->vg_sysid, vg_sysid, sizeof(act->vg_sysid)); dm_strncpy(act->vg_sysid, vg_sysid, sizeof(act->vg_sysid));

View File

@ -231,16 +231,11 @@ static uint64_t daemon_test_lv_count;
* Copy a null-terminated string "str" into a fixed * Copy a null-terminated string "str" into a fixed
* size struct field "buf" which is not null terminated. * size struct field "buf" which is not null terminated.
* (ATM SANLK_NAME_LEN is only 48 bytes. * (ATM SANLK_NAME_LEN is only 48 bytes.
* Avoid strncpy() for coverity issues. * Use memccpy() instead of strncpy().
*/ */
static void strcpy_name_len(char *buf, const char *str, size_t len) static void strcpy_name_len(char *buf, const char *str, size_t len)
{ {
size_t l; memccpy(buf, str, 0, len);
/* copy at most len sized length of str */
for (l = 0; l < len; ++l)
if (!(buf[l] = str[l]))
break;
} }
static int lock_lv_name_from_args(char *vg_args, char *lock_lv_name) static int lock_lv_name_from_args(char *vg_args, char *lock_lv_name)

View File

@ -1256,7 +1256,7 @@ static int _lookup_dev_name(uint64_t dev, char *buf, size_t len)
do { do {
names = (struct dm_names *)((char *) names + next); names = (struct dm_names *)((char *) names + next);
if (names->dev == dev) { if (names->dev == dev) {
strncpy(buf, names->name, len); memccpy(buf, names->name, 0, len);
r = 1; r = 1;
break; break;
} }
@ -1425,12 +1425,10 @@ static struct dm_ioctl *_flatten(struct dm_task *dmt, unsigned repeat_count)
/* FIXME Until resume ioctl supplies name, use dev_name for readahead */ /* FIXME Until resume ioctl supplies name, use dev_name for readahead */
if (DEV_NAME(dmt) && (dmt->type != DM_DEVICE_RESUME || dmt->minor < 0 || if (DEV_NAME(dmt) && (dmt->type != DM_DEVICE_RESUME || dmt->minor < 0 ||
dmt->major < 0)) dmt->major < 0))
/* coverity[buffer_size_warning] */ memccpy(dmi->name, DEV_NAME(dmt), 0, sizeof(dmi->name));
strncpy(dmi->name, DEV_NAME(dmt), sizeof(dmi->name));
if (DEV_UUID(dmt)) if (DEV_UUID(dmt))
/* coverity[buffer_size_warning] */ memccpy(dmi->uuid, DEV_UUID(dmt), 0, sizeof(dmi->uuid));
strncpy(dmi->uuid, DEV_UUID(dmt), sizeof(dmi->uuid));
if (dmt->type == DM_DEVICE_SUSPEND) if (dmt->type == DM_DEVICE_SUSPEND)
dmi->flags |= DM_SUSPEND_FLAG; dmi->flags |= DM_SUSPEND_FLAG;

View File

@ -1128,7 +1128,7 @@ static int _lookup_dev_name(uint64_t dev, char *buf, size_t len)
do { do {
names = (struct dm_names *)((char *) names + next); names = (struct dm_names *)((char *) names + next);
if (names->dev == dev) { if (names->dev == dev) {
strncpy(buf, names->name, len); memccpy(buf, names->name, 0, len);
r = 1; r = 1;
break; break;
} }
@ -1282,12 +1282,10 @@ static struct dm_ioctl *_flatten(struct dm_task *dmt, unsigned repeat_count)
(dmt->minor < 0) || (dmt->major < 0))) (dmt->minor < 0) || (dmt->major < 0)))
/* When RESUME or RELOAD sets maj:min and dev_name, use just maj:min, /* When RESUME or RELOAD sets maj:min and dev_name, use just maj:min,
* passed dev_name is useful for better error/debug messages */ * passed dev_name is useful for better error/debug messages */
/* coverity[buffer_size_warning] */ memccpy(dmi->name, DEV_NAME(dmt), 0, sizeof(dmi->name));
strncpy(dmi->name, DEV_NAME(dmt), sizeof(dmi->name));
if (DEV_UUID(dmt)) if (DEV_UUID(dmt))
/* coverity[buffer_size_warning] */ memccpy(dmi->uuid, DEV_UUID(dmt), 0, sizeof(dmi->uuid));
strncpy(dmi->uuid, DEV_UUID(dmt), sizeof(dmi->uuid));
if (dmt->type == DM_DEVICE_SUSPEND) if (dmt->type == DM_DEVICE_SUSPEND)
dmi->flags |= DM_SUSPEND_FLAG; dmi->flags |= DM_SUSPEND_FLAG;