All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] util/cutils: drop qemu_strnlen() in favor of strnlen()
@ 2026-05-30  6:28 Bin Guo
  2026-05-30 13:29 ` Alex Bennée
  2026-05-30 13:40 ` Warner Losh
  0 siblings, 2 replies; 4+ messages in thread
From: Bin Guo @ 2026-05-30  6:28 UTC (permalink / raw)
  To: qemu-devel
  Cc: Warner Losh, Kyle Evans, Laurent Vivier,
	Philippe Mathieu-Daudé, Alex Bennée,
	Daniel P . Berrangé, Thomas Huth, Markus Armbruster

There are only three call sites, and strnlen() is available on all
supported platforms (POSIX.1-2008, Windows via UCRT, MinGW).  Remove
the hand-rolled wrapper and use the standard function directly.

While here, align bsd-user/uaccess.c to use size_t for max_len/len,
matching linux-user/uaccess.c and eliminating a signed/unsigned mismatch.

Also remove the stale qemu_strnlen() entry from docs/devel/style.rst.

Suggested-by: Philippe Mathieu-Daudé <philmd@linaro.org>
Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org>
Signed-off-by: Bin Guo <guobin@linux.alibaba.com>
---
 bsd-user/uaccess.c    |  4 ++--
 docs/devel/style.rst  |  1 -
 include/qemu/cutils.h | 17 +----------------
 linux-user/uaccess.c  |  2 +-
 util/cutils.c         | 15 +--------------
 5 files changed, 5 insertions(+), 34 deletions(-)

