svn commit: r190659 - in stable/6/lib/libc: . gen inet rpc sys

Xin LI delphij at FreeBSD.org
Thu Apr 2 14:58:38 PDT 2009


Author: delphij
Date: Thu Apr  2 21:58:34 2009
New Revision: 190659
URL: http://svn.freebsd.org/changeset/base/190659

Log:
  MFC revisions 162191, 166134, and 172259:
  
  Avoid memory leaks, properly handle malloc/realloc failures,
  and remove duplicate includes.

Modified:
  stable/6/lib/libc/   (props changed)
  stable/6/lib/libc/gen/siglist.c
  stable/6/lib/libc/inet/inet_net_pton.c   (props changed)
  stable/6/lib/libc/rpc/auth_time.c
  stable/6/lib/libc/rpc/getnetconfig.c
  stable/6/lib/libc/rpc/getnetpath.c
  stable/6/lib/libc/rpc/rpc_generic.c
  stable/6/lib/libc/rpc/rpcb_clnt.c
  stable/6/lib/libc/sys/   (props changed)

Modified: stable/6/lib/libc/gen/siglist.c
==============================================================================
--- stable/6/lib/libc/gen/siglist.c	Thu Apr  2 21:51:54 2009	(r190658)
+++ stable/6/lib/libc/gen/siglist.c	Thu Apr  2 21:58:34 2009	(r190659)
@@ -37,7 +37,6 @@ static char sccsid[] = "@(#)siglist.c	8.
 #include <sys/cdefs.h>
 __FBSDID("$FreeBSD$");
 
