svn commit: r321143 - stable/11/lib/libc/tests/nss

Ngie Cooper ngie at FreeBSD.org
Tue Jul 18 18:09:17 UTC 2017


Author: ngie
Date: Tue Jul 18 18:09:16 2017
New Revision: 321143
URL: https://svnweb.freebsd.org/changeset/base/321143

Log:
  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

Modified:
  stable/11/lib/libc/tests/nss/Makefile
  stable/11/lib/libc/tests/nss/getaddrinfo_test.c
  stable/11/lib/libc/tests/nss/gethostby_test.c
Directory Properties:
  stable/11/   (props changed)

Modified: stable/11/lib/libc/tests/nss/Makefile
==============================================================================
--- stable/11/lib/libc/tests/nss/Makefile	Tue Jul 18 17:51:46 2017	(r321142)
+++ stable/11/lib/libc/tests/nss/Makefile	Tue Jul 18 18:09:16 2017	(r321143)
@@ -20,7 +20,7 @@ ATF_TESTS_C+=	getusershell_test
 
 ${PACKAGE}FILES+=	mach
 
-WARNS?=		1
+WARNS?=		3
 
 CFLAGS+=	-I${SRCTOP}/tests
 

Modified: stable/11/lib/libc/tests/nss/getaddrinfo_test.c
==============================================================================
--- stable/11/lib/libc/tests/nss/getaddrinfo_test.c	Tue Jul 18 17:51:46 2017	(r321142)
+++ stable/11/lib/libc/tests/nss/getaddrinfo_test.c	Tue Jul 18 18:09:16 2017	(r321143)
@@ -410,11 +410,19 @@ addrinfo_read_hostlist_func(struct addrinfo *ai, char 
 }
 
 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)

Modified: stable/11/lib/libc/tests/nss/gethostby_test.c
==============================================================================
--- stable/11/lib/libc/tests/nss/gethostby_test.c	Tue Jul 18 17:51:46 2017	(r321142)
+++ stable/11/lib/libc/tests/nss/gethostby_test.c	Tue Jul 18 18:09:16 2017	(r321143)
@@ -776,24 +776,26 @@ hostent_test_getaddrinfo_eq(struct hostent *he, void *
 		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 *
 		 * 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 *snaps
 		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)


More information about the svn-src-all mailing list