scrub_for_print() is underdocumented, so it's not clear what dst_len_out is intended to hold. However, I'm assuming it's meant to hold the untruncated length of the escaped string based on the following evidence: * the note "Output might be truncated, compared to input." * this function is based on snprintf(), which returns the untruncated string length * you can already get the actual output length by doing strlen() on the destination buffer, so returning the actual length isn't as useful (though it helps avoid an O(n) character walk)
These tests currently fail due to bugs in scrub_for_print(). Subsequent commits will fix the bugs. --- lib/util/tests/stringutils-test.c | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/lib/util/tests/stringutils-test.c b/lib/util/tests/stringutils-test.c index c5302fe..d622b8a 100644 --- a/lib/util/tests/stringutils-test.c +++ b/lib/util/tests/stringutils-test.c @@ -39,10 +39,12 @@ static bool test_scrub_for_print__length_if_truncated( char dst[DST_LEN + 50]; char ref[sizeof(dst) - DST_LEN]; init_dst(dst + DST_LEN, ref, sizeof(ref)); + size_t len_out; - scrub_for_print(dst, src, DST_LEN, NULL, ""); + scrub_for_print(dst, src, DST_LEN, &len_out, ""); TEST(size_t, "%zu", strlen(dst), ==, DST_LEN - 1); TEST_BOOL(memcmp(dst + DST_LEN, ref, sizeof(ref)), false); + TEST(size_t, "%zu", len_out, ==, strlen(src)); return true; } @@ -54,11 +56,13 @@ static bool test_scrub_for_print__length_if_expansion_truncated( char dst[dst_sz + 50]; char ref[sizeof(dst) - dst_sz]; init_dst(dst + dst_sz, ref, sizeof(ref)); + size_t len_out; sprintf(src, "%c%c%s", 'a', '\n', "12345"); - scrub_for_print(dst, src, dst_sz, NULL, ""); + scrub_for_print(dst, src, dst_sz, &len_out, ""); TEST(size_t, "%zu", strlen(dst), ==, dst_sz - 1); TEST_BOOL(memcmp(dst + dst_sz, ref, sizeof(ref)), false); + TEST(size_t, "%zu", len_out, ==, strlen(src) + 3); return true; } @@ -69,9 +73,11 @@ static bool test_scrub_for_print__null_input( size_t const DST_LEN = 5; char src[] = ""; char dst[DST_LEN]; + size_t len_out; - scrub_for_print(dst, src, DST_LEN, NULL, ""); + scrub_for_print(dst, src, DST_LEN, &len_out, ""); TEST(size_t, "%zu", strlen(dst), ==, 0); + TEST(size_t, "%zu", len_out, ==, strlen(src)); return true; } @@ -82,9 +88,11 @@ static bool test_scrub_for_print__copy_all( size_t const DST_LEN = 10; char src[] = "abcdefg"; char dst[DST_LEN]; + size_t len_out; - scrub_for_print(dst, src, DST_LEN, NULL, ""); + scrub_for_print(dst, src, DST_LEN, &len_out, ""); TEST(size_t, "%zu", strlen(dst), ==, strlen(src)); + TEST(size_t, "%zu", len_out, ==, strlen(src)); return true; } @@ -95,9 +103,11 @@ static bool test_scrub_for_print__backslash( size_t const DST_LEN = 50; char src[] = "ab\\cde\\"; char dst[DST_LEN]; + size_t len_out; - scrub_for_print(dst, src, DST_LEN, NULL, ""); + scrub_for_print(dst, src, DST_LEN, &len_out, ""); TEST(size_t, "%zu", strlen(dst), ==, strlen(src) + 2); + TEST(size_t, "%zu", len_out, ==, strlen(src) + 2); return true; } @@ -108,9 +118,11 @@ static bool test_scrub_for_print__escape_chars( size_t const DST_LEN = 50; char src[] = "abcde"; char dst[DST_LEN]; + size_t len_out; - scrub_for_print(dst, src, DST_LEN, NULL, "bd"); + scrub_for_print(dst, src, DST_LEN, &len_out, "bd"); TEST_BOOL(strcmp(dst, "a\\bc\\de"), false); + TEST(size_t, "%zu", len_out, ==, strlen(src) + 2); return true; } -- 2.4.3 ------------------------------------------------------------------------------ _______________________________________________ rpstir-devel mailing list rpstir-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/rpstir-devel