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 <yupingso@chromium.org> Reviewed-on: https://review.coreboot.org/c/coreboot/+/83830 Reviewed-by: Maximilian Brune <maximilian.brune@9elements.com> Tested-by: build bot (Jenkins) <no-reply@coreboot.org> Reviewed-by: Julius Werner <jwerner@chromium.org>
This commit is contained in:
@@ -3,8 +3,12 @@
|
||||
#ifndef _COMMONLIB_BSD_STRING_H_
|
||||
#define _COMMONLIB_BSD_STRING_H_
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
size_t strlen(const char *src);
|
||||
size_t strnlen(const char *str, size_t maxlen);
|
||||
|
||||
unsigned int skip_atoi(char **ptr);
|
||||
|
||||
#endif /* _COMMONLIB_BSD_STRING_H_ */
|
||||
|
@@ -2,6 +2,26 @@
|
||||
|
||||
#include <commonlib/bsd/string.h>
|
||||
#include <ctype.h>
|
||||
#include <stddef.h>
|
||||
|
||||
size_t strlen(const char *str)
|
||||
{
|
||||
const char *ptr = str;
|
||||
|
||||
while (*ptr++)
|
||||
;
|
||||
return ptr - str - 1;
|
||||
}
|
||||
|
||||
size_t strnlen(const char *str, size_t maxlen)
|
||||
{
|
||||
const char *ptr = str;
|
||||
const char *end = str + maxlen + 1;
|
||||
|
||||
while (*ptr++ && ptr < end)
|
||||
;
|
||||
return ptr - str - 1;
|
||||
}
|
||||
|
||||
unsigned int skip_atoi(char **ptr)
|
||||
{
|
||||
|
@@ -13,8 +13,6 @@ int memcmp(const void *s1, const void *s2, size_t n);
|
||||
void *memchr(const void *s, int c, size_t n);
|
||||
char *strdup(const char *s);
|
||||
char *strconcat(const char *s1, const char *s2);
|
||||
size_t strnlen(const char *src, size_t max);
|
||||
size_t strlen(const char *src);
|
||||
char *strchr(const char *s, int c);
|
||||
char *strncpy(char *to, const char *from, size_t count);
|
||||
char *strcpy(char *dst, const char *src);
|
||||
|
@@ -33,22 +33,6 @@ char *strconcat(const char *s1, const char *s2)
|
||||
return d;
|
||||
}
|
||||
|
||||
size_t strnlen(const char *src, size_t max)
|
||||
{
|
||||
size_t i = 0;
|
||||
while ((*src++) && (i < max))
|
||||
i++;
|
||||
return i;
|
||||
}
|
||||
|
||||
size_t strlen(const char *src)
|
||||
{
|
||||
size_t i = 0;
|
||||
while (*src++)
|
||||
i++;
|
||||
return i;
|
||||
}
|
||||
|
||||
char *strchr(const char *s, int c)
|
||||
{
|
||||
do {
|
||||
|
Reference in New Issue
Block a user