qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] migration/dirtyrate: Silence warning about strcpy() on OpenBSD
@ 2024-10-22  6:34 Thomas Huth
  2024-10-22  6:43 ` Yong Huang
  2024-10-22 15:59 ` Peter Xu
  0 siblings, 2 replies; 3+ messages in thread
From: Thomas Huth @ 2024-10-22  6:34 UTC (permalink / raw)
  To: Hyman Huang, Peter Xu, Fabiano Rosas; +Cc: qemu-devel

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



^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH v2] migration/dirtyrate: Silence warning about strcpy() on OpenBSD
  2024-10-22  6:34 [PATCH v2] migration/dirtyrate: Silence warning about strcpy() on OpenBSD Thomas Huth
@ 2024-10-22  6:43 ` Yong Huang
  2024-10-22 15:59 ` Peter Xu
  1 sibling, 0 replies; 3+ messages in thread
From: Yong Huang @ 2024-10-22  6:43 UTC (permalink / raw)
  To: Thomas Huth; +Cc: Peter Xu, Fabiano Rosas, qemu-devel

[-- Attachment #1: Type: text/plain, Size: 2000 bytes --]

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

[-- Attachment #2: Type: text/html, Size: 2928 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH v2] migration/dirtyrate: Silence warning about strcpy() on OpenBSD
  2024-10-22  6:34 [PATCH v2] migration/dirtyrate: Silence warning about strcpy() on OpenBSD Thomas Huth
  2024-10-22  6:43 ` Yong Huang
@ 2024-10-22 15:59 ` Peter Xu
  1 sibling, 0 replies; 3+ messages in thread
From: Peter Xu @ 2024-10-22 15:59 UTC (permalink / raw)
  To: Thomas Huth; +Cc: Hyman Huang, Fabiano Rosas, qemu-devel

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



^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2024-10-22 16:01 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-10-22  6:34 [PATCH v2] migration/dirtyrate: Silence warning about strcpy() on OpenBSD Thomas Huth
2024-10-22  6:43 ` Yong Huang
2024-10-22 15:59 ` Peter Xu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).