-#include <sys/cdefs.h>
 #include <signal.h>
 
 const char *const sys_signame[NSIG] = {

Modified: stable/6/lib/libc/rpc/auth_time.c
==============================================================================
--- stable/6/lib/libc/rpc/auth_time.c	Thu Apr  2 21:51:54 2009	(r190658)
+++ stable/6/lib/libc/rpc/auth_time.c	Thu Apr  2 21:58:34 2009	(r190659)
@@ -156,6 +156,7 @@ get_server(sin, host, srv, eps, maxep)
 	struct hostent		*he;
 	struct hostent		dummy;
 	char			*ptr[2];
+	endpoint		*ep;
 
 	if (host == NULL && sin == NULL)
 		return (NULL);
@@ -175,26 +176,34 @@ get_server(sin, host, srv, eps, maxep)
 	 * This is lame. We go around once for TCP, then again
 	 * for UDP.
 	 */
-	for (i = 0; (he->h_addr_list[i] != NULL) && (num_ep < maxep);
-						i++, num_ep++) {
+	for (i = 0, ep = eps; (he->h_addr_list[i] != NULL) && (num_ep < maxep);
+	    i++, ep++, num_ep++) {
 		struct in_addr *a;
 
 		a = (struct in_addr *)he->h_addr_list[i];
 		snprintf(hname, sizeof(hname), "%s.0.111", inet_ntoa(*a));
-		eps[num_ep].uaddr = strdup(hname);
-		eps[num_ep].family = strdup("inet");
-		eps[num_ep].proto =  strdup("tcp");
+		ep->uaddr = strdup(hname);
+		ep->family = strdup("inet");
+		ep->proto =  strdup("tcp");
+		if (ep->uaddr == NULL || ep->family == NULL || ep->proto == NULL) {
+			free_eps(eps, num_ep + 1);
+			return (NULL);
+		}
 	}
 
 	for (i = 0; (he->h_addr_list[i] != NULL) && (num_ep < maxep);
-						i++, num_ep++) {
+	    i++, ep++, num_ep++) {
 		struct in_addr *a;
 
 		a = (struct in_addr *)he->h_addr_list[i];
 		snprintf(hname, sizeof(hname), "%s.0.111", inet_ntoa(*a));
-		eps[num_ep].uaddr = strdup(hname);
-		eps[num_ep].family = strdup("inet");
-		eps[num_ep].proto =  strdup("udp");
+		ep->uaddr = strdup(hname);
+		ep->family = strdup("inet");
+		ep->proto =  strdup("udp");
+		if (ep->uaddr == NULL || ep->family == NULL || ep->proto == NULL) {
+			free_eps(eps, num_ep + 1);
+			return (NULL);
+		}
 	}
 
 	srv->name = (nis_name) host;

Modified: stable/6/lib/libc/rpc/getnetconfig.c
==============================================================================
--- stable/6/lib/libc/rpc/getnetconfig.c	Thu Apr  2 21:51:54 2009	(r190658)
+++ stable/6/lib/libc/rpc/getnetconfig.c	Thu Apr  2 21:58:34 2009	(r190659)
@@ -42,7 +42,6 @@ __FBSDID("$FreeBSD$");
 
 #include "namespace.h"
 #include "reentrant.h"
-#include <sys/cdefs.h>
 #include <stdio.h>
 #include <errno.h>
 #include <netconfig.h>
@@ -535,6 +534,7 @@ struct netconfig *ncp;	/* where to put r
 {
     char    *tokenp;	/* for processing tokens */
     char    *lasts;
+    char    **nc_lookups;
 
     nc_error = NC_BADFILE;	/* nearly anything that breaks is for this reason */
     stringp[strlen(stringp)-1] = '\0';	/* get rid of newline */
@@ -600,14 +600,18 @@ struct netconfig *ncp;	/* where to put r
 
 	if (ncp->nc_lookups != NULL)	/* from last visit */
 	    free(ncp->nc_lookups);
-	/* preallocate one string pointer */
-	ncp->nc_lookups = (char **)malloc(sizeof (char *));
+	ncp->nc_lookups = NULL;
 	ncp->nc_nlookups = 0;
 	while ((cp = tokenp) != NULL) {
+	    if ((nc_lookups = realloc(ncp->nc_lookups,
+		(ncp->nc_nlookups + 1) * sizeof *ncp->nc_lookups)) == NULL) {
+		    free(ncp->nc_lookups);
+		    ncp->nc_lookups = NULL;
+		    return (-1);
+	    }
 	    tokenp = _get_next_token(cp, ',');
-	    ncp->nc_lookups[(size_t)ncp->nc_nlookups++] = cp;
-	    ncp->nc_lookups = (char **)realloc(ncp->nc_lookups,
-		(size_t)(ncp->nc_nlookups+1) *sizeof(char *));	/* for next loop */
+	    ncp->nc_lookups = nc_lookups;
+	    ncp->nc_lookups[ncp->nc_nlookups++] = cp;
 	}
     }
     return (0);
@@ -692,6 +696,7 @@ struct netconfig	*ncp;
     p->nc_lookups = (char **)malloc((size_t)(p->nc_nlookups+1) * sizeof(char *));
     if (p->nc_lookups == NULL) {
 	free(p->nc_netid);
+	free(p);
 	return(NULL);
     }
     for (i=0; i < p->nc_nlookups; i++) {

Modified: stable/6/lib/libc/rpc/getnetpath.c
==============================================================================
--- stable/6/lib/libc/rpc/getnetpath.c	Thu Apr  2 21:51:54 2009	(r190658)
+++ stable/6/lib/libc/rpc/getnetpath.c	Thu Apr  2 21:58:34 2009	(r190659)
@@ -41,7 +41,6 @@ __FBSDID("$FreeBSD$");
  */
 
 #include "namespace.h"
-#include <sys/cdefs.h>
 #include <stdio.h>
 #include <errno.h>
 #include <netconfig.h>
@@ -101,7 +100,7 @@ setnetpath()
     }
     if ((np_sessionp->nc_handlep = setnetconfig()) == NULL) {
 	syslog (LOG_ERR, "rpc: failed to open " NETCONFIG);
-	return (NULL);
+	goto failed;
     }
     np_sessionp->valid = NP_VALID;
     np_sessionp->ncp_list = NULL;
@@ -110,15 +109,18 @@ setnetpath()
     } else {
 	(void) endnetconfig(np_sessionp->nc_handlep);/* won't need nc session*/
 	np_sessionp->nc_handlep = NULL;
-	if ((np_sessionp->netpath = malloc(strlen(npp)+1)) == NULL) {
-	    free(np_sessionp);
-	    return (NULL);
-	} else {
+	if ((np_sessionp->netpath = malloc(strlen(npp)+1)) == NULL)
+		goto failed;
+	else {
 	    (void) strcpy(np_sessionp->netpath, npp);
 	}
     }
     np_sessionp->netpath_start = np_sessionp->netpath;
     return ((void *)np_sessionp);
+
+failed:
+    free(np_sessionp);
+    return (NULL);
 }
 
 /*

Modified: stable/6/lib/libc/rpc/rpc_generic.c
==============================================================================
--- stable/6/lib/libc/rpc/rpc_generic.c	Thu Apr  2 21:51:54 2009	(r190658)
+++ stable/6/lib/libc/rpc/rpc_generic.c	Thu Apr  2 21:58:34 2009	(r190659)
@@ -319,10 +319,8 @@ __rpc_setconf(nettype)
 	case _RPC_NETPATH:
 	case _RPC_CIRCUIT_N:
 	case _RPC_DATAGRAM_N:
-		if (!(handle->nhandle = setnetpath())) {
-			free(handle);
-			return (NULL);
-		}
+		if (!(handle->nhandle = setnetpath()))
+			goto failed;
 		handle->nflag = TRUE;
 		break;
 	case _RPC_VISIBLE:
@@ -332,16 +330,19 @@ __rpc_setconf(nettype)
 	case _RPC_UDP:
 		if (!(handle->nhandle = setnetconfig())) {
 		        syslog (LOG_ERR, "rpc: failed to open " NETCONFIG);
-			free(handle);
-			return (NULL);
+			goto failed;
 		}
 		handle->nflag = FALSE;
 		break;
 	default:
-		return (NULL);
+		goto failed;
 	}
 
 	return (handle);
+
+failed:
+	free(handle);
+	return (NULL);
 }
 
 /*

Modified: stable/6/lib/libc/rpc/rpcb_clnt.c
==============================================================================
--- stable/6/lib/libc/rpc/rpcb_clnt.c	Thu Apr  2 21:51:54 2009	(r190658)
+++ stable/6/lib/libc/rpc/rpcb_clnt.c	Thu Apr  2 21:58:34 2009	(r190659)
@@ -374,10 +374,15 @@ getclnthandle(host, nconf, targaddr)
 			return (NULL);
 		} else {
 			struct sockaddr_un sun;
-
-			*targaddr = malloc(sizeof(sun.sun_path));
-			strncpy(*targaddr, _PATH_RPCBINDSOCK,
-			    sizeof(sun.sun_path));
+			if (targaddr) {
+			    *targaddr = malloc(sizeof(sun.sun_path));
+			    if (*targaddr == NULL) {
+				CLNT_DESTROY(client);
+				return (NULL);
+			    }
+			    strncpy(*targaddr, _PATH_RPCBINDSOCK,
+				sizeof(sun.sun_path));
+			}
 			return (client);
 		}
 	} else {


More information about the svn-src-stable mailing list