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

Thomas Huth posted 1 patch 14 hours ago
migration/dirtyrate.c | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
[PATCH v2] migration/dirtyrate: Silence warning about strcpy() on OpenBSD
Posted by Thomas Huth 14 hours ago
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.

Signed-off-by: Thomas Huth <thuth@redhat.com>
---
 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(-)

diff --git a/migration/dirtyrate.c b/migration/dirtyrate.c
index 233acb0855..0f941024be 100644
--- a/migration/dirtyrate.c
+++ b/migration/dirtyrate.c
@@ -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)
-- 
2.47.0
Re: [PATCH v2] migration/dirtyrate: Silence warning about strcpy() on OpenBSD
Posted by Peter Xu 4 hours ago
On Tue, Oct 22, 2024 at 08:34:02AM +0200, Thomas Huth wrote:
> 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.
> 
> Signed-off-by: Thomas Huth <thuth@redhat.com>
> ---
>  v2: Check the return value of g_strlcpy to avoid truncation of the string

queued, thanks.

-- 
Peter Xu
Re: [PATCH v2] migration/dirtyrate: Silence warning about strcpy() on OpenBSD
Posted by Yong Huang 13 hours ago
On Tue, Oct 22, 2024 at 2:34 PM Thomas Huth <thuth@redhat.com> wrote:

> 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.
>
> Signed-off-by: Thomas Huth <thuth@redhat.com>
> ---
>  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(-)
>
> diff --git a/migration/dirtyrate.c b/migration/dirtyrate.c
> index 233acb0855..0f941024be 100644
> --- a/migration/dirtyrate.c
> +++ b/migration/dirtyrate.c
> @@ -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)
> --
> 2.47.0
>
>
Reviewed-by: Hyman Huang <yong.huang@smartx.com>

-- 
Best regards