Module Name:    src
Committed By:   christos
Date:           Thu Jan  3 19:04:21 UTC 2019

Modified Files:
        src/usr.sbin/rpcbind: check_bound.c rpcb_stat.c rpcb_svc_4.c
            rpcb_svc_com.c rpcbind.c util.c

Log Message:
Handle memory allocations and error exit codes consistently.


To generate a diff of this commit:
cvs rdiff -u -r1.7 -r1.8 src/usr.sbin/rpcbind/check_bound.c
cvs rdiff -u -r1.6 -r1.7 src/usr.sbin/rpcbind/rpcb_stat.c
cvs rdiff -u -r1.8 -r1.9 src/usr.sbin/rpcbind/rpcb_svc_4.c
cvs rdiff -u -r1.20 -r1.21 src/usr.sbin/rpcbind/rpcb_svc_com.c
cvs rdiff -u -r1.25 -r1.26 src/usr.sbin/rpcbind/rpcbind.c
cvs rdiff -u -r1.21 -r1.22 src/usr.sbin/rpcbind/util.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.sbin/rpcbind/check_bound.c
diff -u src/usr.sbin/rpcbind/check_bound.c:1.7 src/usr.sbin/rpcbind/check_bound.c:1.8
--- src/usr.sbin/rpcbind/check_bound.c:1.7	Wed Aug 16 04:44:40 2017
+++ src/usr.sbin/rpcbind/check_bound.c	Thu Jan  3 14:04:21 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: check_bound.c,v 1.7 2017/08/16 08:44:40 christos Exp $	*/
+/*	$NetBSD: check_bound.c,v 1.8 2019/01/03 19:04:21 christos Exp $	*/
 /*	$FreeBSD: head/usr.sbin/rpcbind/check_bound.c 300942 2016-05-29 06:01:18Z ngie $ */
 
 /*-
@@ -125,7 +125,7 @@ add_bndlist(struct netconfig *nconf, str
 	newnconf = getnetconfigent(nconf->nc_netid);
 	if (newnconf == NULL)
 		return (-1);
-	fdl = malloc(sizeof(struct fdlist));
+	fdl = malloc(sizeof(*fdl));
 	if (fdl == NULL) {
 		freenetconfigent(newnconf);
 		syslog(LOG_ERR, "no memory!");

Index: src/usr.sbin/rpcbind/rpcb_stat.c
diff -u src/usr.sbin/rpcbind/rpcb_stat.c:1.6 src/usr.sbin/rpcbind/rpcb_stat.c:1.7
--- src/usr.sbin/rpcbind/rpcb_stat.c:1.6	Wed Aug 16 04:44:40 2017
+++ src/usr.sbin/rpcbind/rpcb_stat.c	Thu Jan  3 14:04:21 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: rpcb_stat.c,v 1.6 2017/08/16 08:44:40 christos Exp $	*/
+/*	$NetBSD: rpcb_stat.c,v 1.7 2019/01/03 19:04:21 christos Exp $	*/
 /* $FreeBSD: head/usr.sbin/rpcbind/rpcb_stat.c 301605 2016-06-08 12:45:22Z ngie $ */
 
 /*-
@@ -125,7 +125,7 @@ rpcbs_getaddr(rpcvers_t rtype, rpcprog_t
 	if (nconf == NULL) {
 		return;
 	}
-	al = (rpcbs_addrlist *) malloc(sizeof (rpcbs_addrlist));
+	al = malloc(sizeof(*al));
 	if (al == NULL) {
 		return;
 	}
@@ -174,7 +174,7 @@ rpcbs_rmtcall(rpcvers_t rtype, rpcproc_t
 	if (nconf == NULL) {
 		return;
 	}
-	rl = (rpcbs_rmtcalllist *) malloc(sizeof (rpcbs_rmtcalllist));
+	rl = malloc(sizeof(*rl));
 	if (rl == NULL) {
 		return;
 	}

Index: src/usr.sbin/rpcbind/rpcb_svc_4.c
diff -u src/usr.sbin/rpcbind/rpcb_svc_4.c:1.8 src/usr.sbin/rpcbind/rpcb_svc_4.c:1.9
--- src/usr.sbin/rpcbind/rpcb_svc_4.c:1.8	Wed Aug 16 04:44:40 2017
+++ src/usr.sbin/rpcbind/rpcb_svc_4.c	Thu Jan  3 14:04:21 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: rpcb_svc_4.c,v 1.8 2017/08/16 08:44:40 christos Exp $	*/
+/*	$NetBSD: rpcb_svc_4.c,v 1.9 2019/01/03 19:04:21 christos Exp $	*/
 /* $FreeBSD: head/usr.sbin/rpcbind/rpcb_svc_4.c 258564 2013-11-25 16:44:02Z hrs $ */
 
 /*-
@@ -387,7 +387,7 @@ rpcbproc_getaddrlist_4_local(void *arg, 
 		/*
 		 * Add it to rlist.
 		 */
