migration/dirtyrate: Silence warning about strcpy() on OpenBSD
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
@@ -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
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 usestrlcpy()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.Signed-off-by Thomas Huth <[email protected]>---
v2: Check the return value ofg_strlcpyto avoid truncation of the stringmigration/dirtyrate.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)migration/dirtyrate.c +1
-0@@ -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
Peter Xu
AppliedQuoted 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 usestrlcpy()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
Signed-off-by Thomas Huth <[email protected]>---
v2: Check the return value ofg_strlcpyto avoid truncation of the string
queued, thanks.
Signature · 1 line
Peter Xu