From 0dcdc0347c8d0405c1c6b444d23483dd9bf31d34 Mon Sep 17 00:00:00 2001 From: Yu-Ping Wu Date: Thu, 8 Aug 2024 17:20:05 +0800 Subject: commonlib/bsd: Add strlen() and strnlen() functions Add strlen() and strnlen() to commonlib/bsd by rewriting them from scratch, and remove the same functions from coreboot and libpayload. Note that in the existing libpayload implementation, these functions return 0 for NULL strings. Given that POSIX doesn't require the NULL check and that other major libc implementations (e.g. glibc [1]) don't seem to do that, the new functions also don't perform the NULL check. [1] https://github.com/bminor/glibc/blob/master/sysdeps/i386/strlen.c Change-Id: I1203ec9affabe493bd14b46662d212b08240cced Signed-off-by: Yu-Ping Wu Reviewed-on: https://review.coreboot.org/c/coreboot/+/83830 Reviewed-by: Maximilian Brune Tested-by: build bot (Jenkins) Reviewed-by: Julius Werner --- payloads/libpayload/libc/string.c | 44 --------------------------------------- 1 file changed, 44 deletions(-) (limited to 'payloads/libpayload/libc') diff --git a/payloads/libpayload/libc/string.c b/payloads/libpayload/libc/string.c index b3e1c8df0d..f9805e39bf 100644 --- a/payloads/libpayload/libc/string.c +++ b/payloads/libpayload/libc/string.c @@ -35,50 +35,6 @@ #include #include -/** - * Calculate the length of a fixed-size string. - * - * @param str The input string. - * @param maxlen Return at most maxlen characters as length of the string. - * @return The length of the string, not including the final NUL character. - * The maximum length returned is maxlen. - */ -size_t strnlen(const char *str, size_t maxlen) -{ - size_t len = 0; - - /* NULL and empty strings have length 0. */ - if (!str) - return 0; - - /* Loop until we find a NUL character, or maxlen is reached. */ - while ((*str++ != '\0') && (len < maxlen)) - len++; - - return len; -} - -/** - * Calculate the length of a string. - * - * @param str The input string. - * @return The length of the string, not including the final NUL character. - */ -size_t strlen(const char *str) -{ - size_t len = 0; - - /* NULL and empty strings have length 0. */ - if (!str) - return 0; - - /* Loop until we find a NUL character. */ - while (*str++ != '\0') - len++; - - return len; -} - /** * Compare two strings. * -- cgit v1.2.3