diff --git a/bsd-user/uaccess.c b/bsd-user/uaccess.c
index 89163257f4..7ad4b580f4 100644
--- a/bsd-user/uaccess.c
+++ b/bsd-user/uaccess.c
@@ -43,7 +43,7 @@ abi_long target_strlen(abi_ulong guest_addr1)
 {
     uint8_t *ptr;
     abi_ulong guest_addr;
-    int max_len, len;
+    size_t max_len, len;
 
     guest_addr = guest_addr1;
     for (;;) {
@@ -51,7 +51,7 @@ abi_long target_strlen(abi_ulong guest_addr1)
         ptr = lock_user(VERIFY_READ, guest_addr, max_len, 1);
         if (!ptr)
             return -TARGET_EFAULT;
-        len = qemu_strnlen((const char *)ptr, max_len);
+        len = strnlen((const char *)ptr, max_len);
         unlock_user(ptr, guest_addr, 0);
         guest_addr += len;
         /* we don't allow wrapping or integer overflow */
diff --git a/docs/devel/style.rst b/docs/devel/style.rst
index d025933808..0b854ae042 100644
--- a/docs/devel/style.rst
+++ b/docs/devel/style.rst
@@ -519,7 +519,6 @@ QEMU provides other useful string functions:
 
     int strstart(const char *str, const char *val, const char **ptr)
     int stristart(const char *str, const char *val, const char **ptr)
-    int qemu_strnlen(const char *s, int max_len)
 
 There are also replacement character processing macros for isxyz and toxyz,
 so instead of e.g. isalnum you should use qemu_isalnum.
diff --git a/include/qemu/cutils.h b/include/qemu/cutils.h
index 36c68ce86c..d249f22676 100644
--- a/include/qemu/cutils.h
+++ b/include/qemu/cutils.h
@@ -101,22 +101,7 @@ int strstart(const char *str, const char *val, const char **ptr);
  *          false otherwise.
  */
 int stristart(const char *str, const char *val, const char **ptr);
-/**
- * qemu_strnlen:
- * @s: string
- * @max_len: maximum number of bytes in @s to scan
- *
- * Return the length of the string @s, like strlen(), but do not
- * examine more than @max_len bytes of the memory pointed to by @s.
- * If no NUL terminator is found within @max_len bytes, then return
- * @max_len instead.
- *
- * This function has the same behaviour as the POSIX strnlen()
- * function.
- *
- * Returns: length of @s in bytes, or @max_len, whichever is smaller.
- */
-int qemu_strnlen(const char *s, int max_len);
+
 /**
  * qemu_strsep:
  * @input: pointer to string to parse
diff --git a/linux-user/uaccess.c b/linux-user/uaccess.c
index 27e841e651..f9bd713edd 100644
--- a/linux-user/uaccess.c
+++ b/linux-user/uaccess.c
@@ -99,7 +99,7 @@ ssize_t target_strlen(abi_ulong guest_addr1)
         ptr = lock_user(VERIFY_READ, guest_addr, max_len, 1);
         if (!ptr)
             return -TARGET_EFAULT;
-        len = qemu_strnlen((const char *)ptr, max_len);
+        len = strnlen((const char *)ptr, max_len);
         unlock_user(ptr, guest_addr, 0);
         guest_addr += len;
         /* we don't allow wrapping or integer overflow */
diff --git a/util/cutils.c b/util/cutils.c
index 9803f11a59..f39a97e509 100644
--- a/util/cutils.c
+++ b/util/cutils.c
@@ -54,7 +54,7 @@
 
 void strpadcpy(char *buf, int buf_size, const char *str, char pad)
 {
-    int len = qemu_strnlen(str, buf_size);
+    size_t len = strnlen(str, buf_size);
     memcpy(buf, str, len);
     memset(buf + len, pad, buf_size - len);
 }
@@ -118,19 +118,6 @@ int stristart(const char *str, const char *val, const char **ptr)
     return 1;
 }
 
-/* XXX: use host strnlen if available ? */
-int qemu_strnlen(const char *s, int max_len)
-{
-    int i;
-
-    for(i = 0; i < max_len; i++) {
-        if (s[i] == '\0') {
-            break;
-        }
-    }
-    return i;
-}
-
 char *qemu_strsep(char **input, const char *delim)
 {
     char *result = *input;
-- 
2.50.1 (Apple Git-155)



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

* Re: [PATCH v2] util/cutils: drop qemu_strnlen() in favor of strnlen()
  2026-05-29  8:39 [PATCH] " Philippe Mathieu-Daudé
@ 2026-05-30  6:37 ` Bin Guo
  0 siblings, 0 replies; 4+ messages in thread
From: Bin Guo @ 2026-05-30  6:37 UTC (permalink / raw)
  To: Philippe Mathieu-Daudé; +Cc: qemu-devel

On Fri, 29 May 2026 08:02:41 +0200, Philippe Mathieu-Daudé wrote:
> Using size_t here and removing the line in docs/devel/style.rst:
> Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org>
>
> Thanks!

Both suggestions have been applied in v2:

https://lore.kernel.org/qemu-devel/20260530062816.59206-1-guobin@linux.alibaba.com/


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

* Re: [PATCH v2] util/cutils: drop qemu_strnlen() in favor of strnlen()
  2026-05-30  6:28 [PATCH v2] util/cutils: drop qemu_strnlen() in favor of strnlen() Bin Guo
@ 2026-05-30 13:29 ` Alex Bennée
  2026-05-30 13:40 ` Warner Losh
  1 sibling, 0 replies; 4+ messages in thread
From: Alex Bennée @ 2026-05-30 13:29 UTC (permalink / raw)
  To: Bin Guo
  Cc: qemu-devel, Warner Losh, Kyle Evans, Laurent Vivier,
	Philippe Mathieu-Daudé, Daniel P . Berrangé,
	Thomas Huth, Markus Armbruster

Bin Guo <guobin@linux.alibaba.com> writes:

> There are only three call sites, and strnlen() is available on all
> supported platforms (POSIX.1-2008, Windows via UCRT, MinGW).  Remove
> the hand-rolled wrapper and use the standard function directly.
>
> While here, align bsd-user/uaccess.c to use size_t for max_len/len,
> matching linux-user/uaccess.c and eliminating a signed/unsigned mismatch.
>
> Also remove the stale qemu_strnlen() entry from docs/devel/style.rst.
>
> Suggested-by: Philippe Mathieu-Daudé <philmd@linaro.org>
> Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org>
> Signed-off-by: Bin Guo <guobin@linux.alibaba.com>

Reviewed-by: Alex Bennée <alex.bennee@linaro.org>

-- 
Alex Bennée
Virtualisation Tech Lead @ Linaro


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

* Re: [PATCH v2] util/cutils: drop qemu_strnlen() in favor of strnlen()
  2026-05-30  6:28 [PATCH v2] util/cutils: drop qemu_strnlen() in favor of strnlen() Bin Guo
  2026-05-30 13:29 ` Alex Bennée
@ 2026-05-30 13:40 ` Warner Losh
  1 sibling, 0 replies; 4+ messages in thread
From: Warner Losh @ 2026-05-30 13:40 UTC (permalink / raw)
  To: Bin Guo
  Cc: QEMU Developers, Kyle Evans, Laurent Vivier,
	Philippe Mathieu-Daudé, Alex Bennée,
	Daniel P . Berrangé, Thomas Huth, Markus Armbruster

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

On Sat, May 30, 2026, 12:28 AM Bin Guo <guobin@linux.alibaba.com> wrote:

> There are only three call sites, and strnlen() is available on all
> supported platforms (POSIX.1-2008, Windows via UCRT, MinGW).  Remove
> the hand-rolled wrapper and use the standard function directly.
>
> While here, align bsd-user/uaccess.c to use size_t for max_len/len,
> matching linux-user/uaccess.c and eliminating a signed/unsigned mismatch.
>
> Also remove the stale qemu_strnlen() entry from docs/devel/style.rst.
>
> Suggested-by: Philippe Mathieu-Daudé <philmd@linaro.org>
> Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org>
> Signed-off-by: Bin Guo <guobin@linux.alibaba.com>
> ---
>  bsd-user/uaccess.c    |  4 ++--
>  docs/devel/style.rst  |  1 -
>  include/qemu/cutils.h | 17 +----------------
>  linux-user/uaccess.c  |  2 +-
>  util/cutils.c         | 15 +--------------
>  5 files changed, 5 insertions(+), 34 deletions(-)
>

Reviewed-by: Warner Losh <imp@bsdimp.com>

Normally not a fan of while I'm here fixes, but this one is correct and too
small to split out.

Warner

diff --git a/bsd-user/uaccess.c b/bsd-user/uaccess.c
> index 89163257f4..7ad4b580f4 100644
> --- a/bsd-user/uaccess.c
> +++ b/bsd-user/uaccess.c
> @@ -43,7 +43,7 @@ abi_long target_strlen(abi_ulong guest_addr1)
>  {
>      uint8_t *ptr;
>      abi_ulong guest_addr;
> -    int max_len, len;
> +    size_t max_len, len;
>
>      guest_addr = guest_addr1;
>      for (;;) {
> @@ -51,7 +51,7 @@ abi_long target_strlen(abi_ulong guest_addr1)
>          ptr = lock_user(VERIFY_READ, guest_addr, max_len, 1);
>          if (!ptr)
>              return -TARGET_EFAULT;
> -        len = qemu_strnlen((const char *)ptr, max_len);
> +        len = strnlen((const char *)ptr, max_len);
>          unlock_user(ptr, guest_addr, 0);
>          guest_addr += len;
>          /* we don't allow wrapping or integer overflow */
> diff --git a/docs/devel/style.rst b/docs/devel/style.rst
> index d025933808..0b854ae042 100644
> --- a/docs/devel/style.rst
> +++ b/docs/devel/style.rst
> @@ -519,7 +519,6 @@ QEMU provides other useful string functions:
>
>      int strstart(const char *str, const char *val, const char **ptr)
>      int stristart(const char *str, const char *val, const char **ptr)
> -    int qemu_strnlen(const char *s, int max_len)
>
>  There are also replacement character processing macros for isxyz and
> toxyz,
>  so instead of e.g. isalnum you should use qemu_isalnum.
> diff --git a/include/qemu/cutils.h b/include/qemu/cutils.h
> index 36c68ce86c..d249f22676 100644
> --- a/include/qemu/cutils.h
> +++ b/include/qemu/cutils.h
> @@ -101,22 +101,7 @@ int strstart(const char *str, const char *val, const
> char **ptr);
>   *          false otherwise.
>   */
>  int stristart(const char *str, const char *val, const char **ptr);
> -/**
> - * qemu_strnlen:
> - * @s: string
> - * @max_len: maximum number of bytes in @s to scan
> - *
> - * Return the length of the string @s, like strlen(), but do not
> - * examine more than @max_len bytes of the memory pointed to by @s.
> - * If no NUL terminator is found within @max_len bytes, then return
> - * @max_len instead.
> - *
> - * This function has the same behaviour as the POSIX strnlen()
> - * function.
> - *
> - * Returns: length of @s in bytes, or @max_len, whichever is smaller.
> - */
> -int qemu_strnlen(const char *s, int max_len);
> +
>  /**
>   * qemu_strsep:
>   * @input: pointer to string to parse
> diff --git a/linux-user/uaccess.c b/linux-user/uaccess.c
> index 27e841e651..f9bd713edd 100644
> --- a/linux-user/uaccess.c
> +++ b/linux-user/uaccess.c
> @@ -99,7 +99,7 @@ ssize_t target_strlen(abi_ulong guest_addr1)
>          ptr = lock_user(VERIFY_READ, guest_addr, max_len, 1);
>          if (!ptr)
>              return -TARGET_EFAULT;
> -        len = qemu_strnlen((const char *)ptr, max_len);
> +        len = strnlen((const char *)ptr, max_len);
>          unlock_user(ptr, guest_addr, 0);
>          guest_addr += len;
>          /* we don't allow wrapping or integer overflow */
> diff --git a/util/cutils.c b/util/cutils.c
> index 9803f11a59..f39a97e509 100644
> --- a/util/cutils.c
> +++ b/util/cutils.c
> @@ -54,7 +54,7 @@
>
>  void strpadcpy(char *buf, int buf_size, const char *str, char pad)
>  {
> -    int len = qemu_strnlen(str, buf_size);
> +    size_t len = strnlen(str, buf_size);
>      memcpy(buf, str, len);
>      memset(buf + len, pad, buf_size - len);
>  }
> @@ -118,19 +118,6 @@ int stristart(const char *str, const char *val, const
> char **ptr)
>      return 1;
>  }
>
> -/* XXX: use host strnlen if available ? */
> -int qemu_strnlen(const char *s, int max_len)
> -{
> -    int i;
> -
> -    for(i = 0; i < max_len; i++) {
> -        if (s[i] == '\0') {
> -            break;
> -        }
> -    }
> -    return i;
> -}
> -
>  char *qemu_strsep(char **input, const char *delim)
>  {
>      char *result = *input;
> --
> 2.50.1 (Apple Git-155)
>
>

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

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

end of thread, other threads:[~2026-05-30 13:41 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-05-30  6:28 [PATCH v2] util/cutils: drop qemu_strnlen() in favor of strnlen() Bin Guo
2026-05-30 13:29 ` Alex Bennée
2026-05-30 13:40 ` Warner Losh
  -- strict thread matches above, loose matches on Subject: below --
2026-05-29  8:39 [PATCH] " Philippe Mathieu-Daudé
2026-05-30  6:37 ` [PATCH v2] " Bin Guo

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.