]> git.ipfire.org Git - thirdparty/dhcpcd.git/commitdiff
Remove the xmalloc function.
authorRoy Marples <roy@marples.name>
Sat, 16 Feb 2013 13:21:35 +0000 (13:21 +0000)
committerRoy Marples <roy@marples.name>
Sat, 16 Feb 2013 13:21:35 +0000 (13:21 +0000)
Now we have removed all our xmalloc like functions dhcpcd can still
try to operate as best it can in low memory conditions.

12 files changed:
common.c
common.h
dhcp-common.c
dhcp.c
dhcp6.c
dhcpcd.c
eloop.c
if-options.c
ipv4.c
ipv6.c
ipv6rs.c
script.c

index f77c02598c210fb7249a7714b840d669383209df..1725e560e313199a6cbeaa5df922a956cd52161c 100644 (file)
--- a/common.c
+++ b/common.c
@@ -207,7 +207,11 @@ setvar(char ***e, const char *prefix, const char *var, const char *value)
 
        if (prefix)
                len += strlen(prefix) + 1;
-       **e = xmalloc(len);
+       **e = malloc(len);
+       if (**e == NULL) {
+               syslog(LOG_ERR, "%s: %m", __func__);
+               return -1;
+       }
        if (prefix)
                snprintf(**e, len, "%s_%s=%s", prefix, var, value);
        else
@@ -250,17 +254,3 @@ writepid(int fd, pid_t pid)
                return -1;
        return 0;
 }
-
-#ifndef xmalloc
-void *
-xmalloc(size_t s)
-{
-       void *value = malloc(s);
-
-       if (value != NULL)
-               return value;
-       syslog(LOG_ERR, "memory exhausted (xalloc %zu bytes)", s);
-       exit (EXIT_FAILURE);
-       /* NOTREACHED */
-}
-#endif
index ac37b941fc06e5c9ee1c64f6674753ec335a998d..b03f8eee9556d999cd0229af2015fed465a4d313 100644 (file)
--- a/common.h
+++ b/common.h
@@ -90,6 +90,5 @@ 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 *xmalloc(size_t);
 
 #endif
