@samitouri / QOSamiQemu / commits / 66ac993bda

util/cutils: drop qemu_strnlen() in favor of strnlen()

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> Reviewed-by: Warner Losh <imp@bsdimp.com> Message-ID: <20260530062816.59206-1-guobin@linux.alibaba.com> Signed-off-by: Philippe Mathieu-Daudé <philmd@oss.qualcomm.com>

Bin Guo committed May 30, 2026 at 14:28 UTC 66ac993bdacf4d30e96db4dc82f9644913d5f7b7
5 files changed +5 -34
bsd-user/uaccess.c
+2 -2
@@ -43,7 +43,7 @@ abi_long target_strlen(abi_ulong guest_addr1)
43 {
44 uint8_t *ptr;
45 abi_ulong guest_addr;
46 - int max_len, len;
46 + size_t max_len, len;
47
48 guest_addr = guest_addr1;
49 for (;;) {
@@ -51,7 +51,7 @@ abi_long target_strlen(abi_ulong guest_addr1)
51 ptr = lock_user(VERIFY_READ, guest_addr, max_len, 1);
52 if (!ptr)
53 return -TARGET_EFAULT;
54 - len = qemu_strnlen((const char *)ptr, max_len);
54 + len = strnlen((const char *)ptr, max_len);
55 unlock_user(ptr, guest_addr, 0);
56 guest_addr += len;
57 /* we don't allow wrapping or integer overflow */
docs/devel/style.rst
-1
@@ -519,7 +519,6 @@ QEMU provides other useful string functions:
519
520 int strstart(const char *str, const char *val, const char **ptr)
521 int stristart(const char *str, const char *val, const char **ptr)
522 - int qemu_strnlen(const char *s, int max_len)
522
523 There are also replacement character processing macros for isxyz and toxyz,
524 so instead of e.g. isalnum you should use qemu_isalnum.
include/qemu/cutils.h
+1 -16
@@ -101,22 +101,7 @@ int strstart(const char *str, const char *val, const char **ptr);
101 * false otherwise.
102 */
103 int stristart(const char *str, const char *val, const char **ptr);
104 -/**
105 - * qemu_strnlen:
106 - * @s: string
107 - * @max_len: maximum number of bytes in @s to scan
108 - *
109 - * Return the length of the string @s, like strlen(), but do not
110 - * examine more than @max_len bytes of the memory pointed to by @s.
111 - * If no NUL terminator is found within @max_len bytes, then return
112 - * @max_len instead.
113 - *
114 - * This function has the same behaviour as the POSIX strnlen()
115 - * function.
116 - *
117 - * Returns: length of @s in bytes, or @max_len, whichever is smaller.
118 - */
119 -int qemu_strnlen(const char *s, int max_len);
104 +
105 /**
106 * qemu_strsep:
107 * @input: pointer to string to parse
linux-user/uaccess.c
+1 -1
@@ -99,7 +99,7 @@ ssize_t target_strlen(abi_ulong guest_addr1)
99 ptr = lock_user(VERIFY_READ, guest_addr, max_len, 1);
100 if (!ptr)
101 return -TARGET_EFAULT;
102 - len = qemu_strnlen((const char *)ptr, max_len);
102 + len = strnlen((const char *)ptr, max_len);
103 unlock_user(ptr, guest_addr, 0);
104 guest_addr += len;
105 /* we don't allow wrapping or integer overflow */
util/cutils.c
+1 -14
@@ -54,7 +54,7 @@
54
55 void strpadcpy(char *buf, int buf_size, const char *str, char pad)
56 {
57 - int len = qemu_strnlen(str, buf_size);
57 + size_t len = strnlen(str, buf_size);
58 memcpy(buf, str, len);
59 memset(buf + len, pad, buf_size - len);
60 }
@@ -118,19 +118,6 @@ int stristart(const char *str, const char *val, const char **ptr)
118 return 1;
119 }
120
121 -/* XXX: use host strnlen if available ? */
122 -int qemu_strnlen(const char *s, int max_len)
123 -{
124 - int i;
125 -
126 - for(i = 0; i < max_len; i++) {
127 - if (s[i] == '\0') {
128 - break;
129 - }
130 - }
131 - return i;
132 -}
133 -
121 char *qemu_strsep(char **input, const char *delim)
122 {
123 char *result = *input;