From 09cd91c5b95976e8002251104c8538e026177f24 Mon Sep 17 00:00:00 2001 From: ngie Date: Tue, 18 Jul 2017 18:09:26 +0000 Subject: [PATCH] MFC r319048,r319049,r319051,r319054: r319048: Push `snapshot_file` copying down into run_tests function, and mark snapshot_file const char *. This fixes a bogus set of errors from gcc about strdup not being allowed a NULL argument. r319049: Bump WARNS from 1 to 3 after recent commits to fix warnings in the directory. Tested with: clang 4.0, gcc 4.2.1, gcc 6.3.0 r319051: hostent_test_getnameinfo_eq(..): initialize found_a_host to false CID: 1368943 r319054: hostent_test_getaddrinfo_eq(..): call freeaddrinfo on `ai` when done This plugs a leak of memory allocated via getaddrinfo. CID: 1346866 git-svn-id: svn://svn.freebsd.org/base/stable/10@321144 ccf9f872-aa2e-dd11-9fc8-001c23d0bc1f --- lib/libc/tests/nss/Makefile | 2 +- lib/libc/tests/nss/getaddrinfo_test.c | 25 ++++++------- lib/libc/tests/nss/gethostby_test.c | 52 +++++++++++++++------------ 3 files changed, 44 insertions(+), 35 deletions(-) diff --git a/lib/libc/tests/nss/Makefile b/lib/libc/tests/nss/Makefile index df478fea4..3fa9b3dca 100644 --- a/lib/libc/tests/nss/Makefile +++ b/lib/libc/tests/nss/Makefile @@ -18,7 +18,7 @@ ATF_TESTS_C+= getusershell_test FILES+= mach -WARNS?= 1 +WARNS?= 3 CFLAGS+= -I${SRCTOP}/tests diff --git a/lib/libc/tests/nss/getaddrinfo_test.c b/lib/libc/tests/nss/getaddrinfo_test.c index 98b11a2ca..aeac04110 100644 --- a/lib/libc/tests/nss/getaddrinfo_test.c +++ b/lib/libc/tests/nss/getaddrinfo_test.c @@ -410,11 +410,19 @@ addrinfo_read_hostlist_func(struct addrinfo *ai, char *line) } static void -run_tests(char *hostlist_file, char *snapshot_file, int ai_family) +run_tests(char *hostlist_file, const char *snapshot_file, int ai_family) { struct addrinfo_test_data td, td_snap; + char *snapshot_file_copy; int rv; + if (snapshot_file == NULL) + snapshot_file_copy = NULL; + else { + snapshot_file_copy = strdup(snapshot_file); + ATF_REQUIRE(snapshot_file_copy != NULL); + } + memset(&hints, 0, sizeof(struct addrinfo)); hints.ai_family = ai_family; hints.ai_flags = AI_CANONNAME; @@ -477,24 +485,17 @@ fin: TEST_DATA_DESTROY(addrinfo, &td_snap); TEST_DATA_DESTROY(addrinfo, &td); - free(hostlist_file); - free(snapshot_file); + free(snapshot_file_copy); } #define HOSTLIST_FILE "mach" #define RUN_TESTS(tc, snapshot_file, ai_family) do { \ char *_hostlist_file; \ - char *_snapshot_file; \ ATF_REQUIRE(0 < asprintf(&_hostlist_file, "%s/%s", \ atf_tc_get_config_var(tc, "srcdir"), HOSTLIST_FILE)); \ - if (snapshot_file == NULL) \ - _snapshot_file = NULL; \ - else { \ - _snapshot_file = strdup(snapshot_file); \ - ATF_REQUIRE(_snapshot_file != NULL); \ - } \ - run_tests(_hostlist_file, _snapshot_file, ai_family); \ -} while(0) + run_tests(_hostlist_file, snapshot_file, ai_family); \ + free(_hostlist_file); \ +} while (0) ATF_TC_WITHOUT_HEAD(pf_unspec); ATF_TC_BODY(pf_unspec, tc) diff --git a/lib/libc/tests/nss/gethostby_test.c b/lib/libc/tests/nss/gethostby_test.c index a059770ec..39dbb9d9d 100644 --- a/lib/libc/tests/nss/gethostby_test.c +++ b/lib/libc/tests/nss/gethostby_test.c @@ -776,24 +776,26 @@ hostent_test_getaddrinfo_eq(struct hostent *he, void *mdata __unused) rv = getaddrinfo(he->h_name, NULL, &hints, &ai); if (rv == 0) { printf("not ok - shouldn't have been resolved\n"); - return (-1); - } + rv = -1; + } else + rv = 0; } else { rv = getaddrinfo(he->h_name, NULL, &hints, &ai); if (rv != 0) { printf("not ok - should have been resolved\n"); - return (-1); + rv = -1; + goto done; } - rv = is_hostent_equal(he, ai); if (rv != 0) { printf("not ok - addrinfo and hostent are not equal\n"); - return (-1); + rv = -1; } - } - - return (0); +done: + if (ai != NULL) + freeaddrinfo(ai); + return (rv); } static int @@ -884,7 +886,7 @@ hostent_test_getnameinfo_eq(struct hostent *he, void *mdata __unused) * An address might reverse resolve to hostname alias or the * official hostname, e.g. moon.vub.ac.be. */ - bool found_a_match; + bool found_a_match = false; if (strcmp(result->h_name, buffer) == 0) { found_a_match = true; @@ -924,10 +926,19 @@ static int run_tests(const char *hostlist_file, const char *snapshot_file, int _af_type, enum test_methods method, bool use_ipv6_mapping) { + char *snapshot_file_copy; struct hostent_test_data td, td_addr, td_snap; res_state statp; int rv = -2; + if (snapshot_file == NULL) + snapshot_file_copy = NULL; + else { + snapshot_file_copy = strdup(snapshot_file); + ATF_REQUIRE(snapshot_file_copy != NULL); + } + snapshot_file = snapshot_file_copy; + switch (_af_type) { case AF_INET: ATF_REQUIRE_FEATURE("inet"); @@ -946,8 +957,8 @@ run_tests(const char *hostlist_file, const char *snapshot_file, int _af_type, if (statp == NULL || ((statp->options & RES_INIT) == 0 && res_ninit(statp) == -1)) { printf("error: can't init res_state\n"); - - return (-1); + rv = -1; + goto fin2; } if (use_ipv6_mapping) @@ -1051,6 +1062,9 @@ fin: TEST_DATA_DESTROY(hostent, &td_addr); TEST_DATA_DESTROY(hostent, &td); +fin2: + free(snapshot_file_copy); + return (rv); } @@ -1059,30 +1073,24 @@ fin: #define _RUN_TESTS(tc, snapshot_file, af_type, method, use_ipv6_mapping) \ do { \ char *_hostlist_file; \ - char *_snapshot_file; \ ATF_REQUIRE(0 < asprintf(&_hostlist_file, "%s/%s", \ atf_tc_get_config_var(tc, "srcdir"), HOSTLIST_FILE)); \ - if (snapshot_file == NULL) \ - _snapshot_file = NULL; \ - else { \ - _snapshot_file = strdup(snapshot_file); \ - ATF_REQUIRE(_snapshot_file != NULL); \ - } \ - ATF_REQUIRE(run_tests(_hostlist_file, _snapshot_file, af_type, \ + ATF_REQUIRE(run_tests(_hostlist_file, snapshot_file, af_type, \ method, use_ipv6_mapping) == 0); \ -} while(0) + free(_hostlist_file); \ +} while (0) #define RUN_HOST_TESTS(tc, snapshot_file, af_type, method, use_ipv6_mapping) \ do { \ use_ipnode_functions = false; \ _RUN_TESTS(tc, snapshot_file, af_type, method, use_ipv6_mapping); \ -} while(0) +} while (0) #define RUN_IPNODE_TESTS(tc, snapshot_file, af_type, method, use_ipv6_mapping) \ do { \ use_ipnode_functions = true; \ _RUN_TESTS(tc, snapshot_file, af_type, method, use_ipv6_mapping); \ -} while(0) +} while (0) ATF_TC_WITHOUT_HEAD(gethostbyaddr_ipv4); ATF_TC_BODY(gethostbyaddr_ipv4, tc) -- 2.42.0