index 6a69d27270ffe702eebeae1c10c422abe03b1398..4f285ff57b456da9a59436fcf9abde5351fa1d5b 100644 (file)
@@ -244,7 +244,9 @@ print_option(char *s, ssize_t len, int type, int dl, const uint8_t *data,
                l = decode_rfc3397(NULL, 0, dl, data);
                if (l < 1)
                        return l;
-               tmp = xmalloc(l);
+               tmp = malloc(l);
+               if (tmp == NULL)
+                       return -1;
                decode_rfc3397(tmp, l, dl, data);
                l = print_string(s, len, l - 1, (uint8_t *)tmp);
                free(tmp);
diff --git a/dhcp.c b/dhcp.c
index ee61688814f34ed3c599fe51042c3a518c723fe0..b845ed8143fdfd66ba33bb11400e7bf200494bcb 100644 (file)
--- a/dhcp.c
+++ b/dhcp.c
@@ -302,7 +302,9 @@ get_option(const struct dhcp_message *dhcp, uint8_t opt, int *len, int *type)
                if (o == opt) {
                        if (op) {
                                if (!opt_buffer) {
-                                       opt_buffer = xmalloc(sizeof(*dhcp));
+                                       opt_buffer = malloc(sizeof(*dhcp));
+                                       if (opt_buffer == NULL)
+                                               return NULL;
 #ifdef DEBUG_MEMORY
                                        atexit(free_option_buffer);
 #endif
@@ -542,7 +544,9 @@ decode_rfc3361(int dl, const uint8_t *data)
        switch (enc) {
        case 0:
                if ((l = decode_rfc3397(NULL, 0, dl, data)) > 0) {
-                       sip = xmalloc(l);
+                       sip = malloc(l);
+                       if (sip == NULL)
+                               return 0;
                        decode_rfc3397(sip, l, dl, data);
                }
                break;
@@ -553,7 +557,9 @@ decode_rfc3361(int dl, const uint8_t *data)
                }
                addr.s_addr = INADDR_BROADCAST;
                l = ((dl / sizeof(addr.s_addr)) * ((4 * 4) + 1)) + 1;
-               sip = p = xmalloc(l);
+               sip = p = malloc(l);
+               if (sip == NULL)
+                       return 0;
                while (dl != 0) {
                        memcpy(&addr.s_addr, data, sizeof(addr.s_addr));
                        data += sizeof(addr.s_addr);
@@ -656,17 +662,20 @@ get_option_string(const struct dhcp_message *dhcp, uint8_t option)
                        errno = EINVAL;
                        return NULL;
                }
-               s = xmalloc(sizeof(char) * type);
-               decode_rfc3397(s, type, len, p);
+               s = malloc(sizeof(char) * type);
+               if (s)
+                       decode_rfc3397(s, type, len, p);
                return s;
        }
 
        if (type & RFC3361)
                return decode_rfc3361(len, p);
 
-       s = xmalloc(sizeof(char) * (len + 1));
-       memcpy(s, p, len);
-       s[len] = '\0';
+       s = malloc(sizeof(char) * (len + 1));
+       if (s) {
+               memcpy(s, p, len);
+               s[len] = '\0';
+       }
        return s;
 }
 
@@ -1118,8 +1127,11 @@ read_lease(const struct interface *ifp)
        }
        syslog(LOG_DEBUG, "%s: reading lease `%s'",
            ifp->name, state->leasefile);
-       dhcp = xmalloc(sizeof(*dhcp));
-       memset(dhcp, 0, sizeof(*dhcp));
+       dhcp = calloc(1, sizeof(*dhcp));
+       if (dhcp == NULL) {
+               close(fd);
+               return NULL;
+       }
        bytes = read(fd, dhcp, sizeof(*dhcp));
        close(fd);
        if (bytes < 0) {
@@ -1210,7 +1222,9 @@ dhcp_env(char **env, const char *prefix, const struct dhcp_message *dhcp,
                if (len < 0)
                        return -1;
                e = strlen(prefix) + strlen(opt->var) + len + 4;
-               v = val = *ep++ = xmalloc(e);
+               v = val = *ep++ = malloc(e);
+               if (v == NULL)
+                       return -1;
                v += snprintf(val, e, "%s_%s=", prefix, opt->var);
                if (len != 0)
                        print_option(v, len, opt->type, pl, p, ifp->name);
@@ -2304,8 +2318,14 @@ dhcp_handlepacket(void *arg)
        /* We loop through until our buffer is empty.
         * The benefit is that if we get >1 DHCP packet in our buffer and
         * the first one fails for any reason, we can use the next. */
-       if (packet == NULL)
-               packet = xmalloc(udp_dhcp_len);
+       if (packet == NULL) {
+               packet = malloc(udp_dhcp_len);
+               if (packet == NULL) {
+                       syslog(LOG_ERR, "%s: %m", __func__);
+                       return;
+               }
+       }
+
        for(;;) {
                bytes = ipv4_getrawpacket(iface, ETHERTYPE_IP,
                    packet, udp_dhcp_len, &partialcsum);
@@ -2498,18 +2518,24 @@ dhcp_init(struct interface *ifp)
        state->clientid = NULL;
 
        if (*ifo->clientid) {
-               state->clientid = xmalloc(ifo->clientid[0] + 1);
+               state->clientid = malloc(ifo->clientid[0] + 1);
+               if (state->clientid == NULL)
+                       goto eexit;
                memcpy(state->clientid, ifo->clientid, ifo->clientid[0] + 1);
        } else if (ifo->options & DHCPCD_CLIENTID) {
                len = 0;
                if (ifo->options & DHCPCD_DUID) {
-                       duid = xmalloc(DUID_LEN);
+                       duid = malloc(DUID_LEN);
+                       if (duid == NULL)
+                               goto eexit;
                        if ((len = get_duid(duid, ifp)) == 0)
                                syslog(LOG_ERR, "get_duid: %m");
                } else
                        duid = NULL;
                if (len > 0) {
-                       state->clientid = xmalloc(len + 6);
+                       state->clientid = malloc(len + 6);
+                       if (state->clientid == NULL)
+                               goto eexit;
                        state->clientid[0] = len + 5;
                        state->clientid[1] = 255; /* RFC 4361 */
                        ifl = strlen(ifp->name);
@@ -2525,7 +2551,9 @@ dhcp_init(struct interface *ifp)
                        memcpy(state->clientid + 6, duid, len);
                } else if (len == 0) {
                        len = ifp->hwlen + 1;
-                       state->clientid = xmalloc(len + 1);
+                       state->clientid = malloc(len + 1);
+                       if (state->clientid == NULL)
+                               goto eexit;
                        state->clientid[0] = len;
                        state->clientid[1] = ifp->family;
                        memcpy(state->clientid + 2, ifp->hwaddr,
@@ -2539,8 +2567,11 @@ dhcp_init(struct interface *ifp)
        else if (ifp->hwlen)
                syslog(LOG_DEBUG, "%s: using hwaddr %s", ifp->name,
                    hwaddr_ntoa(ifp->hwaddr, ifp->hwlen));
-
        return 0;
+
+eexit:
+       syslog(LOG_ERR, "%s: Error making ClientID: %m", __func__);
+       return -1;
 }
 
 void
diff --git a/dhcp6.c b/dhcp6.c
index d05dc2816d46de4f2a2ca935b39a0254153e5bdc..4a187da9f1e8b14db2b505b9a128ba50082e75bd 100644 (file)
--- a/dhcp6.c
+++ b/dhcp6.c
@@ -1599,7 +1599,11 @@ dhcp6_env(char **env, const char *prefix, const struct interface *ifp,
                if (len < 0)
                        return -1;
                e = strlen(prefix) + 6 + strlen(opt->var) + len + 4;
-               v = val = *ep++ = xmalloc(e);
+               v = val = *ep++ = malloc(e);
+               if (v == NULL) {
+                       syslog(LOG_ERR, "%s: %m", __func__);
+                       return -1;
+               }
                v += snprintf(val, e, "%s_dhcp6_%s=", prefix, opt->var);
                if (len != 0)
                        print_option(v, len, opt->type, ol, od, ifp->name);
@@ -1614,7 +1618,11 @@ dhcp6_env(char **env, const char *prefix, const struct interface *ifp,
                        TAILQ_FOREACH(ap, &state->addrs, next) {
                                e += strlen(ap->saddr) + 1;
                        }
-                       v = val = *ep++ = xmalloc(e);
+                       v = val = *ep++ = malloc(e);
+                       if (v == NULL) {
+                               syslog(LOG_ERR, "%s: %m", __func__);
+                               return -1;
+                       }
                        v += snprintf(val, e, "%s_dhcp6_ip_address=", prefix);
                        TAILQ_FOREACH(ap, &state->addrs, next) {
                                strcpy(v, ap->saddr);
index ad294553a86680301a562933d30d01e13b23d90c..50a455e55563bfc424ff53420814d6be05f63bb2 100644 (file)
--- a/dhcpcd.c
+++ b/dhcpcd.c
@@ -810,7 +810,11 @@ handle_args(struct fd_list *fd, int argc, char **argv)
        len = 0;
        for (opt = 0; opt < argc; opt++)
                len += strlen(argv[opt]) + 1;
-       tmp = p = xmalloc(len + 1);
+       tmp = p = malloc(len + 1);
+       if (tmp == NULL) {
+               syslog(LOG_ERR, "%s: %m", __func__);
+               return -1;
+       }
        for (opt = 0; opt < argc; opt++) {
                l = strlen(argv[opt]);
                strlcpy(p, argv[opt], l + 1);
@@ -842,7 +846,7 @@ handle_args(struct fd_list *fd, int argc, char **argv)
 
        /* We need at least one interface */
        if (optind == argc) {
-               syslog(LOG_ERR, "handle_args: no interface");
+               syslog(LOG_ERR, "%s: no interface", __func__);
                return -1;
        }
 
@@ -989,7 +993,11 @@ main(int argc, char **argv)
                /* If we have any other args, we should run as a single dhcpcd
                 *  instance for that interface. */
                len = strlen(PIDFILE) + IF_NAMESIZE + 2;
-               pidfile = xmalloc(len);
+               pidfile = malloc(len);
+               if (pidfile == NULL) {
+                       syslog(LOG_ERR, "%s: %m", __func__);
+                       exit(EXIT_FAILURE);
+               }
                if (optind == argc - 1)
                        snprintf(pidfile, len, PIDFILE, "-", argv[optind]);
                else {
diff --git a/eloop.c b/eloop.c
index 9a28890ff6fb9aa43fc901744c247d1ff70a9759..0f4191be2f48101c42d5670d03503d7e7bf248e5 100644 (file)
--- a/eloop.c
+++ b/eloop.c
@@ -77,8 +77,14 @@ eloop_event_add(int fd, void (*callback)(void *), void *arg)
        if (free_events) {
                e = free_events;
                free_events = e->next;
-       } else
-               e = xmalloc(sizeof(*e));
+       } else {
+               e = malloc(sizeof(*e));
+               if (e == NULL) {
+                       syslog(LOG_ERR, "%s: %m", __func__);
+                       return;
+               }
+       }
+    
        e->fd = fd;
        e->callback = callback;
        e->arg = arg;
@@ -140,8 +146,13 @@ eloop_q_timeout_add_tv(int queue,
                if (free_timeouts) {
                        t = free_timeouts;
                        free_timeouts = t->next;
-               } else
-                       t = xmalloc(sizeof(*t));
+               } else {
+                       t = malloc(sizeof(*t));
+                       if (t == NULL) {
+                               syslog(LOG_ERR, "%s: %m", __func__);
+                               return;
+                       }
+               }
        }
 
        t->when.tv_sec = w.tv_sec;
index ae63a7dd81d9933b049f734765c6b78430ecdc75..0d20d4d5ba8a7fde50c90bbf805ed2b81799a982 100644 (file)
@@ -635,7 +635,11 @@ parse_option(struct if_options *ifo, int opt, const char *arg)
                while ((p = strchr(arg, ',')))
                        *p = ' ';
                s = strlen("skip_hooks=") + strlen(arg) + 1;
-               p = xmalloc(sizeof(char) * s);
+               p = malloc(sizeof(char) * s);
+               if (p == NULL) {
+                       syslog(LOG_ERR, "%s: %m", __func__);
+                       return -1;
+               }
                snprintf(p, s, "skip_hooks=%s", arg);
                add_environ(ifo, p, 0);
                free(p);
@@ -755,7 +759,11 @@ parse_option(struct if_options *ifo, int opt, const char *arg)
                                rt = ifo->routes;
                                while (rt->next)
                                        rt = rt->next;
-                               rt->next = xmalloc(sizeof(*rt));
+                               rt->next = malloc(sizeof(*rt));
+                               if (rt->next == NULL) {
+                                       syslog(LOG_ERR, "%s: %m", __func__);
+                                       return -1;
+                               }
                                rt = rt->next;
                        }
                        rt->next = NULL;
@@ -777,7 +785,11 @@ parse_option(struct if_options *ifo, int opt, const char *arg)
                                rt = ifo->routes;
                                while (rt->next)
                                        rt = rt->next;
-                               rt->next = xmalloc(sizeof(*rt));
+                               rt->next = malloc(sizeof(*rt));
+                               if (rt->next == NULL) {
+                                       syslog(LOG_ERR, "%s: %m", __func__);
+                                       return -1;
+                               }
                                rt = rt->next;
                        }
                        rt->dest.s_addr = INADDR_ANY;
diff --git a/ipv4.c b/ipv4.c
index 612f14f3999e0ba1cf5e389a79feaf9e28d24fe4..10b79e7a78ee30023a29420fe3f37d4bb3f61ecc 100644 (file)
--- a/ipv4.c
+++ b/ipv4.c
@@ -325,7 +325,9 @@ add_subnet_route(struct rt *rt, const struct interface *iface)
             iface->options->req_addr.s_addr == INADDR_ANY))
                return rt;
 
-       r = xmalloc(sizeof(*r));
+       r = malloc(sizeof(*r));
+       if (r == NULL)
+               return NULL;
        r->dest.s_addr = s->addr.s_addr & s->net.s_addr;
        r->net.s_addr = s->net.s_addr;
        r->gate.s_addr = 0;
@@ -346,11 +348,16 @@ get_routes(struct interface *ifp)
                        if (rt->gate.s_addr == 0)
                                break;
                        if (r == NULL)
-                               r = nrt = xmalloc(sizeof(*r));
+                               r = nrt = malloc(sizeof(*r));
                        else {
-                               r->next = xmalloc(sizeof(*r));
+                               r->next = malloc(sizeof(*r));
                                r = r->next;
                        }
+                       if (r == NULL) {
+                               syslog(LOG_ERR, "%s: %m", __func__);
+                               ipv4_freeroutes(nrt);
+                               return NULL;
+                       }
                        memcpy(r, rt, sizeof(*r));
                        r->next = NULL;
                }
@@ -384,7 +391,11 @@ add_destination_route(struct rt *rt, const struct interface *iface)
        if (!(iface->flags & IFF_POINTOPOINT) ||
            !has_option_mask(iface->options->dstmask, DHO_ROUTER))
                return rt;
-       r = xmalloc(sizeof(*r));
+       r = malloc(sizeof(*r));
+       if (r == NULL) {
+               syslog(LOG_ERR, "%s: %m", __func__);
+               return NULL;
+       }
        r->dest.s_addr = INADDR_ANY;
        r->net.s_addr = INADDR_ANY;
        r->gate.s_addr = D_CSTATE(iface)->dst.s_addr;
@@ -431,7 +442,11 @@ add_router_host_route(struct rt *rt, const struct interface *ifp)
                }
                syslog(LOG_WARNING, "%s: router %s requires a host route",
                    ifp->name, inet_ntoa(rtp->gate));
-               rtn = xmalloc(sizeof(*rtn));
+               rtn = malloc(sizeof(*rtn));
+               if (rtn == NULL) {
+                       syslog(LOG_ERR, "%s: %m", __func__);
+                       continue;
+               }
                rtn->dest.s_addr = rtp->gate.s_addr;
                rtn->net.s_addr = INADDR_BROADCAST;
                rtn->gate.s_addr = rtp->gate.s_addr;
diff --git a/ipv6.c b/ipv6.c
index 5c2f3503119cad1ed23213de42b69244378192cd..e7282ba64916a23093e8369f603f51f8a510db86 100644 (file)
--- a/ipv6.c
+++ b/ipv6.c
@@ -133,7 +133,11 @@ ipv6_linklocal(const char *ifname)
        }
 
        if (ifa) {
-               in6 = xmalloc(sizeof(*in6));
+               in6 = malloc(sizeof(*in6));
+               if (in6 == NULL) {
+                       syslog(LOG_ERR, "%s: %m", __func__);
+                       return NULL;
+               }
                memcpy(in6, &sa6->sin6_addr, sizeof(*in6));
        } else
                in6 = NULL;
@@ -504,7 +508,11 @@ ipv6_buildroutes(void)
                }
        }
 