-		rp = malloc(sizeof(rpcb_entry_list));
+		rp = malloc(sizeof(*rp));
 		if (rp == NULL)
 			goto fail;
 		a = &rp->rpcb_entry_map;

Index: src/usr.sbin/rpcbind/rpcb_svc_com.c
diff -u src/usr.sbin/rpcbind/rpcb_svc_com.c:1.20 src/usr.sbin/rpcbind/rpcb_svc_com.c:1.21
--- src/usr.sbin/rpcbind/rpcb_svc_com.c:1.20	Wed Aug 16 04:44:40 2017
+++ src/usr.sbin/rpcbind/rpcb_svc_com.c	Thu Jan  3 14:04:21 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: rpcb_svc_com.c,v 1.20 2017/08/16 08:44:40 christos Exp $	*/
+/*	$NetBSD: rpcb_svc_com.c,v 1.21 2019/01/03 19:04:21 christos Exp $	*/
 /*	$FreeBSD: head/usr.sbin/rpcbind/rpcb_svc_com.c 301770 2016-06-09 22:25:00Z pfg $ */
 
 /*-
@@ -179,7 +179,7 @@ map_set(RPCB *regp, char *owner)
 	/*
 	 * add to the end of the list
 	 */
-	rbl = malloc(sizeof(RPCBLIST));
+	rbl = malloc(sizeof(*rbl));
 	if (rbl == NULL)
 		return (FALSE);
 	a = &(rbl->rpcb_map);
@@ -1460,7 +1460,7 @@ add_pmaplist(RPCB *arg)
 	/*
 	 * add to END of list
 	 */
-	pml = malloc(sizeof(struct pmaplist));
+	pml = malloc(sizeof(*pml));
 	if (pml == NULL) {
 		(void) syslog(LOG_ERR, "rpcbind: no memory!\n");
 		return (1);

Index: src/usr.sbin/rpcbind/rpcbind.c
diff -u src/usr.sbin/rpcbind/rpcbind.c:1.25 src/usr.sbin/rpcbind/rpcbind.c:1.26
--- src/usr.sbin/rpcbind/rpcbind.c:1.25	Mon Aug 21 13:01:04 2017
+++ src/usr.sbin/rpcbind/rpcbind.c	Thu Jan  3 14:04:21 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: rpcbind.c,v 1.25 2017/08/21 17:01:04 christos Exp $	*/
+/*	$NetBSD: rpcbind.c,v 1.26 2019/01/03 19:04:21 christos Exp $	*/
 
 /*-
  * Copyright (c) 2009, Sun Microsystems, Inc.
@@ -175,10 +175,11 @@ rpcbind_main(void *arg)
 #ifndef RPCBIND_RUMP
 	/* Check that another rpcbind isn't already running. */
 	if ((rpcbindlockfd = open(RPCBINDDLOCK, O_RDONLY|O_CREAT, 0444)) == -1)
-		err(1, "%s", RPCBINDDLOCK);
+		err(EXIT_FAILURE, "%s", RPCBINDDLOCK);
 
 	if (flock(rpcbindlockfd, LOCK_EX|LOCK_NB) == -1 && errno == EWOULDBLOCK)
-		errx(1, "another rpcbind is already running. Aborting");
+		errx(EXIT_FAILURE,
+		    "another rpcbind is already running. Aborting");
 
 	if (geteuid()) /* This command allowed only to root */
 		errx(EXIT_FAILURE, "Sorry. You are not superuser\n");
@@ -360,12 +361,18 @@ init_transport(struct netconfig *nconf)
 	}
 
 	if (strcmp(nconf->nc_netid, "local") != 0) {
+		char **nhp;
 		/*
 		 * If no hosts were specified, just bind to INADDR_ANY.
 		 * Otherwise  make sure 127.0.0.1 is added to the list.
 		 */
 		nhostsbak = nhosts + 1;
-		hosts = realloc(hosts, nhostsbak * sizeof(char *));
+		nhp = realloc(hosts, nhostsbak * sizeof(*hosts));
+		if (nhp == NULL) {
+			syslog(LOG_ERR, "Can't grow hosts array");
+			return 1;
+		}
+		hosts = nhp;
 		if (nhostsbak == 1)
 			hosts[0] = __UNCONST("*");
 		else {
@@ -583,7 +590,7 @@ init_transport(struct netconfig *nconf)
 			    nconf->nc_netid);
 			goto error;
 		}
