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 /device_mapper/ioctl | |
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 'device_mapper/ioctl')
-rw-r--r-- | device_mapper/ioctl/libdm-iface.c | 41 |
1 files changed, 25 insertions, 16 deletions
diff --git a/device_mapper/ioctl/libdm-iface.c b/device_mapper/ioctl/libdm-iface.c index ffcbc8a48..0cc81b9d9 100644 --- a/device_mapper/ioctl/libdm-iface.c +++ b/device_mapper/ioctl/libdm-iface.c @@ -843,8 +843,8 @@ int dm_task_get_device_list(struct dm_task *dmt, struct dm_list **devs_list, dm_dev->event_nr = 0; dm_dev->uuid = NULL; - strcpy(dm_dev->name, names->name); len = strlen(names->name) + 1; + memcpy(dm_dev->name, names->name, len); dm_new_dev = _align_ptr((char*)(dm_dev + 1) + len); if (_check_has_event_nr()) { @@ -862,8 +862,9 @@ int dm_task_get_device_list(struct dm_task *dmt, struct dm_list **devs_list, *devs_features |= DM_DEVICE_LIST_HAS_UUID; uuid_ptr = _align_ptr(event_nr + 2); dm_dev->uuid = (char*) dm_new_dev; - dm_new_dev = _align_ptr((char*)dm_new_dev + strlen(uuid_ptr) + 1); - strcpy(dm_dev->uuid, uuid_ptr); + len = strlen(uuid_ptr) + 1; + dm_new_dev = _align_ptr((char*)dm_new_dev + len); + memcpy(dm_dev->uuid, uuid_ptr, len); if (!dm_hash_insert(devs->uuids, dm_dev->uuid, dm_dev)) return_0; // FIXME #if 0 @@ -1201,9 +1202,10 @@ static char *_add_target(struct target *t, char *out, char *end) while (*pt) if (*pt++ == '\\') backslash_count++; - len = strlen(t->params) + backslash_count; - if ((out >= end) || (out + len + 1) >= end) { + len = strlen(t->params) + 1; + + if ((out >= end) || (out + len + backslash_count) >= end) { log_error("Ran out of memory building ioctl parameter"); return NULL; } @@ -1219,8 +1221,8 @@ static char *_add_target(struct target *t, char *out, char *end) *out++ = '\0'; } else { - strcpy(out, t->params); - out += len + 1; + memcpy(out, t->params, len); + out += len + backslash_count; } /* align next block */ @@ -1291,6 +1293,7 @@ static struct dm_ioctl *_flatten(struct dm_task *dmt, unsigned repeat_count) struct target *t; struct dm_target_msg *tmsg; size_t len = sizeof(struct dm_ioctl); + size_t message_len = 0, newname_len = 0, geometry_len = 0; char *b, *e; int count = 0; @@ -1351,14 +1354,20 @@ static struct dm_ioctl *_flatten(struct dm_task *dmt, unsigned repeat_count) return NULL; } - if (dmt->newname) - len += strlen(dmt->newname) + 1; + if (dmt->newname) { + newname_len = strlen(dmt->newname) + 1; + len += newname_len; + } - if (dmt->message) - len += sizeof(struct dm_target_msg) + strlen(dmt->message) + 1; + if (dmt->message) { + message_len = strlen(dmt->message) + 1; + len += sizeof(struct dm_target_msg) + message_len; + } - if (dmt->geometry) - len += strlen(dmt->geometry) + 1; + if (dmt->geometry) { + geometry_len = strlen(dmt->geometry) + 1; + len += geometry_len; + } /* * Give len a minimum size so that we have space to store @@ -1480,16 +1489,16 @@ static struct dm_ioctl *_flatten(struct dm_task *dmt, unsigned repeat_count) goto_bad; if (dmt->newname) - strcpy(b, dmt->newname); + memcpy(b, dmt->newname, newname_len); if (dmt->message) { tmsg = (struct dm_target_msg *) b; tmsg->sector = dmt->sector; - strcpy(tmsg->message, dmt->message); + memcpy(tmsg->message, dmt->message, message_len); } if (dmt->geometry) - strcpy(b, dmt->geometry); + memcpy(b, dmt->geometry, geometry_len); return dmi; |