util/cutils: drop qemu_strnlen() in favor of strnlen()
PATCH v2 1/1 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.
--- 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(-)
bsd-user/uaccess.c +2 -2
@@ -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 */
docs/devel/style.rst +0 -1
@@ -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.
include/qemu/cutils.h +1 -16
@@ -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
linux-user/uaccess.c +1 -1
@@ -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 */
util/cutils.c +1 -14
@@ -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;
Signature · 1 line
2.50.1 (Apple Git-155)
Quoted 4 lines · Bin Guo
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.
Quoted 8 lines
While here, align
bsd-user/uaccess.cto usesize_tfor max_len/len, matchinglinux-user/uaccess.cand eliminating a signed/unsigned mismatch.Also remove the stale
qemu_strnlen()entry fromdocs/devel/style.rst.Suggested-by Philippe Mathieu-Daudé <[email protected]>Reviewed-by Philippe Mathieu-Daudé <[email protected]>Signed-off-by Bin Guo <[email protected]>
Signature · 2 lines
Alex Bennée Virtualisation Tech Lead @ Linaro
Quoted 12 lines · Bin Guo
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.cto usesize_tfor max_len/len, matchinglinux-user/uaccess.cand eliminating a signed/unsigned mismatch.Also remove the stale
qemu_strnlen()entry fromdocs/devel/style.rst.Suggested-by Philippe Mathieu-Daudé <[email protected]>Reviewed-by Philippe Mathieu-Daudé <[email protected]>Signed-off-by Bin Guo <[email protected]>
Quoted 8 lines
--- 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(-)
Normally not a fan of while I'm here fixes, but this one is correct and too small to split out.
Warner
bsd-user/uaccess.c +0 -0
Quoted 114 lines
index
89163257f4..7ad4b580f4100644bsd-user/uaccess.c +2
-2@@ -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 */ docs/devel/style.rst +0
-1@@ -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.include/qemu/cutils.h +1
-16@@ -101,22 +101,7 @@ int strstart(const char *str, const char *val, constchar **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
linux-user/uaccess.c +1
-1@@ -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 */ util/cutils.c +1
-14@@ -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)