diff mbox series

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

Message ID 20241022063402.184213-1-thuth@redhat.com
State New
Headers show
Series [v2] migration/dirtyrate: Silence warning about strcpy() on OpenBSD | expand

Commit Message

Thomas Huth Oct. 22, 2024, 6:34 a.m. UTC
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(-)

Comments

Yong Huang Oct. 22, 2024, 6:43 a.m. UTC | #1
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>
Peter Xu Oct. 22, 2024, 3:59 p.m. UTC | #2
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.
diff mbox series

Patch

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)