Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.sbin/rpcbind Handle memory allocations and error exit co...



details:   https://anonhg.NetBSD.org/src/rev/962dc71e7856
branches:  trunk
changeset: 838119:962dc71e7856
user:      christos <christos%NetBSD.org@localhost>
date:      Thu Jan 03 19:04:21 2019 +0000

description:
Handle memory allocations and error exit codes consistently.

diffstat:

 usr.sbin/rpcbind/check_bound.c  |   4 ++--
 usr.sbin/rpcbind/rpcb_stat.c    |   6 +++---
 usr.sbin/rpcbind/rpcb_svc_4.c   |   4 ++--
 usr.sbin/rpcbind/rpcb_svc_com.c |   6 +++---
 usr.sbin/rpcbind/rpcbind.c      |  35 +++++++++++++++++++++++------------
 usr.sbin/rpcbind/util.c         |  13 ++++++++-----
 6 files changed, 41 insertions(+), 27 deletions(-)

diffs (245 lines):

diff -r c017fcc4b6c7 -r 962dc71e7856 usr.sbin/rpcbind/check_bound.c
--- a/usr.sbin/rpcbind/check_bound.c    Thu Jan 03 18:28:21 2019 +0000
+++ b/usr.sbin/rpcbind/check_bound.c    Thu Jan 03 19:04:21 2019 +0000
@@ -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 @@
        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!");
diff -r c017fcc4b6c7 -r 962dc71e7856 usr.sbin/rpcbind/rpcb_stat.c
--- a/usr.sbin/rpcbind/rpcb_stat.c      Thu Jan 03 18:28:21 2019 +0000
+++ b/usr.sbin/rpcbind/rpcb_stat.c      Thu Jan 03 19:04:21 2019 +0000
@@ -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 @@
        if (nconf == NULL) {
                return;
        }
-       al = (rpcbs_addrlist *) malloc(sizeof (rpcbs_addrlist));
+       al = malloc(sizeof(*al));
        if (al == NULL) {
                return;
        }
@@ -174,7 +174,7 @@
        if (nconf == NULL) {
                return;
        }
-       rl = (rpcbs_rmtcalllist *) malloc(sizeof (rpcbs_rmtcalllist));
+       rl = malloc(sizeof(*rl));
        if (rl == NULL) {
                return;
        }
diff -r c017fcc4b6c7 -r 962dc71e7856 usr.sbin/rpcbind/rpcb_svc_4.c
--- a/usr.sbin/rpcbind/rpcb_svc_4.c     Thu Jan 03 18:28:21 2019 +0000
+++ b/usr.sbin/rpcbind/rpcb_svc_4.c     Thu Jan 03 19:04:21 2019 +0000
@@ -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 @@
                /*
                 * Add it to rlist.
                 */
-               rp = malloc(sizeof(rpcb_entry_list));
+               rp = malloc(sizeof(*rp));
                if (rp == NULL)
                        goto fail;
                a = &rp->rpcb_entry_map;
diff -r c017fcc4b6c7 -r 962dc71e7856 usr.sbin/rpcbind/rpcb_svc_com.c
--- a/usr.sbin/rpcbind/rpcb_svc_com.c   Thu Jan 03 18:28:21 2019 +0000
+++ b/usr.sbin/rpcbind/rpcb_svc_com.c   Thu Jan 03 19:04:21 2019 +0000
@@ -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 @@
        /*
         * 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 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);
diff -r c017fcc4b6c7 -r 962dc71e7856 usr.sbin/rpcbind/rpcbind.c
--- a/usr.sbin/rpcbind/rpcbind.c        Thu Jan 03 18:28:21 2019 +0000
+++ b/usr.sbin/rpcbind/rpcbind.c        Thu Jan 03 19:04:21 2019 +0000
@@ -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 @@
 #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 @@
        }
 
        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 @@
                            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 @@
                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 @@
                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 @@
 
        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 @@
 {
        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 @@
                        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;
diff -r c017fcc4b6c7 -r 962dc71e7856 usr.sbin/rpcbind/util.c
--- a/usr.sbin/rpcbind/util.c   Thu Jan 03 18:28:21 2019 +0000
+++ b/usr.sbin/rpcbind/util.c   Thu Jan 03 19:04:21 2019 +0000
@@ -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 @@
                        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 @@
                        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 @@
                        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");



Home | Main Index | Thread Index | Old Index