# HG changeset patch # User Mike Becker # Date 1767180796 -3600 # Node ID 0344372c7115d8ee24ec89b8437afa4722c0e697 # Parent 89a07d18f86c1f7e12be40f8a97ff7b44f79fdd1 add test for cx_strcat() with zero additional strings diff -r 89a07d18f86c -r 0344372c7115 src/string.c --- a/src/string.c Wed Dec 31 12:24:39 2025 +0100 +++ b/src/string.c Wed Dec 31 12:33:16 2025 +0100 @@ -106,7 +106,11 @@ ... ) { if (count == 0) { - return cx_strdup_a(alloc, str); + if (cxReallocate(alloc, &str.ptr, str.length + 1)) { + return CX_NULLSTR; // LCOV_EXCL_LINE + } + str.ptr[str.length] = '\0'; + return str; } va_list ap; va_start(ap, count); diff -r 89a07d18f86c -r 0344372c7115 tests/test_string.c --- a/tests/test_string.c Wed Dec 31 12:24:39 2025 +0100 +++ b/tests/test_string.c Wed Dec 31 12:33:16 2025 +0100 @@ -595,6 +595,36 @@ cx_testing_allocator_destroy(&talloc); } +CX_TEST(test_strcat_zero) { + CxTestingAllocator talloc; + cx_testing_allocator_init(&talloc); + CxAllocator *alloc = &talloc.base; + + CX_TEST_DO { + cxmutstr t1 = cx_strcat_a(alloc, CX_NULLSTR, 0); + CX_TEST_ASSERT(0 == cx_strcmp(t1, "")); + ASSERT_ZERO_TERMINATED(t1); + cx_strfree_a(alloc, &t1); + + cxmutstr s2 = cx_strdup_a(alloc, "testing"); + s2.length = 4; + cxmutstr t2 = cx_strcat_a(alloc, s2, 0); + CX_TEST_ASSERT(0 == cx_strcmp(t2, "test")); + ASSERT_ZERO_TERMINATED(t2); + cx_strfree_a(alloc, &t2); + + cxmutstr s3 = cx_strdup_a(alloc, "fake"); + s3.length = 0; + cxmutstr t3 = cx_strcat_a(alloc, s3, 0); + CX_TEST_ASSERT(0 == cx_strcmp(t3, "")); + ASSERT_ZERO_TERMINATED(t3); + cx_strfree_a(alloc, &t3); + + CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); + } + cx_testing_allocator_destroy(&talloc); +} + CX_TEST(test_strcat_more_than_eight) { cxstring s1 = cx_str("12"); cxstring s2 = cx_str("34"); @@ -1684,6 +1714,7 @@ cx_test_register(suite, test_strcmp); cx_test_register(suite, test_strcasecmp); cx_test_register(suite, test_strcat); + cx_test_register(suite, test_strcat_zero); cx_test_register(suite, test_strcat_more_than_eight); cx_test_register(suite, test_strsplit); cx_test_register(suite, test_strsplit_a);