Module Name:    src
Committed By:   nia
Date:           Sat Oct 30 11:04:48 UTC 2021

Modified Files:
        src/usr.sbin/rpcbind: rpcb_svc_com.c rpcbind.c

Log Message:
rpcbind(8): Use reallocarr, fix reallocation logic.

In my_svc_run(), only update the fd count _after_ the allocation
succeeds (which may be after waiting in the loop). This function
previously tried to go to a label that doesn't exist. I wonder why
GCC didn't catch this before but does now.


To generate a diff of this commit:
cvs rdiff -u -r1.25 -r1.26 src/usr.sbin/rpcbind/rpcb_svc_com.c
cvs rdiff -u -r1.30 -r1.31 src/usr.sbin/rpcbind/rpcbind.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/rpcb_svc_com.c
diff -u src/usr.sbin/rpcbind/rpcb_svc_com.c:1.25 src/usr.sbin/rpcbind/rpcb_svc_com.c:1.26
--- src/usr.sbin/rpcbind/rpcb_svc_com.c:1.25	Tue Apr 13 05:58:45 2021
+++ src/usr.sbin/rpcbind/rpcb_svc_com.c	Sat Oct 30 11:04:48 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: rpcb_svc_com.c,v 1.25 2021/04/13 05:58:45 mrg Exp $	*/
+/*	$NetBSD: rpcb_svc_com.c,v 1.26 2021/10/30 11:04:48 nia Exp $	*/
 /*	$FreeBSD: head/usr.sbin/rpcbind/rpcb_svc_com.c 301770 2016-06-09 22:25:00Z pfg $ */
 
 /*-
@@ -1106,7 +1106,7 @@ my_svc_run(void)
 {
 	size_t nfds;
 	struct pollfd *pollfds;
-	int npollfds;
+	int npollfds, newfdcount;
 	int poll_ret, check_ret;
 	int n, *m;
 #ifdef SVC_RUN_DEBUG
@@ -1118,19 +1118,19 @@ my_svc_run(void)
 	npollfds = 0;
 
 	for (;;) {
-		if (svc_fdset_getsize(0) != npollfds) {
-			npollfds = svc_fdset_getsize(0);
-			pollfds = realloc(pollfds, npollfds * sizeof(*pollfds));
+		newfdcount = svc_fdset_getsize(0);
+		if (newfdcount != npollfds) {
+			if (reallocarr(&pollfds,
+			    newfdcount, sizeof(*pollfds)) != 0) {
+				syslog(LOG_ERR, "Cannot allocate pollfds");
+				sleep(1);
+				continue;
+			}
+			npollfds = newfdcount;
 		}
 		p = pollfds;
-		if (p == NULL) {
-out:
-			syslog(LOG_ERR, "Cannot allocate pollfds");
-			sleep(1);
-			continue;
-		}
 		if ((m = svc_fdset_getmax()) == NULL)
-			goto out;
+			break;
 		for (n = 0; n <= *m; n++) {
 			if (svc_fdset_isset(n)) {
 				p->fd = n;

Index: src/usr.sbin/rpcbind/rpcbind.c
diff -u src/usr.sbin/rpcbind/rpcbind.c:1.30 src/usr.sbin/rpcbind/rpcbind.c:1.31
--- src/usr.sbin/rpcbind/rpcbind.c:1.30	Sun Mar  7 00:23:06 2021
+++ src/usr.sbin/rpcbind/rpcbind.c	Sat Oct 30 11:04:48 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: rpcbind.c,v 1.30 2021/03/07 00:23:06 christos Exp $	*/
+/*	$NetBSD: rpcbind.c,v 1.31 2021/10/30 11:04:48 nia Exp $	*/
 
 /*-
  * Copyright (c) 2009, Sun Microsystems, Inc.
@@ -362,18 +362,15 @@ 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;
-		nhp = realloc(hosts, nhostsbak * sizeof(*hosts));
-		if (nhp == NULL) {
+		if (reallocarr(&hosts, nhostsbak, sizeof(*hosts)) != 0) {
 			syslog(LOG_ERR, "Can't grow hosts array");
 			return 1;
 		}
-		hosts = nhp;
 		if (nhostsbak == 1)
 			hosts[0] = __UNCONST("*");
 		else {
@@ -903,8 +900,7 @@ parseargs(int argc, char *argv[])
 			break;
 		case 'h':
 			++nhosts;
-			hosts = realloc(hosts, nhosts * sizeof(*hosts));
-			if (hosts == NULL)
+			if (reallocarr(&hosts, nhosts, sizeof(*hosts)) != 0)
 				err(EXIT_FAILURE, "Can't allocate host array");
 			hosts[nhosts - 1] = strdup(optarg);
 			if (hosts[nhosts - 1] == NULL)

Reply via email to