diff options
-rw-r--r-- | ChangeLog | 8 | ||||
-rw-r--r-- | nis/nis_creategroup.c | 4 | ||||
-rw-r--r-- | nis/nis_findserv.c | 44 | ||||
-rw-r--r-- | nis/nis_getservlist.c | 18 | ||||
-rw-r--r-- | nis/nis_subr.c | 14 | ||||
-rw-r--r-- | nis/ypclnt.c | 12 |
6 files changed, 58 insertions, 42 deletions
@@ -1,5 +1,13 @@ 2000-04-21 Ulrich Drepper <drepper@redhat.com> + * nis/nis_findserv.c (__nis_findfastest): Improve memory handling. + * nis/nis_print_group_entry.c (nis_print_group_entry): Use alloca + instead of malloc. + * nis/nis_subr.c: Use __builtin_expect. + * nis/ypclnt.c: Likewise. + * nis/nis_getservlist.c: Likewise. + * nis/nis_creategroup.c: Likewise. + * nis/nis_callback.c (__nis_create_callback): Check result of memory allocation. Fix memory leaks. Use __builtin_expect. (__nis_destroy_callback): Fix memory leaks. diff --git a/nis/nis_creategroup.c b/nis/nis_creategroup.c index 6d4af65..9168655 100644 --- a/nis/nis_creategroup.c +++ b/nis/nis_creategroup.c @@ -1,4 +1,4 @@ -/* Copyright (c) 1997, 1998, 1999 Free Software Foundation, Inc. +/* Copyright (c) 1997, 1998, 1999, 2000 Free Software Foundation, Inc. This file is part of the GNU C Library. Contributed by Thorsten Kukuk <kukuk@suse.de>, 1997. @@ -47,7 +47,7 @@ nis_creategroup (const_nis_name group, unsigned int flags) return NIS_BADNAME; obj = malloc (sizeof (nis_object)); - if (obj == NULL) + if (__builtin_expect (obj == NULL, 0)) return NIS_NOMEMORY; obj->zo_oid.ctime = obj->zo_oid.mtime = time (NULL); diff --git a/nis/nis_findserv.c b/nis/nis_findserv.c index 5b6a749..2659878 100644 --- a/nis/nis_findserv.c +++ b/nis/nis_findserv.c @@ -1,4 +1,4 @@ -/* Copyright (C) 1997, 1998 Free Software Foundation, Inc. +/* Copyright (C) 1997, 1998, 2000 Free Software Foundation, Inc. This file is part of the GNU C Library. Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.de>, 1997. @@ -131,7 +131,7 @@ __nis_findfastest (dir_binding *bind) #else const struct timeval TIMEOUT50 = {5, 0}; const struct timeval TIMEOUT00 = {0, 0}; - struct findserv_req **pings; + struct findserv_req *pings; struct sockaddr_in sin, saved_sin; int found = -1; u_int32_t xid_seed, xid_lookup; @@ -145,9 +145,12 @@ __nis_findfastest (dir_binding *bind) pings_max = bind->server_len * 2; /* Reserve a little bit more memory for multihomed hosts */ pings_count = 0; - pings = malloc (sizeof (struct findserv_req *) * pings_max); + pings = malloc (sizeof (struct findserv_req) * pings_max); xid_seed = (u_int32_t) (time (NULL) ^ getpid ()); + if (__builtin_expect (pings == NULL, 0)) + return -1; + memset (&sin, '\0', sizeof (sin)); sin.sin_family = AF_INET; for (i = 0; i < bind->server_len; i++) @@ -169,17 +172,24 @@ __nis_findfastest (dir_binding *bind) if (pings_count >= pings_max) { + struct findserv_req *new_pings; + pings_max += 10; - pings = realloc (pings, sizeof (struct findserv_req) * - pings_max); + new_pings = realloc (pings, sizeof (struct findserv_req) * + pings_max); + if (__builtin_expect (new_pings == NULL, 0)) + { + free (pings); + return -1; + } + pings = new_pings; } - pings[pings_count] = calloc (1, sizeof (struct findserv_req)); - memcpy ((char *) &pings[pings_count]->sin, (char *) &sin, + memcpy ((char *) &pings[pings_count].sin, (char *) &sin, sizeof (sin)); memcpy ((char *)&saved_sin, (char *)&sin, sizeof(sin)); - pings[pings_count]->xid = xid_seed; - pings[pings_count]->server_nr = i; - pings[pings_count]->server_ep = j; + pings[pings_count].xid = xid_seed; + pings[pings_count].server_nr = i; + pings[pings_count].server_ep = j; ++xid_seed; ++pings_count; } @@ -197,8 +207,6 @@ __nis_findfastest (dir_binding *bind) if (clnt == NULL) { close (sock); - for (i = 0; i < pings_count; ++i) - free (pings[i]); free (pings); return -1; } @@ -212,8 +220,8 @@ __nis_findfastest (dir_binding *bind) for (i = 0; i < pings_count; ++i) { /* clntudp_call() will increment, subtract one */ - *((u_int32_t *) (cu->cu_outbuf)) = pings[i]->xid - 1; - memcpy ((char *) &cu->cu_raddr, (char *) &pings[i]->sin, + *((u_int32_t *) (cu->cu_outbuf)) = pings[i].xid - 1; + memcpy ((char *) &cu->cu_raddr, (char *) &pings[i].sin, sizeof (struct sockaddr_in)); /* Transmit to NULLPROC, return immediately. */ clnt_call (clnt, NULLPROC, (xdrproc_t) xdr_void, (caddr_t) foo, @@ -228,10 +236,10 @@ __nis_findfastest (dir_binding *bind) xid_lookup = *((u_int32_t *) (cu->cu_inbuf)); for (i = 0; i < pings_count; i++) { - if (pings[i]->xid == xid_lookup) + if (pings[i].xid == xid_lookup) { - bind->server_used = pings[i]->server_nr; - bind->current_ep = pings[i]->server_ep; + bind->server_used = pings[i].server_nr; + bind->current_ep = pings[i].server_ep; found = 1; } } @@ -240,8 +248,6 @@ __nis_findfastest (dir_binding *bind) clnt_destroy (clnt); close (sock); - for (i = 0; i < pings_count; ++i) - free (pings[i]); free (pings); return found; diff --git a/nis/nis_getservlist.c b/nis/nis_getservlist.c index 5d93f38..0b4096a 100644 --- a/nis/nis_getservlist.c +++ b/nis/nis_getservlist.c @@ -1,4 +1,4 @@ -/* Copyright (c) 1997, 1998, 1999 Free Software Foundation, Inc. +/* Copyright (c) 1997, 1998, 1999, 2000 Free Software Foundation, Inc. This file is part of the GNU C Library. Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.de>, 1997. @@ -39,7 +39,7 @@ nis_getservlist (const_nis_name dir) serv = malloc (sizeof (nis_server *) * (NIS_RES_OBJECT (res)->DI_data.do_servers.do_servers_len + 1)); - if (serv == NULL) + if (__builtin_expect (serv == NULL, 0)) return NULL; for (i = 0; i < NIS_RES_OBJECT (res)->DI_data.do_servers.do_servers_len; @@ -48,10 +48,13 @@ nis_getservlist (const_nis_name dir) server = &NIS_RES_OBJECT (res)->DI_data.do_servers.do_servers_val[i]; serv[i] = calloc (1, sizeof (nis_server)); + if (__builtin_expect (serv[i] == NULL, 0)) + return NULL; + if (server->name != NULL) { serv[i]->name = strdup (server->name); - if (serv[i]->name == NULL) + if (__builtin_expect (serv[i]->name == NULL, 0)) return NULL; } @@ -62,7 +65,7 @@ nis_getservlist (const_nis_name dir) serv[i]->ep.ep_val = malloc (server->ep.ep_len * sizeof (endpoint)); - if (serv[i]->ep.ep_val == NULL) + if (__builtin_expect (serv[i]->ep.ep_val == NULL, 0)) return NULL; for (j = 0; j < serv[i]->ep.ep_len; ++j) @@ -90,9 +93,8 @@ nis_getservlist (const_nis_name dir) serv[i]->pkey.n_len = server->pkey.n_len; if (server->pkey.n_len > 0) { - serv[i]->pkey.n_bytes = - malloc (server->pkey.n_len); - if (serv[i]->pkey.n_bytes == NULL) + serv[i]->pkey.n_bytes = malloc (server->pkey.n_len); + if (__builtin_expect (serv[i]->pkey.n_bytes == NULL, 0)) return NULL; memcpy (serv[i]->pkey.n_bytes, server->pkey.n_bytes, server->pkey.n_len); @@ -105,7 +107,7 @@ nis_getservlist (const_nis_name dir) else { serv = malloc (sizeof (nis_server *)); - if (serv != NULL) + if (__builtin_expect (serv != NULL, 0)) serv[0] = NULL; } diff --git a/nis/nis_subr.c b/nis/nis_subr.c index c7d58a6..a5ddf03 100644 --- a/nis/nis_subr.c +++ b/nis/nis_subr.c @@ -1,4 +1,4 @@ -/* Copyright (c) 1997, 1999 Free Software Foundation, Inc. +/* Copyright (c) 1997, 1999, 2000 Free Software Foundation, Inc. This file is part of the GNU C Library. Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.de>, 1997. @@ -126,7 +126,7 @@ nis_getnames (const_nis_name name) count = 1; getnames = malloc ((count + 1) * sizeof (char *)); - if (getnames == NULL) + if (__builtin_expect (getnames == NULL, 0)) return NULL; /* Do we have a fully qualified NIS+ name ? If yes, give it back */ @@ -165,12 +165,12 @@ nis_getnames (const_nis_name name) { count += 5; getnames = realloc (getnames, (count + 1) * sizeof (char *)); - if (getnames == NULL) + if (__builtin_expect (getnames == NULL, 0)) return NULL; } tmp = malloc (strlen (cptr) + strlen (local_domain) + strlen (name) + 2); - if (tmp == NULL) + if (__builtin_expect (tmp == NULL, 0)) return NULL; getnames[pos] = tmp; @@ -200,7 +200,7 @@ nis_getnames (const_nis_name name) char *p; tmp = malloc (cplen + strlen (local_domain) + strlen (name) + 2); - if (tmp == NULL) + if (__builtin_expect (tmp == NULL, 0)) return NULL; p = __stpcpy (tmp, name); @@ -216,7 +216,7 @@ nis_getnames (const_nis_name name) char *p; tmp = malloc (cplen + strlen (name) + 2); - if (tmp == NULL) + if (__builtin_expect (tmp == NULL, 0)) return NULL; p = __stpcpy (tmp, name); @@ -228,7 +228,7 @@ nis_getnames (const_nis_name name) { count += 5; getnames = realloc (getnames, (count + 1) * sizeof (char *)); - if (getnames == NULL) + if (__builtin_expect (getnames == NULL, 0)) return NULL; } getnames[pos] = tmp; diff --git a/nis/ypclnt.c b/nis/ypclnt.c index 637afcc..a62e356 100644 --- a/nis/ypclnt.c +++ b/nis/ypclnt.c @@ -81,7 +81,7 @@ __yp_bind (const char *domain, dom_binding **ypdb) { is_new = 1; ysd = (dom_binding *) calloc (1, sizeof *ysd); - if (ysd == NULL) + if (__builtin_expect (ysd == NULL, 0)) return YPERR_RESRC; } @@ -448,7 +448,7 @@ yp_match (const char *indomain, const char *inmap, const char *inkey, *outvallen = resp.val.valdat_len; *outval = malloc (*outvallen + 1); - if (*outval == NULL) + if (__builtin_expect (*outval == NULL, 0)) return YPERR_RESRC; memcpy (*outval, resp.val.valdat_val, *outvallen); (*outval)[*outvallen] = '\0'; @@ -488,13 +488,13 @@ yp_first (const char *indomain, const char *inmap, char **outkey, *outkeylen = resp.key.keydat_len; *outkey = malloc (*outkeylen + 1); - if (*outkey == NULL) + if (__builtin_expect (*outkey == NULL, 0)) return YPERR_RESRC; memcpy (*outkey, resp.key.keydat_val, *outkeylen); (*outkey)[*outkeylen] = '\0'; *outvallen = resp.val.valdat_len; *outval = malloc (*outvallen + 1); - if (*outval == NULL) + if (__builtin_expect (*outval == NULL, 0)) return YPERR_RESRC; memcpy (*outval, resp.val.valdat_val, *outvallen); (*outval)[*outvallen] = '\0'; @@ -538,13 +538,13 @@ yp_next (const char *indomain, const char *inmap, const char *inkey, *outkeylen = resp.key.keydat_len; *outkey = malloc (*outkeylen + 1); - if (*outkey == NULL) + if (__builtin_expect (*outkey == NULL, 0)) return YPERR_RESRC; memcpy (*outkey, resp.key.keydat_val, *outkeylen); (*outkey)[*outkeylen] = '\0'; *outvallen = resp.val.valdat_len; *outval = malloc (*outvallen + 1); - if (*outval == NULL) + if (__builtin_expect (*outval == NULL, 0)) return YPERR_RESRC; memcpy (*outval, resp.val.valdat_val, *outvallen); (*outval)[*outvallen] = '\0'; |