Handle memory allocations and error exit codes consistently.

This commit is contained in:
christos 2019-01-03 19:04:21 +00:00
parent 7965e0e70c
commit 05d4f66d89
6 changed files with 41 additions and 27 deletions

View File

@ -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, struct netbuf *baddr __unused)
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!");

View File

@ -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 prog, rpcvers_t vers,
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 rpcbproc, rpcprog_t prog,
if (nconf == NULL) {
return;
}
rl = (rpcbs_rmtcalllist *) malloc(sizeof (rpcbs_rmtcalllist));
rl = malloc(sizeof(*rl));
if (rl == NULL) {
return;
}

View File

@ -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, struct svc_req *rqstp __unused,
/*
* Add it to rlist.
*/
rp = malloc(sizeof(rpcb_entry_list));
rp = malloc(sizeof(*rp));
if (rp == NULL)
goto fail;
a = &rp->rpcb_entry_map;

View File

@ -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);

View File

@ -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 vers, struct netconfig *nconf,
{
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;

View File

@ -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 *serv_uaddr, char *clnt_uaddr,
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");