From: Roy Marples Date: Fri, 15 Feb 2013 22:46:47 +0000 (+0000) Subject: Remove xstrdup X-Git-Tag: v5.99.6~71 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=78369646353546425488e71cd5169f9e5331340e;p=thirdparty%2Fdhcpcd.git Remove xstrdup --- diff --git a/common.c b/common.c index 1010245d..f77c0259 100644 --- a/common.c +++ b/common.c @@ -264,21 +264,3 @@ xmalloc(size_t s) /* NOTREACHED */ } #endif - -#ifndef xstrdup -char * -xstrdup(const char *str) -{ - char *value; - - if (str == NULL) - return NULL; - - if ((value = strdup(str)) != NULL) - return value; - - syslog(LOG_ERR, "memory exhausted (xstrdup)"); - exit(EXIT_FAILURE); - /* NOTREACHED */ -} -#endif diff --git a/common.h b/common.h index 279642b0..ac37b941 100644 --- a/common.h +++ b/common.h @@ -91,6 +91,5 @@ ssize_t setvard(char ***, const char *, const char *, int); time_t uptime(void); int writepid(int, pid_t); void *xmalloc(size_t); -char *xstrdup(const char *); #endif diff --git a/dhcp-common.c b/dhcp-common.c index d8dcf0fd..6a69d272 100644 --- a/dhcp-common.c +++ b/dhcp-common.c @@ -45,7 +45,9 @@ int make_option_mask(const struct dhcp_opt *dopts, const struct dhcp_opt *opt; int match, n; - o = p = xstrdup(opts); + o = p = strdup(opts); + if (opts == NULL) + return -1; while ((token = strsep(&p, ", "))) { if (*token == '\0') continue; diff --git a/dhcp.c b/dhcp.c index c0749808..ee616888 100644 --- a/dhcp.c +++ b/dhcp.c @@ -2015,7 +2015,11 @@ log_dhcp(int lvl, const char *msg, a = get_option_string(dhcp, DHO_MESSAGE); else if (dhcp->yiaddr != 0) { addr.s_addr = dhcp->yiaddr; - a = xstrdup(inet_ntoa(addr)); + a = strdup(inet_ntoa(addr)); + if (a == NULL) { + syslog(LOG_ERR, "%s: %m", __func__); + return; + } } else a = NULL; diff --git a/if-options.c b/if-options.c index 43c7692c..ae63a7dd 100644 --- a/if-options.c +++ b/if-options.c @@ -147,7 +147,11 @@ add_environ(struct if_options *ifo, const char *value, int uniq) size_t i = 0, l, lv; char *match = NULL, *p, *n; - match = xstrdup(value); + match = strdup(value); + if (match == NULL) { + syslog(LOG_ERR, "%s: %m", __func__); + return NULL; + } p = strchr(match, '='); if (p) *p++ = '\0'; @@ -156,8 +160,13 @@ add_environ(struct if_options *ifo, const char *value, int uniq) while (lst && lst[i]) { if (match && strncmp(lst[i], match, l) == 0) { if (uniq) { + n = strdup(value); + if (n == NULL) { + syslog(LOG_ERR, "%s: %m", __func__); + return NULL; + } free(lst[i]); - lst[i] = xstrdup(value); + lst[i] = n; } else { /* Append a space and the value to it */ l = strlen(lst[i]); @@ -178,12 +187,17 @@ add_environ(struct if_options *ifo, const char *value, int uniq) i++; } + n = strdup(value); + if (n == NULL) { + syslog(LOG_ERR, "%s: %m", __func__); + return NULL; + } newlist = realloc(lst, sizeof(char *) * (i + 2)); if (newlist == NULL) { syslog(LOG_ERR, "%s: %m", __func__); return NULL; } - newlist[i] = xstrdup(value); + newlist[i] = n; newlist[i + 1] = NULL; ifo->environ = newlist; free(match); @@ -300,8 +314,12 @@ static char ** splitv(int *argc, char **argv, const char *arg) { char **n, **v = argv; - char *o = xstrdup(arg), *p, *t, *nt; + char *o = strdup(arg), *p, *t, *nt; + if (o == NULL) { + syslog(LOG_ERR, "%s: %m", __func__); + return v; + } p = o; while ((t = strsep(&p, ", "))) { nt = strdup(t); @@ -774,20 +792,31 @@ parse_option(struct if_options *ifo, int opt, const char *arg) if (strncmp(ifo->config[s], arg, p - arg) == 0) { + p = strdup(arg); + if (p == NULL) { + syslog(LOG_ERR, + "%s: %m", __func__); + return -1; + } free(ifo->config[s]); - ifo->config[s] = xstrdup(arg); + ifo->config[s] = p; return 1; } s++; } } - nconf = realloc(ifo->config, sizeof(char *) * (s + 2)); + p = strdup(arg); if (p == NULL) { syslog(LOG_ERR, "%s: %m", __func__); return -1; } + nconf = realloc(ifo->config, sizeof(char *) * (s + 2)); + if (nconf == NULL) { + syslog(LOG_ERR, "%s: %m", __func__); + return -1; + } ifo->config = nconf; - ifo->config[s] = xstrdup(arg); + ifo->config[s] = p; ifo->config[s + 1] = NULL; } break; @@ -857,7 +886,11 @@ parse_option(struct if_options *ifo, int opt, const char *arg) break; case O_FALLBACK: free(ifo->fallback); - ifo->fallback = xstrdup(arg); + ifo->fallback = strdup(arg); + if (ifo->fallback == NULL) { + syslog(LOG_ERR, "%s: %m", __func__); + return -1; + } break; #endif case O_IPV6RS: diff --git a/ipv6rs.c b/ipv6rs.c index a8d60b77..faff75d1 100644 --- a/ipv6rs.c +++ b/ipv6rs.c @@ -661,7 +661,7 @@ ipv6rs_handledata(_unused void *arg) strcpy(opt + l + 1, ap->saddr); } } else - opt = xstrdup(ap->saddr); + opt = strdup(ap->saddr); lifetime = ap->prefix_vltime; break; @@ -675,7 +675,7 @@ ipv6rs_handledata(_unused void *arg) } rap->mtu = mtuv; snprintf(buf, sizeof(buf), "%d", mtuv); - opt = xstrdup(buf); + opt = strdup(buf); break; case ND_OPT_RDNSS: @@ -731,20 +731,26 @@ ipv6rs_handledata(_unused void *arg) syslog(LOG_ERR, "%s: invalid DNSSL option", ifp->name); } else { - tmp = xmalloc(l); - decode_rfc3397(tmp, l, n, op); - n = print_string(NULL, 0, - l - 1, (const uint8_t *)tmp); - opt = xmalloc(n); - print_string(opt, n, - l - 1, (const uint8_t *)tmp); - free(tmp); + tmp = malloc(l); + if (tmp) { + decode_rfc3397(tmp, l, n, op); + n = print_string(NULL, 0, + l - 1, (const uint8_t *)tmp); + opt = malloc(n); + if (opt) + print_string(opt, n, + l - 1, + (const uint8_t *)tmp); + free(tmp); + } } break; } - if (opt == NULL) + if (opt == NULL) { + syslog(LOG_ERR, "%s: %m", __func__); continue; + } TAILQ_FOREACH(rao, &rap->options, next) { if (rao->type == ndo->nd_opt_type && strcmp(rao->option, opt) == 0) diff --git a/platform-linux.c b/platform-linux.c index e42f8dd8..e5d401bd 100644 --- a/platform-linux.c +++ b/platform-linux.c @@ -175,7 +175,7 @@ int check_ipv6(const char *ifname) { int r, ex, i; - char path[256]; + char path[256], *p, **nrest; if (ifname == NULL) { ifname = "all"; @@ -203,18 +203,26 @@ check_ipv6(const char *ifname) if (strcmp(restore[i], ifname) == 0) break; if (i == nrestore) { - restore = realloc(restore, + p = strdup(ifname); + if (p == NULL) { + syslog(LOG_ERR, "%s: %m", __func__); + goto forward; + } + nrest = realloc(restore, (nrestore + 1) * sizeof(char *)); - if (restore == NULL) { - syslog(LOG_ERR, "realloc: %m"); - exit(EXIT_FAILURE); + if (nrest == NULL) { + syslog(LOG_ERR, "%s: %m", __func__); + goto forward; } - restore[nrestore++] = xstrdup(ifname); + restore = nrest; + restore[nrestore++] = p; + } if (ex) atexit(restore_kernel_ra); } +forward: if (r != 2) { snprintf(path, sizeof(path), "%s/%s/forwarding", prefix, ifname); diff --git a/script.c b/script.c index d2e24721..0f9f8440 100644 --- a/script.c +++ b/script.c @@ -382,7 +382,9 @@ dumplease: env = nenv; e = 0; while (ifo->environ[e]) { - env[elen + e] = xstrdup(ifo->environ[e]); + env[elen + e] = strdup(ifo->environ[e]); + if (env[elen + e] == NULL) + goto eexit; e++; } elen += e; @@ -482,8 +484,13 @@ script_runreason(const struct interface *ifp, const char *reason) e = strlen("PATH") + strlen(path) + 2; env[elen] = xmalloc(e); snprintf(env[elen], e, "PATH=%s", path); - } else - env[elen] = xstrdup(DEFAULT_PATH); + } else { + env[elen] = strdup(DEFAULT_PATH); + if (env[elen] == NULL) { + elen = -1; + goto out; + } + } env[++elen] = '\0'; pid = exec_script(argv, env);