~donmcc/astr

326e3fe902e8c23e1638399130d468955bf99da1 — Don McCaughey 1 year, 6 months ago a866d79
Add test for reallocating a `NULL` string.

Validate that `astr_realloc_append_formatted()` and
`astr_realloc_append_formatted_from_va_list()` correctly handles a
pointer to `NULL` as the string argument `s` by adding a unit test to
cover this case.

Fix `astr_realloc_append_formatted_from_va_list()` so that it only calls
`strlen()` on non-`NULL` string pointers.

Rename various unit tests to change `_for_NULL` to `_for_invalid` to
distinguish between valid `NULL` cases and invalid ones.
2 files changed, 22 insertions(+), 7 deletions(-)

M astr.c
M astr_tests.c
M astr.c => astr.c +1 -1
@@ 83,7 83,7 @@ astr_realloc_append_formatted_from_va_list(char **s,
    size_t length_to_append = astr_formatted_length_from_va_list(format, arguments);
    if (!length_to_append) return 0;

    size_t original_length = strlen(*s);
    size_t original_length = *s ? strlen(*s) : 0;
    size_t new_size = original_length + length_to_append + sizeof('\0');
    char *resized_s = realloc(*s, new_size);
    if (!resized_s) return -1;

M astr_tests.c => astr_tests.c +21 -6
@@ 26,7 26,7 @@ test_astr_alloc_formatted(void)


static void
test_astr_alloc_formatted_for_NULL_format(void)
test_astr_alloc_formatted_for_invalid_format(void)
{
    errno = 0;
    char *s = astr_alloc_formatted(NULL);


@@ 117,7 117,7 @@ test_astr_formatted_length(void)


static void
test_astr_formatted_length_for_NULL_format(void)
test_astr_formatted_length_for_invalid_format(void)
{
    errno = 0;
    size_t length = astr_formatted_length(NULL);


@@ 157,6 157,20 @@ test_astr_realloc_append_formatted_for_empty_format(void)
static void
test_astr_realloc_append_formatted_for_NULL_string(void)
{
    char *s = NULL;

    int result = astr_realloc_append_formatted(&s, "bar %i", 42);

    assert(0 == result);
    assert(s);
    assert(astr_eq("bar 42", s));
    free(s);
}


static void
test_astr_realloc_append_formatted_for_invalid_string_pointer(void)
{
    errno = 0;
    int result = astr_realloc_append_formatted(NULL, "foobar");



@@ 166,7 180,7 @@ test_astr_realloc_append_formatted_for_NULL_string(void)


static void
test_astr_realloc_append_formatted_for_NULL_format(void)
test_astr_realloc_append_formatted_for_invalid_format(void)
{
    char *s = astr_alloc_formatted("foo");



@@ 184,17 198,18 @@ main(int argc, char *argv[])
{
    test_astr_alloc_empty();
    test_astr_alloc_formatted();
    test_astr_alloc_formatted_for_NULL_format();
    test_astr_alloc_formatted_for_invalid_format();
    test_astr_cmp();
    test_astr_empty();
    test_astr_eq();
    test_astr_eq_for_NULL_string();
    test_astr_eq_for_empty_strings();
    test_astr_formatted_length();
    test_astr_formatted_length_for_NULL_format();
    test_astr_formatted_length_for_invalid_format();
    test_astr_realloc_append_formatted_for_empty_format();
    test_astr_realloc_append_formatted_for_NULL_string();
    test_astr_realloc_append_formatted_for_NULL_format();
    test_astr_realloc_append_formatted_for_invalid_string_pointer();
    test_astr_realloc_append_formatted_for_invalid_format();
    return EXIT_SUCCESS;
}