security: replace strcpy with memcpy using known lengths in pw-dump

Memory Safety: Low

The strcpy() calls here operate on buffers that are correctly sized,
but using strcpy obscures the bounds guarantee and forces redundant
strlen() calls to compute pointer offsets. Replace with memcpy()
using the lengths already computed for the allocation, making the
bounds safety explicit and avoiding repeated string scanning.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Wim Taymans 2026-04-27 16:13:52 +02:00
parent d456be1943
commit 1ebbd9d7bc

View file

@ -1097,9 +1097,10 @@ static int metadata_property(void *data,
free(e); free(e);
} }
if (key != NULL && value != NULL) { if (key != NULL && value != NULL) {
size_t size = strlen(key) + 1; size_t key_len = strlen(key) + 1;
size += strlen(value) + 1; size_t value_len = strlen(value) + 1;
size += type ? strlen(type) + 1 : 0; size_t type_len = type ? strlen(type) + 1 : 0;
size_t size = key_len + value_len + type_len;
e = calloc(1, sizeof(*e) + size); e = calloc(1, sizeof(*e) + size);
if (e == NULL) if (e == NULL)
@ -1107,12 +1108,12 @@ static int metadata_property(void *data,
e->subject = subject; e->subject = subject;
e->key = SPA_PTROFF(e, sizeof(*e), void); e->key = SPA_PTROFF(e, sizeof(*e), void);
strcpy(e->key, key); memcpy(e->key, key, key_len);
e->value = SPA_PTROFF(e->key, strlen(e->key) + 1, void); e->value = SPA_PTROFF(e->key, key_len, void);
strcpy(e->value, value); memcpy(e->value, value, value_len);
if (type) { if (type) {
e->type = SPA_PTROFF(e->value, strlen(e->value) + 1, void); e->type = SPA_PTROFF(e->value, value_len, void);
strcpy(e->type, type); memcpy(e->type, type, type_len);
} else { } else {
e->type = NULL; e->type = NULL;
} }