Yu-Ping Wu has uploaded this change for review. ( https://review.coreboot.org/c/coreboot/+/83765?usp=email )
Change subject: lib/string: Add strncat() and strcat() functions ......................................................................
lib/string: Add strncat() and strcat() functions
Change-Id: If02fce0eafb4f6fa01d8bab17d87a32360f4ac83 Signed-off-by: Yu-Ping Wu yupingso@chromium.org --- M src/include/string.h M src/lib/string.c M tests/lib/string-test.c 3 files changed, 77 insertions(+), 0 deletions(-)
git pull ssh://review.coreboot.org:29418/coreboot refs/changes/65/83765/1
diff --git a/src/include/string.h b/src/include/string.h index 30d014a..4f29fcb 100644 --- a/src/include/string.h +++ b/src/include/string.h @@ -18,6 +18,8 @@ char *strchr(const char *s, int c); char *strncpy(char *to, const char *from, size_t count); char *strcpy(char *dst, const char *src); +char *strncat(char *dst, const char *src, size_t count); +char *strcat(char *dst, const char *src); int strcmp(const char *s1, const char *s2); int strncmp(const char *s1, const char *s2, size_t maxlen); size_t strspn(const char *str, const char *spn); diff --git a/src/lib/string.c b/src/lib/string.c index a7f8074..bb81c0f 100644 --- a/src/lib/string.c +++ b/src/lib/string.c @@ -102,6 +102,18 @@ return ptr; }
+char *strncat(char *dst, const char *src, size_t count) +{ + strncpy(dst + strlen(dst), src, count); + return dst; +} + +char *strcat(char *dst, const char *src) +{ + strcpy(dst + strlen(dst), src); + return dst; +} + int strcmp(const char *s1, const char *s2) { int r; diff --git a/tests/lib/string-test.c b/tests/lib/string-test.c index 17bb69a..7039ed9 100644 --- a/tests/lib/string-test.c +++ b/tests/lib/string-test.c @@ -170,6 +170,67 @@ assert_int_equal(0, memcmp(dst, src, __builtin_strlen(src) + 1)); }
+static void test_strncat(void **state) +{ + const char str[] = "Hello "; + size_t len = __builtin_strlen(str); + const char src[] = "World"; + const char expected[] = "Hello World"; + size_t expected_len = __builtin_strlen(expected); + char dst[100]; + char *ret; + + /* Count larger than src len */ + memset(dst, 0, sizeof(dst)); + memcpy(dst, str, len); + ret = strncat(dst, src, len + 5); + assert_ptr_equal(dst, ret); + assert_memory_equal(expected, dst, expected_len + 1); + + /* Count smaller than src len */ + memset(dst, 0, sizeof(dst)); + memcpy(dst, str, len); + ret = strncat(dst, src, len - 1); + assert_ptr_equal(dst, ret); + assert_memory_equal(expected, dst, expected_len + 1); +} + +static void test_strcat(void **state) +{ + const char str[] = "Hello "; + size_t len = __builtin_strlen(str); + const char src[] = "World"; + const char expected[] = "Hello World"; + size_t expected_len = __builtin_strlen(expected); + char dst[100]; + char *ret; + + /* Empty src & dst */ + memset(dst, 0, sizeof(dst)); + ret = strcat(dst, ""); + assert_ptr_equal(dst, ret); + assert_int_equal('\0', dst[0]); + + /* Empty src */ + memset(dst, 0, sizeof(dst)); + memcpy(dst, str, len); + ret = strcat(dst, ""); + assert_memory_equal(str, dst, len + 1); + + /* Empty dst */ + memset(dst, 0, sizeof(dst)); + ret = strcat(dst, src); + assert_ptr_equal(dst, ret); + assert_memory_equal(src, dst, __builtin_strlen(src) + 1); + + /* Non-empty str & dst */ + memset(dst, 0, sizeof(dst)); + memcpy(dst, str, len); + ret = strcat(dst, src); + assert_ptr_equal(dst, ret); + assert_memory_equal(expected, dst, expected_len + 1); +} + static void test_strcmp(void **state) { char str[] = "Banana"; @@ -233,6 +294,8 @@ cmocka_unit_test(test_strrchr), cmocka_unit_test(test_strncpy), cmocka_unit_test(test_strcpy), + cmocka_unit_test(test_strncat), + cmocka_unit_test(test_strcat), cmocka_unit_test(test_strcmp), cmocka_unit_test(test_strncmp), cmocka_unit_test(test_strspn),