-       nrs = xmalloc(sizeof(*nrs));
+       nrs = malloc(sizeof(*nrs));
+       if (nrs == NULL) {
+               syslog(LOG_ERR, "%s: %m", __func__);
+               return;
+       }
        TAILQ_INIT(nrs);
        have_default = 0;
        TAILQ_FOREACH_SAFE(rt, &dnr, next, rtn) {
index 82fafcba939b8dc20007cef53f700a064bcb34f4..3e09ee38f5ce049502978c437d7e19e74dc17fc4 100644 (file)
--- a/ipv6rs.c
+++ b/ipv6rs.c
@@ -527,7 +527,13 @@ ipv6rs_handledata(_unused void *arg)
        } else
                new_rap = 0;
        if (rap->data_len == 0) {
-               rap->data = xmalloc(len);
+               rap->data = malloc(len);
+               if (rap->data == NULL) {
+                       syslog(LOG_ERR, "%s: %m", __func__);
+                       if (new_rap)
+                               free(rap);
+                       return;
+               }
                memcpy(rap->data, icp, len);
                rap->data_len = len;
        }
@@ -609,7 +615,11 @@ ipv6rs_handledata(_unused void *arg)
                                    !(pi->nd_opt_pi_flags_reserved &
                                    ND_OPT_PI_FLAG_ONLINK))
                                        break;
