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:
@ -47,8 +47,6 @@ int memcmp(const void *s1, const void *s2, size_t len);
|
|||||||
* @defgroup string String functions
|
* @defgroup string String functions
|
||||||
* @{
|
* @{
|
||||||
*/
|
*/
|
||||||
size_t strnlen(const char *str, size_t maxlen);
|
|
||||||
size_t strlen(const char *str);
|
|
||||||
int strcmp(const char *s1, const char *s2);
|
int strcmp(const char *s1, const char *s2);
|
||||||
int strncmp(const char *s1, const char *s2, size_t maxlen);
|
int strncmp(const char *s1, const char *s2, size_t maxlen);
|
||||||
int strcasecmp(const char *s1, const char *s2);
|
int strcasecmp(const char *s1, const char *s2);
|
||||||
|
@ -35,50 +35,6 @@
|
|||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
|
||||||
/**
|
|
||||||
* 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.
|
* Compare two strings.
|
||||||
*
|
*
|
||||||
|
@ -3,8 +3,12 @@
|
|||||||
#ifndef _COMMONLIB_BSD_STRING_H_
|
#ifndef _COMMONLIB_BSD_STRING_H_
|
||||||
#define _COMMONLIB_BSD_STRING_H_
|
#define _COMMONLIB_BSD_STRING_H_
|
||||||
|
|
||||||
|
#include <stddef.h>
|
||||||
#include <stdint.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);
|
unsigned int skip_atoi(char **ptr);
|
||||||
|
|
||||||
#endif /* _COMMONLIB_BSD_STRING_H_ */
|
#endif /* _COMMONLIB_BSD_STRING_H_ */
|
||||||
|
@ -2,6 +2,26 @@
|
|||||||
|
|
||||||
#include <commonlib/bsd/string.h>
|
#include <commonlib/bsd/string.h>
|
||||||
#include <ctype.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)
|
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);
|
void *memchr(const void *s, int c, size_t n);
|
||||||
char *strdup(const char *s);
|
char *strdup(const char *s);
|
||||||
char *strconcat(const char *s1, const char *s2);
|
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 *strchr(const char *s, int c);
|
||||||
char *strncpy(char *to, const char *from, size_t count);
|
char *strncpy(char *to, const char *from, size_t count);
|
||||||
char *strcpy(char *dst, const char *src);
|
char *strcpy(char *dst, const char *src);
|
||||||
|
@ -33,22 +33,6 @@ char *strconcat(const char *s1, const char *s2)
|
|||||||
return d;
|
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)
|
char *strchr(const char *s, int c)
|
||||||
{
|
{
|
||||||
do {
|
do {
|
||||||
|
@ -1,24 +1,57 @@
|
|||||||
/* SPDX-License-Identifier: GPL-2.0-only */
|
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||||
|
|
||||||
#include <commonlib/bsd/string.h>
|
#include <commonlib/bsd/string.h>
|
||||||
|
#include <stddef.h>
|
||||||
#include <tests/test.h>
|
#include <tests/test.h>
|
||||||
|
|
||||||
/* Used to test skip_atoi */
|
static void test_strlen(void **state)
|
||||||
struct str_with_u_val_t {
|
{
|
||||||
char *str;
|
const char *str;
|
||||||
uint32_t value;
|
|
||||||
uint32_t offset;
|
str = "coreboot";
|
||||||
} str_with_u_val[] = {
|
assert_int_equal(__builtin_strlen(str), strlen(str));
|
||||||
{"42aa", 42, 2},
|
|
||||||
{"a", 0, 0},
|
str = "is\0very";
|
||||||
{"0", 0, 1},
|
assert_int_equal(__builtin_strlen(str), strlen(str));
|
||||||
{"4a2", 4, 1},
|
|
||||||
};
|
str = "nice\n";
|
||||||
|
assert_int_equal(__builtin_strlen(str), strlen(str));
|
||||||
|
|
||||||
|
assert_int_equal(0, strlen(""));
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_strnlen(void **state)
|
||||||
|
{
|
||||||
|
/* maxlen larger than string len */
|
||||||
|
assert_int_equal(8, strnlen("coreboot", 100));
|
||||||
|
|
||||||
|
/* maxlen equal to string len */
|
||||||
|
assert_int_equal(8, strnlen("coreboot", 8));
|
||||||
|
|
||||||
|
/* maxlen smaller than string len */
|
||||||
|
assert_int_equal(5, strnlen("coreboot", 5));
|
||||||
|
|
||||||
|
/* maxlen is 0 */
|
||||||
|
assert_int_equal(0, strnlen("coreboot", 0));
|
||||||
|
|
||||||
|
/* Empty string */
|
||||||
|
assert_int_equal(0, strnlen("", 3));
|
||||||
|
}
|
||||||
|
|
||||||
static void test_skip_atoi(void **state)
|
static void test_skip_atoi(void **state)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
char *ptr, *copy;
|
char *ptr, *copy;
|
||||||
|
const struct str_with_u_val_t {
|
||||||
|
char *str;
|
||||||
|
uint32_t value;
|
||||||
|
uint32_t offset;
|
||||||
|
} str_with_u_val[] = {
|
||||||
|
{"42aa", 42, 2},
|
||||||
|
{"a", 0, 0},
|
||||||
|
{"0", 0, 1},
|
||||||
|
{"4a2", 4, 1},
|
||||||
|
};
|
||||||
|
|
||||||
for (i = 0; i < ARRAY_SIZE(str_with_u_val); i++) {
|
for (i = 0; i < ARRAY_SIZE(str_with_u_val); i++) {
|
||||||
ptr = str_with_u_val[i].str;
|
ptr = str_with_u_val[i].str;
|
||||||
@ -31,6 +64,8 @@ static void test_skip_atoi(void **state)
|
|||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
const struct CMUnitTest tests[] = {
|
const struct CMUnitTest tests[] = {
|
||||||
|
cmocka_unit_test(test_strlen),
|
||||||
|
cmocka_unit_test(test_strnlen),
|
||||||
cmocka_unit_test(test_skip_atoi),
|
cmocka_unit_test(test_skip_atoi),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -23,12 +23,6 @@ struct string_pairs_t {
|
|||||||
{"", ""},
|
{"", ""},
|
||||||
};
|
};
|
||||||
|
|
||||||
const char *strings[] = {
|
|
||||||
"coreboot",
|
|
||||||
"is\0very",
|
|
||||||
"nice\n"
|
|
||||||
};
|
|
||||||
|
|
||||||
/* Used to test atol */
|
/* Used to test atol */
|
||||||
struct str_with_l_val_t {
|
struct str_with_l_val_t {
|
||||||
char *str;
|
char *str;
|
||||||
@ -76,26 +70,6 @@ static void test_strconcat(void **state)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void test_strnlen(void **state)
|
|
||||||
{
|
|
||||||
int i, n = 5;
|
|
||||||
size_t str_len, limited_len;
|
|
||||||
|
|
||||||
for (i = 0; i < ARRAY_SIZE(strings); i++) {
|
|
||||||
str_len = __builtin_strlen(strings[i]);
|
|
||||||
limited_len = MIN(n, str_len);
|
|
||||||
assert_int_equal(limited_len, strnlen(strings[i], n));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void test_strlen(void **state)
|
|
||||||
{
|
|
||||||
int i;
|
|
||||||
|
|
||||||
for (i = 0; i < ARRAY_SIZE(strings); i++)
|
|
||||||
assert_int_equal(__builtin_strlen(strings[i]), strlen(strings[i]));
|
|
||||||
}
|
|
||||||
|
|
||||||
static void test_strchr(void **state)
|
static void test_strchr(void **state)
|
||||||
{
|
{
|
||||||
char str[] = "Abracadabra!\n";
|
char str[] = "Abracadabra!\n";
|
||||||
@ -227,8 +201,6 @@ int main(void)
|
|||||||
const struct CMUnitTest tests[] = {
|
const struct CMUnitTest tests[] = {
|
||||||
cmocka_unit_test(test_strdup),
|
cmocka_unit_test(test_strdup),
|
||||||
cmocka_unit_test(test_strconcat),
|
cmocka_unit_test(test_strconcat),
|
||||||
cmocka_unit_test(test_strnlen),
|
|
||||||
cmocka_unit_test(test_strlen),
|
|
||||||
cmocka_unit_test(test_strchr),
|
cmocka_unit_test(test_strchr),
|
||||||
cmocka_unit_test(test_strrchr),
|
cmocka_unit_test(test_strrchr),
|
||||||
cmocka_unit_test(test_strncpy),
|
cmocka_unit_test(test_strncpy),
|
||||||
|
Reference in New Issue
Block a user