-		pml = malloc(sizeof (struct pmaplist));
+		pml = malloc(sizeof(*pml));
 		if (pml == NULL) {
 			syslog(LOG_ERR, "Cannot allocate memory");
 			goto error;
@@ -637,7 +644,7 @@ init_transport(struct netconfig *nconf)
 		list_pml = pml;
 
 		/* Add version 3 information */
-		pml = malloc(sizeof (struct pmaplist));
+		pml = malloc(sizeof(*pml));
 		if (pml == NULL) {
 			syslog(LOG_ERR, "Cannot allocate memory");
 			goto error;
@@ -648,7 +655,7 @@ init_transport(struct netconfig *nconf)
 		list_pml = pml;
 
 		/* Add version 4 information */
-		pml = malloc(sizeof (struct pmaplist));
+		pml = malloc(sizeof(*pml));
 		if (pml == NULL) {
 			syslog(LOG_ERR, "Cannot allocate memory");
 			goto error;
@@ -742,13 +749,17 @@ update_bound_sa(void)
 
 	if (nhosts == 0)
 		return;
-	bound_sa = malloc(sizeof(*bound_sa) * nhosts);
+	bound_sa = calloc(nhosts, sizeof(*bound_sa));
+	if (bound_sa == NULL)
+		err(EXIT_FAILURE, "no space for bound address array");
 	memset(&hints, 0, sizeof(hints));
 	hints.ai_family = PF_UNSPEC;
 	for (i = 0; i < nhosts; i++)  {
 		if (getaddrinfo(hosts[i], NULL, &hints, &res) != 0)
 			continue;
 		bound_sa[i] = malloc(res->ai_addrlen);
+		if (bound_sa[i] == NULL)
+		    err(EXIT_FAILURE, "no space for bound address");
 		memcpy(bound_sa[i], res->ai_addr, res->ai_addrlen);
 	}
 }
@@ -801,7 +812,7 @@ rbllist_add(rpcprog_t prog, rpcvers_t ve
 {
 	rpcblist_ptr rbl;
 
-	rbl = malloc(sizeof(rpcblist));
+	rbl = malloc(sizeof(*rbl));
 	if (rbl == NULL) {
 		syslog(LOG_ERR, "Out of memory");
 		return;
@@ -878,12 +889,12 @@ parseargs(int argc, char *argv[])
 			break;
 		case 'h':
 			++nhosts;
-			hosts = realloc(hosts, nhosts * sizeof(char *));
+			hosts = realloc(hosts, nhosts * sizeof(*hosts));
 			if (hosts == NULL)
-				errx(1, "Out of memory");
+				err(EXIT_FAILURE, "Can't allocate host array");
 			hosts[nhosts - 1] = strdup(optarg);
 			if (hosts[nhosts - 1] == NULL)
-				errx(1, "Out of memory");
+				err(EXIT_FAILURE, "Can't allocate host");
 			break;
 		case 'i':
 			insecure = 1;

Index: src/usr.sbin/rpcbind/util.c
diff -u src/usr.sbin/rpcbind/util.c:1.21 src/usr.sbin/rpcbind/util.c:1.22
--- src/usr.sbin/rpcbind/util.c:1.21	Wed Aug 16 04:44:40 2017
+++ src/usr.sbin/rpcbind/util.c	Thu Jan  3 14:04:21 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: util.c,v 1.21 2017/08/16 08:44:40 christos Exp $	*/
+/*	$NetBSD: util.c,v 1.22 2019/01/03 19:04:21 christos Exp $	*/
 /* $FreeBSD: head/usr.sbin/rpcbind/util.c 300973 2016-05-29 20:28:01Z ngie $ */
 
 /*-
@@ -145,8 +145,11 @@ addrmerge(struct netbuf *caller, char *s
 			return strdup(serv_uaddr);
 		}
 	} else {
-		clnt_sa = (struct sockaddr *)
-		    malloc(sizeof (struct sockaddr_storage));
+		clnt_sa = malloc(sizeof(*clnt_sa));
+		if (clnt_sa == NULL) {
+			free(serv_nbp);
+			return NULL;
+		}
 		memcpy(clnt_sa, clnt, clnt->sa_len);
 	}
 
@@ -313,7 +316,7 @@ network_init()
 			fprintf(stderr, "can't get local ip4 address: %s\n",
 			    gai_strerror(ecode));
 	} else {
-		local_in4 = (struct sockaddr_in *)malloc(sizeof *local_in4);
+		local_in4 = malloc(sizeof(*local_in4));
 		if (local_in4 == NULL) {
 			if (debugging)
 				fprintf(stderr, "can't alloc local ip4 addr\n");
@@ -328,7 +331,7 @@ network_init()
 			fprintf(stderr, "can't get local ip6 address: %s\n",
 			    gai_strerror(ecode));
 	} else {
-		local_in6 = (struct sockaddr_in6 *)malloc(sizeof *local_in6);
+		local_in6 = malloc(sizeof(*local_in6));
 		if (local_in6 == NULL) {
 			if (debugging)
 				fprintf(stderr, "can't alloc local ip6 addr\n");

Reply via email to