From 80b62dae14ce067fa9f13145d4ad8a8fef8b17a3 Mon Sep 17 00:00:00 2001 From: Jelmer Vernooij Date: Wed, 12 Dec 2007 14:09:15 +0100 Subject: r26417: Make str_list_copy(mem_ctx, NULL) return NULL rather than an empty list. (This used to be commit cf8636c8b77c745812376d0ea6f0fb6246a2e4fb) --- source4/lib/util/tests/strlist.c | 25 +++++++++++++++++++++++++ source4/lib/util/util_strlist.c | 10 ++++++++-- 2 files changed, 33 insertions(+), 2 deletions(-) (limited to 'source4/lib') diff --git a/source4/lib/util/tests/strlist.c b/source4/lib/util/tests/strlist.c index 437d9d741a..3ecb982e24 100644 --- a/source4/lib/util/tests/strlist.c +++ b/source4/lib/util/tests/strlist.c @@ -64,6 +64,29 @@ static bool test_lists_shell(struct torture_context *tctx, return true; } +static bool test_list_copy(struct torture_context *tctx) +{ + const char **result; + const char *list[] = { "foo", "bar", NULL }; + const char *empty_list[] = { NULL }; + const char **null_list = NULL; + + result = str_list_copy(tctx, list); + torture_assert_int_equal(tctx, str_list_length(result), 2, "list length"); + torture_assert_str_equal(tctx, result[0], "foo", "element 0"); + torture_assert_str_equal(tctx, result[1], "bar", "element 1"); + torture_assert_str_equal(tctx, result[2], NULL, "element 2"); + + result = str_list_copy(tctx, empty_list); + torture_assert_int_equal(tctx, str_list_length(result), 0, "list length"); + torture_assert_str_equal(tctx, result[0], NULL, "element 0"); + + result = str_list_copy(tctx, null_list); + torture_assert(tctx, result == NULL, "result NULL"); + + return true; +} + struct torture_suite *torture_local_util_strlist(TALLOC_CTX *mem_ctx) { struct torture_suite *suite = torture_suite_create(mem_ctx, "STRLIST"); @@ -75,5 +98,7 @@ struct torture_suite *torture_local_util_strlist(TALLOC_CTX *mem_ctx) &test_lists_shell_strings[i]); } + torture_suite_add_simple_test(suite, "list_copy", test_list_copy); + return suite; } diff --git a/source4/lib/util/util_strlist.c b/source4/lib/util/util_strlist.c index 1f1cc17d00..30de4b962d 100644 --- a/source4/lib/util/util_strlist.c +++ b/source4/lib/util/util_strlist.c @@ -199,8 +199,14 @@ _PUBLIC_ size_t str_list_length(const char **list) _PUBLIC_ const char **str_list_copy(TALLOC_CTX *mem_ctx, const char **list) { int i; - const char **ret = talloc_array(mem_ctx, const char *, str_list_length(list)+1); - if (ret == NULL) return NULL; + const char **ret; + + if (list == NULL) + return NULL; + + ret = talloc_array(mem_ctx, const char *, str_list_length(list)+1); + if (ret == NULL) + return NULL; for (i=0;list && list[i];i++) { ret[i] = talloc_strdup(ret, list[i]); -- cgit