Skip to contentSkip to conversation

migration/dirtyrate: Silence warning about strcpy() on OpenBSD

PATCH Applied Thomas Huth 3 participants 3 messages Oct 22, 2024

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 migration/dirtyrate: Silence warning about strcpy() on OpenBSD

The linker on OpenBSD complains:

ld: warning: dirtyrate.c:447 (../src/migration/dirtyrate.c:447)(...):
warning: strcpy() is almost always misused, please use strlcpy()

It's currently not a real problem in this case since both arrays have the same size (256 bytes). But just in case somebody changes the size of the source array in the future, let's better play safe and use g_strlcpy() here instead, with an additional check that the string has been copied as a whole.

---
v2: Check the return value of g_strlcpy to avoid truncation of the string

 migration/dirtyrate.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
migration/dirtyrate.c +3 -1
index 233acb0855..0f941024be 100644
@@ -436,6 +436,7 @@ static void get_ramblock_dirty_info(RAMBlock *block,
                                     struct DirtyRateConfig *config)
 {
     uint64_t sample_pages_per_gigabytes = config->sample_pages_per_gigabytes;
+    gsize len;
 
     /* Right shift 30 bits to calc ramblock size in GB */
     info->sample_pages_count = (qemu_ram_get_used_length(block) *
@@ -444,7 +445,8 @@ static void get_ramblock_dirty_info(RAMBlock *block,
     info->ramblock_pages = qemu_ram_get_used_length(block) >>
                            qemu_target_page_bits();
     info->ramblock_addr = qemu_ram_get_host_addr(block);
-    strcpy(info->idstr, qemu_ram_get_idstr(block));
+    len = g_strlcpy(info->idstr, qemu_ram_get_idstr(block), sizeof(info->idstr));
+    g_assert(len < sizeof(info->idstr));
 }
 
 static void free_ramblock_dirty_info(struct RamblockDirtyInfo *infos, int count)
Signature · 1 line
2.47.0
Reviewed-by on v2 1/1
#1 lore raw
Quoted 41 lines · Thomas Huth

The linker on OpenBSD complains:

ld: warning: dirtyrate.c:447 (../src/migration/dirtyrate.c:447)(...):
warning: strcpy() is almost always misused, please use strlcpy()

It's currently not a real problem in this case since both arrays have the same size (256 bytes). But just in case somebody changes the size of the source array in the future, let's better play safe and use g_strlcpy() here instead, with an additional check that the string has been copied as a whole.

---
v2: Check the return value of g_strlcpy to avoid truncation of the string

 migration/dirtyrate.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
migration/dirtyrate.c +1 -0
index 233acb0855..0f941024be 100644
@@ -436,6 +436,7 @@ static void get_ramblock_dirty_info(RAMBlock *block,
                                     struct DirtyRateConfig *config)
 {
     uint64_t sample_pages_per_gigabytes =
 config->sample_pages_per_gigabytes;
+    gsize len;
 
     /* Right shift 30 bits to calc ramblock size in GB */
     info->sample_pages_count = (qemu_ram_get_used_length(block) *
migration/dirtyrate.c +2 -1
@@ -444,7 +445,8 @@ static void get_ramblock_dirty_info(RAMBlock *block,
     info->ramblock_pages = qemu_ram_get_used_length(block) >>
                            qemu_target_page_bits();
     info->ramblock_addr = qemu_ram_get_host_addr(block);
-    strcpy(info->idstr, qemu_ram_get_idstr(block));
+    len = g_strlcpy(info->idstr, qemu_ram_get_idstr(block),
 sizeof(info->idstr));
+    g_assert(len < sizeof(info->idstr));
 }
 
Quoted 6 lines

static void free_ramblock_dirty_info(struct RamblockDirtyInfo *infos, int
count)
--
2.47.0

Signature · 1 line
Best regards
Applied
#2 lore raw
In reply to Thomas Huth
Quoted 11 lines · Thomas Huth

The linker on OpenBSD complains:

ld: warning: dirtyrate.c:447 (../src/migration/dirtyrate.c:447)(...):
warning: strcpy() is almost always misused, please use strlcpy()

It's currently not a real problem in this case since both arrays have the same size (256 bytes). But just in case somebody changes the size of the source array in the future, let's better play safe and use g_strlcpy() here instead, with an additional check that the string has been copied as a whole.

Quoted 3 lines

---
v2: Check the return value of g_strlcpy to avoid truncation of the string

queued, thanks.

Signature · 1 line
Peter Xu