From 8edb6c01d27cf3a7e736026f6ca18ce217f0aa87 Mon Sep 17 00:00:00 2001 From: Pietro Cerutti Date: Mon, 20 May 2019 16:10:12 +0000 Subject: [PATCH] Add helper to create and populate an Address, avoid leaks in tests --- address/address.c | 17 +++++++++++++++++ address/address.h | 1 + test/address/mutt_addr_copy.c | 4 ++-- test/address/mutt_addrlist_append.c | 7 +++---- test/address/mutt_addrlist_copy.c | 16 +++++++--------- test/address/mutt_addrlist_count_recips.c | 10 +++++----- test/address/mutt_addrlist_prepend.c | 7 +++---- 7 files changed, 38 insertions(+), 24 deletions(-) diff --git a/address/address.c b/address/address.c index 6c188091d..fb4352a65 100644 --- a/address/address.c +++ b/address/address.c @@ -387,6 +387,23 @@ struct Address *mutt_addr_new(void) return mutt_mem_calloc(1, sizeof(struct Address)); } +/** + * mutt_addr_create - Create and populate a new Address + * @param[in] personal The personal name for the Address (can be NULL) + * @param[in] mailbox The mailbox for the Address (can be NULL) + * @retval ptr Newly allocated Address + * @note The personal and mailbox values, if not NULL, are going to be copied + * into the newly allocated Address. + */ +struct Address *mutt_addr_create(const char *personal, const char *mailbox) +{ + struct Address *a = mutt_addr_new(); + a->personal = mutt_str_strdup(personal); + a->mailbox = mutt_str_strdup(mailbox); + return a; +} + + /** * mutt_addrlist_remove - Remove an Address from a list * @param[in, out] al AddressList diff --git a/address/address.h b/address/address.h index 9da7da449..d7fec2a9f 100644 --- a/address/address.h +++ b/address/address.h @@ -68,6 +68,7 @@ bool mutt_addr_valid_msgid(const char *msgid); /* Functions that work on a single struct Address */ bool mutt_addr_cmp(const struct Address *a, const struct Address *b); struct Address *mutt_addr_copy(const struct Address *addr); +struct Address *mutt_addr_create(const char *personal, const char *mailbox); const char * mutt_addr_for_display(const struct Address *a); void mutt_addr_free(struct Address **a); struct Address *mutt_addr_new(void); diff --git a/test/address/mutt_addr_copy.c b/test/address/mutt_addr_copy.c index 4b9284be3..c78b3e2dd 100644 --- a/test/address/mutt_addr_copy.c +++ b/test/address/mutt_addr_copy.c @@ -49,7 +49,7 @@ void test_mutt_addr_copy(void) TEST_CHECK(a1.group == a2->group); TEST_CHECK(a1.is_intl == a2->is_intl); TEST_CHECK(a1.intl_checked == a2->intl_checked); - FREE(&a2); + mutt_addr_free(&a2); } { @@ -63,6 +63,6 @@ void test_mutt_addr_copy(void) TEST_CHECK(a1.group == a2->group); TEST_CHECK(a1.is_intl == a2->is_intl); TEST_CHECK(a1.intl_checked == a2->intl_checked); - FREE(&a2); + mutt_addr_free(&a2); } } diff --git a/test/address/mutt_addrlist_append.c b/test/address/mutt_addrlist_append.c index e6eeea243..e92fb7466 100644 --- a/test/address/mutt_addrlist_append.c +++ b/test/address/mutt_addrlist_append.c @@ -45,14 +45,13 @@ void test_mutt_addrlist_append(void) } { - struct Address a1 = { .mailbox = "test@example.com" }; - struct Address a2 = { .mailbox = "john@doe.org" }; struct AddressList al = TAILQ_HEAD_INITIALIZER(al); - mutt_addrlist_append(&al, &a1); - mutt_addrlist_append(&al, &a2); + mutt_addrlist_append(&al, mutt_addr_create(NULL, "test@example.com")); + mutt_addrlist_append(&al, mutt_addr_create(NULL, "john@doe.org")); struct Address *a = TAILQ_FIRST(&al); TEST_CHECK_STR_EQ("test@example.com", a->mailbox); a = TAILQ_NEXT(a, entries); TEST_CHECK_STR_EQ("john@doe.org", a->mailbox); + mutt_addrlist_clear(&al); } } diff --git a/test/address/mutt_addrlist_copy.c b/test/address/mutt_addrlist_copy.c index 1adcd67b4..7734399b2 100644 --- a/test/address/mutt_addrlist_copy.c +++ b/test/address/mutt_addrlist_copy.c @@ -56,21 +56,19 @@ void test_mutt_addrlist_copy(void) { struct AddressList src = TAILQ_HEAD_INITIALIZER(src); struct AddressList dst = TAILQ_HEAD_INITIALIZER(dst); - struct Address a1 = { .mailbox = "test@example.com" }; - struct Address a2 = { .mailbox = "john@doe.org" }; - struct Address a3 = { .mailbox = "the-who@stage.co.uk" }; - mutt_addrlist_append(&src, &a1); - mutt_addrlist_append(&src, &a2); - mutt_addrlist_append(&src, &a3); + mutt_addrlist_append(&src, mutt_addr_create(NULL, "test@example.com")); + mutt_addrlist_append(&src, mutt_addr_create(NULL, "john@doe.org")); + mutt_addrlist_append(&src, mutt_addr_create(NULL, "the-who@stage.co.uk")); mutt_addrlist_copy(&dst, &src, false); TEST_CHECK(!TAILQ_EMPTY(&src)); TEST_CHECK(!TAILQ_EMPTY(&dst)); struct Address *adst = TAILQ_FIRST(&dst); - TEST_CHECK_STR_EQ(a1.mailbox, adst->mailbox); + TEST_CHECK_STR_EQ("test@example.com", adst->mailbox); adst = TAILQ_NEXT(adst, entries); - TEST_CHECK_STR_EQ(a2.mailbox, adst->mailbox); + TEST_CHECK_STR_EQ("john@doe.org", adst->mailbox); adst = TAILQ_NEXT(adst, entries); - TEST_CHECK_STR_EQ(a3.mailbox, adst->mailbox); + TEST_CHECK_STR_EQ("the-who@stage.co.uk", adst->mailbox); + mutt_addrlist_clear(&src); mutt_addrlist_clear(&dst); } } diff --git a/test/address/mutt_addrlist_count_recips.c b/test/address/mutt_addrlist_count_recips.c index c29ace056..1c40a55aa 100644 --- a/test/address/mutt_addrlist_count_recips.c +++ b/test/address/mutt_addrlist_count_recips.c @@ -44,26 +44,26 @@ void test_mutt_addrlist_count_recips(void) struct AddressList al = TAILQ_HEAD_INITIALIZER(al); mutt_addrlist_parse(&al, "test@example.com, john@doe.org"); TEST_CHECK(mutt_addrlist_count_recips(&al) == 2); + mutt_addrlist_clear(&al); } { struct AddressList al = TAILQ_HEAD_INITIALIZER(al); mutt_addrlist_parse(&al, "test@example.com, john@doe.org"); mutt_addrlist_append(&al, mutt_addr_new()); - struct Address *a = mutt_addr_new(); - a->mailbox = mutt_str_strdup("foo@bar.baz"); - mutt_addrlist_append(&al, a); + mutt_addrlist_append(&al, mutt_addr_create(NULL, "foo@bar.baz")); TEST_CHECK(mutt_addrlist_count_recips(&al) == 3); + mutt_addrlist_clear(&al); } { struct AddressList al = TAILQ_HEAD_INITIALIZER(al); mutt_addrlist_parse(&al, "test@example.com, john@doe.org"); mutt_addrlist_append(&al, mutt_addr_new()); - struct Address *a = mutt_addr_new(); - a->mailbox = mutt_str_strdup("foo@bar.baz"); + struct Address *a = mutt_addr_create(NULL, "foo@bar.baz"); a->group = 1; mutt_addrlist_append(&al, a); TEST_CHECK(mutt_addrlist_count_recips(&al) == 2); + mutt_addrlist_clear(&al); } } diff --git a/test/address/mutt_addrlist_prepend.c b/test/address/mutt_addrlist_prepend.c index c8f58f556..8196869c6 100644 --- a/test/address/mutt_addrlist_prepend.c +++ b/test/address/mutt_addrlist_prepend.c @@ -45,14 +45,13 @@ void test_mutt_addrlist_prepend(void) } { - struct Address a1 = { .mailbox = "test@example.com" }; - struct Address a2 = { .mailbox = "john@doe.org" }; struct AddressList al = TAILQ_HEAD_INITIALIZER(al); - mutt_addrlist_prepend(&al, &a1); - mutt_addrlist_prepend(&al, &a2); + mutt_addrlist_prepend(&al, mutt_addr_create(NULL, "test@example.com")); + mutt_addrlist_prepend(&al, mutt_addr_create(NULL, "john@doe.org")); struct Address *a = TAILQ_FIRST(&al); TEST_CHECK_STR_EQ("john@doe.org", a->mailbox); a = TAILQ_NEXT(a, entries); TEST_CHECK_STR_EQ("test@example.com", a->mailbox); + mutt_addrlist_clear(&al); } } -- 2.40.0