From: Roy Marples Date: Fri, 15 Feb 2013 22:20:18 +0000 (+0000) Subject: Remove xrealloc X-Git-Tag: v5.99.6~72 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=fa245a4dea8dd4425d14b2602b3c68f133faee15;p=thirdparty%2Fdhcpcd.git Remove xrealloc --- diff --git a/common.c b/common.c index a3e9aee6..1010245d 100644 --- a/common.c +++ b/common.c @@ -265,20 +265,6 @@ xmalloc(size_t s) } #endif -#ifndef xrealloc -void * -xrealloc(void *ptr, size_t s) -{ - void *value = realloc(ptr, s); - - if (value != NULL) - return value; - syslog(LOG_ERR, "memory exhausted (xrealloc %zu bytes)", s); - exit(EXIT_FAILURE); - /* NOTREACHED */ -} -#endif - #ifndef xstrdup char * xstrdup(const char *str) diff --git a/common.h b/common.h index af390937..279642b0 100644 --- a/common.h +++ b/common.h @@ -90,7 +90,6 @@ ssize_t setvar(char ***, const char *, const char *, const char *); ssize_t setvard(char ***, const char *, const char *, int); time_t uptime(void); int writepid(int, pid_t); -void *xrealloc(void *, size_t); void *xmalloc(size_t); char *xstrdup(const char *); diff --git a/if-options.c b/if-options.c index 47a1a514..43c7692c 100644 --- a/if-options.c +++ b/if-options.c @@ -145,7 +145,7 @@ add_environ(struct if_options *ifo, const char *value, int uniq) char **newlist; char **lst = ifo->environ; size_t i = 0, l, lv; - char *match = NULL, *p; + char *match = NULL, *p, *n; match = xstrdup(value); p = strchr(match, '='); @@ -162,7 +162,12 @@ add_environ(struct if_options *ifo, const char *value, int uniq) /* Append a space and the value to it */ l = strlen(lst[i]); lv = strlen(p); - lst[i] = xrealloc(lst[i], l + lv + 2); + n = realloc(lst[i], l + lv + 2); + if (n == NULL) { + syslog(LOG_ERR, "%s: %m", __func__); + return NULL; + } + lst[i] = n; lst[i][l] = ' '; memcpy(lst[i] + l + 1, p, lv); lst[i][l + lv + 1] = '\0'; @@ -173,7 +178,11 @@ add_environ(struct if_options *ifo, const char *value, int uniq) i++; } - newlist = xrealloc(lst, sizeof(char *) * (i + 2)); + newlist = realloc(lst, sizeof(char *) * (i + 2)); + if (newlist == NULL) { + syslog(LOG_ERR, "%s: %m", __func__); + return NULL; + } newlist[i] = xstrdup(value); newlist[i + 1] = NULL; ifo->environ = newlist; @@ -290,14 +299,24 @@ parse_string_hwaddr(char *sbuf, ssize_t slen, const char *str, int clid) static char ** splitv(int *argc, char **argv, const char *arg) { - char **v = argv; - char *o = xstrdup(arg), *p, *t; + char **n, **v = argv; + char *o = xstrdup(arg), *p, *t, *nt; p = o; while ((t = strsep(&p, ", "))) { + nt = strdup(t); + if (nt == NULL) { + syslog(LOG_ERR, "%s: %m", __func__); + return NULL; + } (*argc)++; - v = xrealloc(v, sizeof(char *) * ((*argc))); - v[(*argc) - 1] = xstrdup(t); + n = realloc(v, sizeof(char *) * ((*argc))); + if (n == NULL) { + syslog(LOG_ERR, "%s: %m", __func__); + return NULL; + } + v = n; + v[(*argc) - 1] = nt; } free(o); return v; @@ -374,9 +393,10 @@ static int parse_option(struct if_options *ifo, int opt, const char *arg) { int i; - char *p = NULL, *fp, *np; + char *p = NULL, *fp, *np, **nconf; ssize_t s; struct in_addr addr, addr2; + in_addr_t *naddr; struct rt *rt; const struct dhcp_opt const *d; uint8_t *request, *require, *no; @@ -761,8 +781,12 @@ parse_option(struct if_options *ifo, int opt, const char *arg) s++; } } - ifo->config = xrealloc(ifo->config, - sizeof(char *) * (s + 2)); + nconf = realloc(ifo->config, sizeof(char *) * (s + 2)); + if (p == NULL) { + syslog(LOG_ERR, "%s: %m", __func__); + return -1; + } + ifo->config = nconf; ifo->config[s] = xstrdup(arg); ifo->config[s + 1] = NULL; } @@ -772,8 +796,13 @@ parse_option(struct if_options *ifo, int opt, const char *arg) return -1; if (strchr(arg, '/') == NULL) addr2.s_addr = INADDR_BROADCAST; - ifo->whitelist = xrealloc(ifo->whitelist, + naddr = realloc(ifo->whitelist, sizeof(in_addr_t) * (ifo->whitelist_len + 2)); + if (naddr == NULL) { + syslog(LOG_ERR, "%s: %m", __func__); + return -1; + } + ifo->whitelist = naddr; ifo->whitelist[ifo->whitelist_len++] = addr.s_addr; ifo->whitelist[ifo->whitelist_len++] = addr2.s_addr; break; @@ -782,8 +811,13 @@ parse_option(struct if_options *ifo, int opt, const char *arg) return -1; if (strchr(arg, '/') == NULL) addr2.s_addr = INADDR_BROADCAST; - ifo->blacklist = xrealloc(ifo->blacklist, + naddr = realloc(ifo->blacklist, sizeof(in_addr_t) * (ifo->blacklist_len + 2)); + if (naddr == NULL) { + syslog(LOG_ERR, "%s: %m", __func__); + return -1; + } + ifo->blacklist = naddr; ifo->blacklist[ifo->blacklist_len++] = addr.s_addr; ifo->blacklist[ifo->blacklist_len++] = addr2.s_addr; break; @@ -802,8 +836,13 @@ parse_option(struct if_options *ifo, int opt, const char *arg) case O_ARPING: if (parse_addr(&addr, NULL, arg) != 0) return -1; - ifo->arping = xrealloc(ifo->arping, + naddr = realloc(ifo->arping, sizeof(in_addr_t) * (ifo->arping_len + 1)); + if (naddr == NULL) { + syslog(LOG_ERR, "%s: %m", __func__); + return -1; + } + ifo->arping = naddr; ifo->arping[ifo->arping_len++] = addr.s_addr; break; case O_DESTINATION: diff --git a/ipv6rs.c b/ipv6rs.c index a1bd9d14..a8d60b77 100644 --- a/ipv6rs.c +++ b/ipv6rs.c @@ -653,10 +653,13 @@ ipv6rs_handledata(_unused void *arg) ntohl(pi->nd_opt_pi_preferred_time); if (opt) { l = strlen(opt); - opt = xrealloc(opt, + tmp = realloc(opt, l + strlen(ap->saddr) + 2); - opt[l] = ' '; - strcpy(opt + l + 1, ap->saddr); + if (tmp) { + opt = tmp; + opt[l] = ' '; + strcpy(opt + l + 1, ap->saddr); + } } else opt = xstrdup(ap->saddr); lifetime = ap->prefix_vltime; diff --git a/script.c b/script.c index 7240dde9..d2e24721 100644 --- a/script.c +++ b/script.c @@ -125,7 +125,7 @@ append_config(char ***env, ssize_t *len, const char *prefix, const char *const *config) { ssize_t i, j, e1; - char **ne, *eq; + char **ne, *eq, **nep; if (config == NULL) return; @@ -145,7 +145,12 @@ append_config(char ***env, ssize_t *len, } if (j == *len) { j++; - ne = xrealloc(ne, sizeof(char *) * (j + 1)); + nep = realloc(ne, sizeof(char *) * (j + 1)); + if (nep == NULL) { + syslog(LOG_ERR, "%s: %m", __func__); + break; + } + ne = nep; ne[j - 1] = make_var(prefix, config[i]); *len = j; } @@ -179,7 +184,7 @@ arraytostr(const char *const *argv, char **s) static ssize_t make_env(const struct interface *ifp, const char *reason, char ***argv) { - char **env, *p; + char **env, **nenv, *p; ssize_t e, elen, l; const struct if_options *ifo = ifp->options; const struct interface *ifp2; @@ -218,7 +223,9 @@ make_env(const struct interface *ifp, const char *reason, char ***argv) elen = 10; /* Make our env */ - env = xmalloc(sizeof(char *) * (elen + 1)); + env = calloc(1, sizeof(char *) * (elen + 1)); + if (env == NULL) + goto eexit; e = strlen("interface") + strlen(ifp->name) + 2; env[0] = xmalloc(e); snprintf(env[0], e, "interface=%s", ifp->name); @@ -278,12 +285,18 @@ make_env(const struct interface *ifp, const char *reason, char ***argv) if (ifp->wireless) { e = strlen("new_ssid=") + strlen(ifp->ssid) + 2; if (strcmp(reason, "CARRIER") == 0) { - env = xrealloc(env, sizeof(char *) * (elen + 2)); + nenv = realloc(env, sizeof(char *) * (elen + 2)); + if (nenv == NULL) + goto eexit; + env = nenv; env[elen] = xmalloc(e); snprintf(env[elen++], e, "new_ssid=%s", ifp->ssid); } else if (strcmp(reason, "NOCARRIER") == 0) { - env = xrealloc(env, sizeof(char *) * (elen + 2)); + nenv = realloc(env, sizeof(char *) * (elen + 2)); + if (nenv == NULL) + goto eexit; + env = nenv; env[elen] = xmalloc(e); snprintf(env[elen++], e, "old_ssid=%s", ifp->ssid); } @@ -292,9 +305,11 @@ make_env(const struct interface *ifp, const char *reason, char ***argv) if (dhcp && state && state->old) { e = dhcp_env(NULL, NULL, state->old, ifp); if (e > 0) { - env = xrealloc(env, sizeof(char *) * (elen + e + 1)); - elen += dhcp_env(env + elen, "old", - state->old, ifp); + nenv = realloc(env, sizeof(char *) * (elen + e + 1)); + if (nenv == NULL) + goto eexit; + env = nenv; + elen += dhcp_env(env + elen, "old", state->old, ifp); } append_config(&env, &elen, "old", (const char *const *)ifo->config); @@ -305,7 +320,10 @@ make_env(const struct interface *ifp, const char *reason, char ***argv) e = dhcp6_env(NULL, NULL, ifp, d6_state->old, d6_state->old_len); if (e > 0) { - env = xrealloc(env, sizeof(char *) * (elen + e + 1)); + nenv = realloc(env, sizeof(char *) * (elen + e + 1)); + if (nenv == NULL) + goto eexit; + env = nenv; elen += dhcp6_env(env + elen, "old", ifp, d6_state->old, d6_state->old_len); } @@ -317,7 +335,10 @@ dumplease: if (dhcp && state && state->new) { e = dhcp_env(NULL, NULL, state->new, ifp); if (e > 0) { - env = xrealloc(env, sizeof(char *) * (elen + e + 1)); + nenv = realloc(env, sizeof(char *) * (elen + e + 1)); + if (nenv == NULL) + goto eexit; + env = nenv; elen += dhcp_env(env + elen, "new", state->new, ifp); } @@ -330,7 +351,10 @@ dumplease: e = dhcp6_env(NULL, NULL, ifp, d6_state->new, d6_state->new_len); if (e > 0) { - env = xrealloc(env, sizeof(char *) * (elen + e + 1)); + nenv = realloc(env, sizeof(char *) * (elen + e + 1)); + if (nenv == NULL) + goto eexit; + env = nenv; elen += dhcp6_env(env + elen, "new", ifp, d6_state->new, d6_state->new_len); } @@ -338,7 +362,10 @@ dumplease: if (ra) { e = ipv6rs_env(NULL, NULL, ifp); if (e > 0) { - env = xrealloc(env, sizeof(char *) * (elen + e + 1)); + nenv = realloc(env, sizeof(char *) * (elen + e + 1)); + if (nenv == NULL) + goto eexit; + env = nenv; elen += ipv6rs_env(env + elen, NULL, ifp); } } @@ -349,7 +376,10 @@ dumplease: e = 0; while (ifo->environ[e++]) ; - env = xrealloc(env, sizeof(char *) * (elen + e + 1)); + nenv = realloc(env, sizeof(char *) * (elen + e + 1)); + if (nenv == NULL) + goto eexit; + env = nenv; e = 0; while (ifo->environ[e]) { env[elen + e] = xstrdup(ifo->environ[e]); @@ -361,6 +391,14 @@ dumplease: *argv = env; return elen; + +eexit: + syslog(LOG_ERR, "%s: %m", __func__); + nenv = env; + while (*nenv) + free(*nenv++); + free(env); + return -1; } static int @@ -432,7 +470,12 @@ script_runreason(const struct interface *ifp, const char *reason) /* Make our env */ elen = make_env(ifp, reason, &env); - env = xrealloc(env, sizeof(char *) * (elen + 2)); + ep = realloc(env, sizeof(char *) * (elen + 2)); + if (ep == NULL) { + elen = -1; + goto out; + } + env = ep; /* Add path to it */ path = getenv("PATH"); if (path) { @@ -483,10 +526,13 @@ script_runreason(const struct interface *ifp, const char *reason) } free(bigenv); +out: /* Cleanup */ ep = env; while (*ep) free(*ep++); free(env); + if (elen == -1) + return -1; return WEXITSTATUS(status); }