]> git.ipfire.org Git - thirdparty/dhcpcd.git/commitdiff
Remove xstrdup
authorRoy Marples <roy@marples.name>
Fri, 15 Feb 2013 22:46:47 +0000 (22:46 +0000)
committerRoy Marples <roy@marples.name>
Fri, 15 Feb 2013 22:46:47 +0000 (22:46 +0000)
common.c
common.h
dhcp-common.c
dhcp.c
if-options.c
ipv6rs.c
platform-linux.c
script.c

index 1010245d9276212e866f20c2c1b33d5b9d1c765b..f77c02598c210fb7249a7714b840d669383209df 100644 (file)
--- 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
index 279642b0e5708b9f0dcb39a67fb4e9568b5f5a1f..ac37b941fc06e5c9ee1c64f6674753ec335a998d 100644 (file)
--- 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
index d8dcf0fd071bb9a47c65c5acfd4cfa87a3fd6084..6a69d27270ffe702eebeae1c10c422abe03b1398 100644 (file)
@@ -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 c0749808be09f93623c08b7547f0408719bdb13e..ee61688814f34ed3c599fe51042c3a518c723fe0 100644 (file)
--- 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;
 
index 43c7692c2dc0c8d701c3a69d23a3e6465832ac2b..ae63a7dd81d9933b049f734765c6b78430ecdc75 100644 (file)
@@ -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:
index a8d60b7750e90a6edeb3d22c39bec94dbbbfd3ee..faff75d1789c91e67a73037a8e0880d0998b89d0 100644 (file)
--- 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)
index e42f8dd89e6bc49a41b220813846e82192468a87..e5d401bd435246537c53fc9829ab49387072888d 100644 (file)
@@ -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);
index d2e24721e0fb8e1765966e21b814a4eca1b6576b..0f9f84408bd436e716e6aba7d827a4a8f93f4b6c 100644 (file)
--- 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);