-                               ap = xmalloc(sizeof(*ap));
+                               ap = malloc(sizeof(*ap));
+                               if (ap == NULL) {
+                                       syslog(LOG_ERR, "%s: %m", __func__);
+                                       break;
+                               }
                                ap->new = 1;
                                ap->onlink = 0;
                                ap->prefix_len = pi->nd_opt_pi_prefix_len;
@@ -769,7 +779,11 @@ ipv6rs_handledata(_unused void *arg)
                }
 
                if (rao == NULL) {
-                       rao = xmalloc(sizeof(*rao));
+                       rao = malloc(sizeof(*rao));
+                       if (rao == NULL) {
+                               syslog(LOG_ERR, "%s: %m", __func__);
+                               continue;
+                       }
                        rao->type = ndo->nd_opt_type;
                        rao->option = opt;
                        TAILQ_INSERT_TAIL(&rap->options, rao, next);
@@ -873,7 +887,8 @@ ipv6rs_env(char **env, const char *prefix, const struct interface *ifp)
                if (env) {
                        snprintf(buffer, sizeof(buffer),
                            "ra%d_from", i);
-                       setvar(&env, prefix, buffer, rap->sfrom);
+                       if (setvar(&env, prefix, buffer, rap->sfrom) == -1)
+                               return -1;
                }
                l++;
 
@@ -936,24 +951,28 @@ ipv6rs_env(char **env, const char *prefix, const struct interface *ifp)
                                new = realloc(**var,
                                    strlen(**var) + 1 +
                                    strlen(rao->option) + 1);
-                               if (new) {
-                                       **var = new;
-                                       new += strlen(new);
-                                       *new++ = ' ';
-                                       strcpy(new, rao->option);
-                                       continue;
-                               }
+                               if (new == NULL)
+                                       return -1;
+                               **var = new;
+                               new += strlen(new);
+                               *new++ = ' ';
+                               strcpy(new, rao->option);
+                               continue;
                        }
                        if (env) {
                                snprintf(buffer, sizeof(buffer),
                                    "ra%d_%s", i, optn);
-                               setvar(&env, prefix, buffer, rao->option);
+                               if (setvar(&env, prefix, buffer, rao->option)
+                                   == -1)
+                                       return -1;
                        }
                }
        }
 
