From 22a0cfdc05a3550bf8ac15dc0e7d2a6317786fdc Mon Sep 17 00:00:00 2001 From: Zdenek Kabelac Date: Tue, 9 Apr 2024 00:31:57 +0200 Subject: [PATCH] cov: use memccpy When we want to copy string which may not be null terminated, replace strncpy with more correct memccpy. --- daemons/lvmlockd/lvmlockd-core.c | 4 ++-- daemons/lvmlockd/lvmlockd-sanlock.c | 9 ++------- device_mapper/ioctl/libdm-iface.c | 8 +++----- libdm/ioctl/libdm-iface.c | 8 +++----- 4 files changed, 10 insertions(+), 19 deletions(-) diff --git a/daemons/lvmlockd/lvmlockd-core.c b/daemons/lvmlockd/lvmlockd-core.c index 504b2969e..c25fbd961 100644 --- a/daemons/lvmlockd/lvmlockd-core.c +++ b/daemons/lvmlockd/lvmlockd-core.c @@ -2981,7 +2981,7 @@ static int add_lockspace_thread(const char *ls_name, if (vg_uuid) /* coverity[buffer_size_warning] */ - strncpy(ls->vg_uuid, vg_uuid, 64); + memccpy(ls->vg_uuid, vg_uuid, 0, 64); if (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)); 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")) dm_strncpy(act->vg_sysid, vg_sysid, sizeof(act->vg_sysid)); diff --git a/daemons/lvmlockd/lvmlockd-sanlock.c b/daemons/lvmlockd/lvmlockd-sanlock.c index 00347bfba..f0696e3ec 100644 --- a/daemons/lvmlockd/lvmlockd-sanlock.c +++ b/daemons/lvmlockd/lvmlockd-sanlock.c @@ -231,16 +231,11 @@ static uint64_t daemon_test_lv_count; * Copy a null-terminated string "str" into a fixed * size struct field "buf" which is not null terminated. * (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) { - size_t l; - - /* copy at most len sized length of str */ - for (l = 0; l < len; ++l) - if (!(buf[l] = str[l])) - break; + memccpy(buf, str, 0, len); } static int lock_lv_name_from_args(char *vg_args, char *lock_lv_name) diff --git a/device_mapper/ioctl/libdm-iface.c b/device_mapper/ioctl/libdm-iface.c index a17a4e468..5bb4080a0 100644 --- a/device_mapper/ioctl/libdm-iface.c +++ b/device_mapper/ioctl/libdm-iface.c @@ -1256,7 +1256,7 @@ static int _lookup_dev_name(uint64_t dev, char *buf, size_t len) do { names = (struct dm_names *)((char *) names + next); if (names->dev == dev) { - strncpy(buf, names->name, len); + memccpy(buf, names->name, 0, len); r = 1; 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 */ if (DEV_NAME(dmt) && (dmt->type != DM_DEVICE_RESUME || dmt->minor < 0 || dmt->major < 0)) - /* coverity[buffer_size_warning] */ - strncpy(dmi->name, DEV_NAME(dmt), sizeof(dmi->name)); + memccpy(dmi->name, DEV_NAME(dmt), 0, sizeof(dmi->name)); if (DEV_UUID(dmt)) - /* coverity[buffer_size_warning] */ - strncpy(dmi->uuid, DEV_UUID(dmt), sizeof(dmi->uuid)); + memccpy(dmi->uuid, DEV_UUID(dmt), 0, sizeof(dmi->uuid)); if (dmt->type == DM_DEVICE_SUSPEND) dmi->flags |= DM_SUSPEND_FLAG; diff --git a/libdm/ioctl/libdm-iface.c b/libdm/ioctl/libdm-iface.c index 7e1372325..b8793e106 100644 --- a/libdm/ioctl/libdm-iface.c +++ b/libdm/ioctl/libdm-iface.c @@ -1128,7 +1128,7 @@ static int _lookup_dev_name(uint64_t dev, char *buf, size_t len) do { names = (struct dm_names *)((char *) names + next); if (names->dev == dev) { - strncpy(buf, names->name, len); + memccpy(buf, names->name, 0, len); r = 1; break; } @@ -1282,12 +1282,10 @@ static struct dm_ioctl *_flatten(struct dm_task *dmt, unsigned repeat_count) (dmt->minor < 0) || (dmt->major < 0))) /* When RESUME or RELOAD sets maj:min and dev_name, use just maj:min, * passed dev_name is useful for better error/debug messages */ - /* coverity[buffer_size_warning] */ - strncpy(dmi->name, DEV_NAME(dmt), sizeof(dmi->name)); + memccpy(dmi->name, DEV_NAME(dmt), 0, sizeof(dmi->name)); if (DEV_UUID(dmt)) - /* coverity[buffer_size_warning] */ - strncpy(dmi->uuid, DEV_UUID(dmt), sizeof(dmi->uuid)); + memccpy(dmi->uuid, DEV_UUID(dmt), 0, sizeof(dmi->uuid)); if (dmt->type == DM_DEVICE_SUSPEND) dmi->flags |= DM_SUSPEND_FLAG;