From 24131dee30bd7eedb4d99bd13bbb9792f7361937 Mon Sep 17 00:00:00 2001 From: Aurelien DARRAGON Date: Fri, 25 Oct 2024 17:04:37 +0200 Subject: [PATCH] MINOR: tools: add strnlen2() helper strnlen2() is functionally equivalent to strnlen(). Goal is to provide an alternative to strnlen() which is not portable since it requires _POSIX_C_SOURCE >= 200809L --- include/haproxy/tools.h | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/include/haproxy/tools.h b/include/haproxy/tools.h index 8d1afb5d7c..0bade0fd19 100644 --- a/include/haproxy/tools.h +++ b/include/haproxy/tools.h @@ -82,6 +82,21 @@ */ extern int strlcpy2(char *dst, const char *src, int size); +/* + * portable equivalent to POSIX strnlen(): + * returns the number of bytes in the string pointed to by , excluding + * the terminating null byte, but at most . The function does not + * look at characters passed . + */ +static inline size_t strnlen2(const char *s, size_t maxlen) +{ + size_t len; + + for (len = 0; len < maxlen && s[len]; len++) + ; + return len; +} + /* * This function simply returns a locally allocated string containing * the ascii representation for number 'n' in decimal.