-       if (env)
-               setvard(&env, prefix, "ra_count", i);
+       if (env) {
+               if (setvard(&env, prefix, "ra_count", i) == -1)
+                       return -1;
+       }
        l++;
        return l;
 }
index 0f9f84408bd436e716e6aba7d827a4a8f93f4b6c..863ed29a6ffa1ba4fea2642deac6cf010c76c4bc 100644 (file)
--- a/script.c
+++ b/script.c
@@ -114,21 +114,25 @@ make_var(const char *prefix, const char *var)
        char *v;
 
        len = strlen(prefix) + strlen(var) + 2;
-       v = xmalloc(len);
+       v = malloc(len);
+       if (v == NULL) {
+               syslog(LOG_ERR, "%s: %m", __func__);
+               return NULL;
+       }
        snprintf(v, len, "%s_%s", prefix, var);
        return v;
 }
 
 
-static void
+static int
 append_config(char ***env, ssize_t *len,
     const char *prefix, const char *const *config)
 {
        ssize_t i, j, e1;
-       char **ne, *eq, **nep;
+       char **ne, *eq, **nep, *p;
 
        if (config == NULL)
-               return;
+               return 0;
 
        ne = *env;
        for (i = 0; config[i] != NULL; i++) {
@@ -140,22 +144,28 @@ append_config(char ***env, ssize_t *len,
                        {
                                free(ne[j]);
                                ne[j] = make_var(prefix, config[i]);
+                               if (ne[j] == NULL)
+                                       return -1;
                                break;
                        }
                }
                if (j == *len) {
                        j++;
+                       p = make_var(prefix, config[i]);
+                       if (p == NULL)
+                               return -1;
                        nep = realloc(ne, sizeof(char *) * (j + 1));
                        if (nep == NULL) {
                                syslog(LOG_ERR, "%s: %m", __func__);
-                               break;
+                               return -1;
                        }
                        ne = nep;
-                       ne[j - 1] = make_var(prefix, config[i]);
+                       ne[j - 1] = p;
                        *len = j;
                }
        }
        *env = ne;
+       return 0;
 }
 #endif
 
