Attention is currently required from: Jakub Czapiga, Julius Werner, Yu-Ping Wu.
Hello build bot (Jenkins), Jakub Czapiga, Julius Werner, Yu-Ping Wu,
I'd like you to do a code review. Please visit
https://review.coreboot.org/c/coreboot/+/83931?usp=email
to review the following change.
Change subject: Revert "commonlib/bsd: Add strcat() and strncat() functions" ......................................................................
Revert "commonlib/bsd: Add strcat() and strncat() functions"
This reverts commit aff734bc42b4e132abb5de0e44b0464dd4688590.
Reason for revert: b/359951393 breaking builds for ChromeOS and likely others
Change-Id: I32d79fceca462e363c7d7b17a08d6c60c2bb8aff Signed-off-by: Jon Murphy jpmurphy@google.com --- M payloads/libpayload/include/string.h M payloads/libpayload/libc/string.c M src/commonlib/bsd/include/commonlib/bsd/string.h M src/commonlib/bsd/string.c M tests/commonlib/bsd/string-test.c 5 files changed, 42 insertions(+), 99 deletions(-)
git pull ssh://review.coreboot.org:29418/coreboot refs/changes/31/83931/1
diff --git a/payloads/libpayload/include/string.h b/payloads/libpayload/include/string.h index b002822..7762c36 100644 --- a/payloads/libpayload/include/string.h +++ b/payloads/libpayload/include/string.h @@ -53,6 +53,8 @@ int strncasecmp(const char *s1, const char *s2, size_t maxlen); char *strncpy(char *d, const char *s, size_t n); char *strcpy(char *d, const char *s); +char *strncat(char *d, const char *s, size_t n); +char *strcat(char *d, const char *s); char *strchr(const char *s, int c); char *strrchr(const char *s, int c); char *strdup(const char *s); diff --git a/payloads/libpayload/libc/string.c b/payloads/libpayload/libc/string.c index a1b7d4d..f9805e3 100644 --- a/payloads/libpayload/libc/string.c +++ b/payloads/libpayload/libc/string.c @@ -153,6 +153,46 @@ }
/** + * Concatenates two strings + * + * @param d The destination string. + * @param s The source string. + * @return A pointer to the destination string. + */ +char *strcat(char *d, const char *s) +{ + char *p = d + strlen(d); + size_t sl = strlen(s); + + for (size_t i = 0; i < sl; i++) + p[i] = s[i]; + + p[sl] = '\0'; + return d; +} + +/** + * Concatenates two strings with a maximum length. + * + * @param d The destination string. + * @param s The source string. + * @param n Not more than n characters from s will be appended to d. + * @return A pointer to the destination string. + */ +char *strncat(char *d, const char *s, size_t n) +{ + char *p = d + strlen(d); + size_t sl = strlen(s); + size_t max = n > sl ? sl : n; + + for (size_t i = 0; i < max; i++) + p[i] = s[i]; + + p[max] = '\0'; + return d; +} + +/** * Concatenates two strings with a maximum length. * * @param d The destination string. diff --git a/src/commonlib/bsd/include/commonlib/bsd/string.h b/src/commonlib/bsd/include/commonlib/bsd/string.h index 5c97c65..8e8b896 100644 --- a/src/commonlib/bsd/include/commonlib/bsd/string.h +++ b/src/commonlib/bsd/include/commonlib/bsd/string.h @@ -8,8 +8,6 @@
size_t strlen(const char *src); size_t strnlen(const char *str, size_t maxlen); -char *strcat(char *dst, const char *src); -char *strncat(char *dst, const char *src, size_t n);
unsigned int skip_atoi(char **ptr);
diff --git a/src/commonlib/bsd/string.c b/src/commonlib/bsd/string.c index 16cd4b5..a9dce39 100644 --- a/src/commonlib/bsd/string.c +++ b/src/commonlib/bsd/string.c @@ -23,29 +23,6 @@ return ptr - str - 1; }
-char *strcat(char *dst, const char *src) -{ - char *ptr = dst + strlen(dst); - - while (*src) - *ptr++ = *src++; - - *ptr = '\0'; - return dst; -} - -char *strncat(char *dst, const char *src, size_t n) -{ - char *ptr = dst + strlen(dst); - - /* Not using strncpy() because '\0' may not be appended. */ - while (n-- > 0 && *src) - *ptr++ = *src++; - - *ptr = '\0'; - return dst; -} - unsigned int skip_atoi(char **ptr) { unsigned int result = 0; diff --git a/tests/commonlib/bsd/string-test.c b/tests/commonlib/bsd/string-test.c index 37419f0..29e3977 100644 --- a/tests/commonlib/bsd/string-test.c +++ b/tests/commonlib/bsd/string-test.c @@ -2,7 +2,6 @@
#include <commonlib/bsd/string.h> #include <stddef.h> -#include <string.h> #include <tests/test.h>
static void test_strlen(void **state) @@ -39,77 +38,6 @@ assert_int_equal(0, strnlen("", 3)); }
-static void test_strcat(void **state) -{ - static const char str[] = "Hello "; - size_t len = __builtin_strlen(str); - static const char src[] = "World"; - static const char expected[] = "Hello World"; - size_t expected_len = __builtin_strlen(expected); - char dst[100]; - char *ret; - - /* Empty src & dst */ - dst[0] = '\0'; - memset(dst + 1, 0xee, sizeof(dst) - 1); - ret = strcat(dst, ""); - assert_ptr_equal(dst, ret); - assert_int_equal('\0', dst[0]); - - /* Empty src */ - memcpy(dst, str, len + 1); - memset(dst + len + 1, 0xee, sizeof(dst) - len - 1); - ret = strcat(dst, ""); - assert_memory_equal(str, dst, len + 1); - - /* Empty dst */ - memset(dst, 0x0, sizeof(dst)); - memset(dst + 1, 0xee, sizeof(dst) - 1); - ret = strcat(dst, src); - assert_ptr_equal(dst, ret); - assert_memory_equal(src, dst, __builtin_strlen(src) + 1); - - /* Non-empty str & dst */ - memcpy(dst, str, len + 1); - memset(dst + len + 1, 0xee, sizeof(dst) - len - 1); - ret = strcat(dst, src); - assert_ptr_equal(dst, ret); - assert_memory_equal(expected, dst, expected_len + 1); -} - -static void test_strncat(void **state) -{ - static const char str[] = "Hello "; - size_t len = __builtin_strlen(str); - static const char src[] = "World"; - size_t src_len = __builtin_strlen(src); - static const char expected[] = "Hello World"; - size_t expected_len = __builtin_strlen(expected); - char dst[100]; - char *ret; - - /* n larger than src len */ - memcpy(dst, str, len + 1); - memset(dst + len + 1, 0xee, sizeof(dst) - len - 1); - ret = strncat(dst, src, src_len + 5); - assert_ptr_equal(dst, ret); - assert_memory_equal(expected, dst, expected_len + 1); - - /* n smaller than src len */ - memcpy(dst, str, len + 1); - memset(dst + len + 1, 0xee, sizeof(dst) - len - 1); - ret = strncat(dst, src, src_len - 2); - assert_ptr_equal(dst, ret); - assert_memory_equal("Hello Wor", dst, expected_len - 2 + 1); - - /* n is 0 */ - memcpy(dst, str, len + 1); - memset(dst + len + 1, 0xee, sizeof(dst) - len - 1); - ret = strncat(dst, src, 0); - assert_ptr_equal(dst, ret); - assert_memory_equal(str, dst, len + 1); -} - static void test_skip_atoi(void **state) { int i; @@ -138,8 +66,6 @@ const struct CMUnitTest tests[] = { cmocka_unit_test(test_strlen), cmocka_unit_test(test_strnlen), - cmocka_unit_test(test_strcat), - cmocka_unit_test(test_strncat), cmocka_unit_test(test_skip_atoi), };