@@ -170,7 +180,9 @@ arraytostr(const char *const *argv, char **s)
        ap = argv;
        while (*ap)
                len += strlen(*ap++) + 1;
-       *s = p = xmalloc(len);
+       *s = p = malloc(len);
+       if (p == NULL)
+               return -1;
        ap = argv;
        while (*ap) {
                l = strlen(*ap) + 1;
@@ -222,34 +234,35 @@ make_env(const struct interface *ifp, const char *reason, char ***argv)
        else
                elen = 10;
 
+#define EMALLOC(i, l) if ((env[(i)] = malloc(l)) == NULL) goto eexit;
        /* Make our env */
        env = calloc(1, sizeof(char *) * (elen + 1));
        if (env == NULL)
                goto eexit;
        e = strlen("interface") + strlen(ifp->name) + 2;
-       env[0] = xmalloc(e);
+       EMALLOC(0, e);
        snprintf(env[0], e, "interface=%s", ifp->name);
        e = strlen("reason") + strlen(reason) + 2;
-       env[1] = xmalloc(e);
+       EMALLOC(1, e);
        snprintf(env[1], e, "reason=%s", reason);
        if (options & DHCPCD_DUMPLEASE)
                goto dumplease;
-
        e = 20;
-       env[2] = xmalloc(e);
+       EMALLOC(2, e);
        snprintf(env[2], e, "pid=%d", getpid());
-       env[3] = xmalloc(e);
+       EMALLOC(3, e);
        snprintf(env[3], e, "ifmetric=%d", ifp->metric);
-       env[4] = xmalloc(e);
+       EMALLOC(4, e);
        snprintf(env[4], e, "ifwireless=%d", ifp->wireless);
-       env[5] = xmalloc(e);
+       EMALLOC(5, e);
        snprintf(env[5], e, "ifflags=%u", ifp->flags);
-       env[6] = xmalloc(e);
+       EMALLOC(6, e);
        snprintf(env[6], e, "ifmtu=%d", get_mtu(ifp->name));
        l = e = strlen("interface_order=");
        for (ifp2 = ifaces; ifp2; ifp2 = ifp2->next)
                e += strlen(ifp2->name) + 1;
-       p = env[7] = xmalloc(e);
+       EMALLOC(7, e);
+       p = env[7];
        strlcpy(p, "interface_order=", e);
        e -= l;
        p += l;
@@ -277,9 +290,11 @@ make_env(const struct interface *ifp, const char *reason, char ***argv)
                env[8] = strdup("if_up=false");
                env[9] = strdup("if_down=true");
        }
+       if (env[8] == NULL || env[9] == NULL)
+               goto eexit;
        if (*ifp->profile) {
                e = strlen("profile=") + strlen(ifp->profile) + 2;
-               env[elen] = xmalloc(e);
+               EMALLOC(elen, e);
                snprintf(env[elen++], e, "profile=%s", ifp->profile);
        }
        if (ifp->wireless) {
@@ -289,7 +304,7 @@ make_env(const struct interface *ifp, const char *reason, char ***argv)
                        if (nenv == NULL)
                                goto eexit;
                        env = nenv;
-                       env[elen] = xmalloc(e);
+                       EMALLOC(elen, e);
                        snprintf(env[elen++], e, "new_ssid=%s", ifp->ssid);
                }
                else if (strcmp(reason, "NOCARRIER") == 0) {
@@ -297,7 +312,7 @@ make_env(const struct interface *ifp, const char *reason, char ***argv)
                        if (nenv == NULL)
                                goto eexit;
                        env = nenv;
-                       env[elen] = xmalloc(e);
+                       EMALLOC(elen, e);
                        snprintf(env[elen++], e, "old_ssid=%s", ifp->ssid);
                }
        }
@@ -309,10 +324,14 @@ make_env(const struct interface *ifp, const char *reason, char ***argv)
                        if (nenv == NULL)
                                goto eexit;
                        env = nenv;
-                       elen += dhcp_env(env + elen, "old", state->old, ifp);
+                       l = dhcp_env(env + elen, "old", state->old, ifp);
+                       if (l == -1)
+                               goto eexit;
+                       elen += l;
                }
-               append_config(&env, &elen, "old",
-                   (const char *const *)ifo->config);
+               if (append_config(&env, &elen, "old",
+                   (const char *const *)ifo->config) == -1)
+                       goto eexit;
        }
 #endif
 #ifdef INET6
@@ -324,8 +343,11 @@ make_env(const struct interface *ifp, const char *reason, char ***argv)
                        if (nenv == NULL)
                                goto eexit;
                        env = nenv;
-                       elen += dhcp6_env(env + elen, "old", ifp,
+                       = dhcp6_env(env + elen, "old", ifp,
                            d6_state->old, d6_state->old_len);
+                       if (l == -1)
+                               goto eexit;
+                       elen += l;
                }
        }
 #endif
@@ -339,11 +361,15 @@ dumplease:
                        if (nenv == NULL)
                                goto eexit;
                        env = nenv;
-                       elen += dhcp_env(env + elen, "new",
+                       = dhcp_env(env + elen, "new",
                            state->new, ifp);
+                       if (l == -1)
+                               goto eexit;
+                       elen += l;
                }
-               append_config(&env, &elen, "new",
-                   (const char *const *)ifo->config);
+               if (append_config(&env, &elen, "new",
+                   (const char *const *)ifo->config) == -1)
+                       goto eexit;
        }
 #endif
 #ifdef INET6
@@ -355,8 +381,11 @@ dumplease:
                        if (nenv == NULL)
                                goto eexit;
                        env = nenv;
-                       elen += dhcp6_env(env + elen, "new", ifp,
+                       = dhcp6_env(env + elen, "new", ifp,
                            d6_state->new, d6_state->new_len);
+                       if (l == -1)
+                               goto eexit;
+                       elen += l;
                }
        }
        if (ra) {
@@ -366,7 +395,10 @@ dumplease:
                        if (nenv == NULL)
                                goto eexit;
                        env = nenv;
-                       elen += ipv6rs_env(env + elen, NULL, ifp);
+                       l = ipv6rs_env(env + elen, NULL, ifp);
+                       if (l == -1)
+                               goto eexit;
+                       elen += l;
                }
        }
 #endif
@@ -411,8 +443,11 @@ send_interface1(int fd, const struct interface *iface, const char *reason)
        struct iovec iov[2];
        int retval;
 
-       make_env(iface, reason, &env);
+       if (make_env(iface, reason, &env) == -1)
+               return -1;
        elen = arraytostr((const char *const *)env, &s);
+       if (elen == -1)
+               return -1;
        iov[0].iov_base = &elen;
        iov[0].iov_len = sizeof(ssize_t);
        iov[1].iov_base = s;
@@ -482,7 +517,11 @@ script_runreason(const struct interface *ifp, const char *reason)
        path = getenv("PATH");
        if (path) {
                e = strlen("PATH") + strlen(path) + 2;
-               env[elen] = xmalloc(e);
+               env[elen] = malloc(e);
+               if (env[elen] == NULL) {
+                       elen = -1;
+                       goto out;
+               }
                snprintf(env[elen], e, "PATH=%s", path);
        } else {
                env[elen] = strdup(DEFAULT_PATH);