]> git.ipfire.org Git - thirdparty/dhcpcd.git/commitdiff
Remove xzalloc function
authorRoy Marples <roy@marples.name>
Fri, 15 Feb 2013 21:45:08 +0000 (21:45 +0000)
committerRoy Marples <roy@marples.name>
Fri, 15 Feb 2013 21:45:08 +0000 (21:45 +0000)
common.c
common.h
dhcp.c
dhcpcd.c
if-linux.c
if-options.c
ipv4ll.c
ipv6.c
ipv6rs.c
net.c

index 15fe3771fd627379b8d9a93c9b19b072407bd014..a3e9aee6c7f6ae31a6276ef61401a0238c356949 100644 (file)
--- a/common.c
+++ b/common.c
@@ -296,12 +296,3 @@ xstrdup(const char *str)
        /* NOTREACHED */
 }
 #endif
-
-void *
-xzalloc(size_t s)
-{
-       void *value = xmalloc(s);
-
-       memset(value, 0, s);
-       return value;
-}
index 37b7a2c8703dce3faeadec8e0c9a9514e30f3ce2..af390937c92e619d25612210b092838974927b9a 100644 (file)
--- a/common.h
+++ b/common.h
@@ -92,7 +92,6 @@ time_t uptime(void);
 int writepid(int, pid_t);
 void *xrealloc(void *, size_t);
 void *xmalloc(size_t);
-void *xzalloc(size_t);
 char *xstrdup(const char *);
 
 #endif
diff --git a/dhcp.c b/dhcp.c
index 962cae9166114885dc7b223f8d9e1e6eae5796c5..c0749808be09f93623c08b7547f0408719bdb13e 100644 (file)
--- a/dhcp.c
+++ b/dhcp.c
@@ -496,10 +496,15 @@ decode_rfc3442_rt(int dl, const uint8_t *data)
                }
 
                if (rt) {
-                       rt->next = xzalloc(sizeof(*rt));
+                       rt->next = calloc(1, sizeof(*rt));
                        rt = rt->next;
                } else {
-                       routes = rt = xzalloc(sizeof(*routes));
+                       routes = rt = calloc(1, sizeof(*routes));
+               }
+               if (rt == NULL) {
+                       syslog(LOG_ERR, "%s: %m", __func__);
+                       ipv4_freeroutes(routes);
+                       return NULL;
                }
                rt->next = NULL;
 
@@ -737,10 +742,14 @@ get_option_routes(struct interface *ifp, const struct dhcp_message *dhcp)
                e = p + len;
                while (p < e) {
                        if (route) {
-                               route->next = xmalloc(sizeof(*route));
+                               route->next = calloc(1, sizeof(*route));
                                route = route->next;
                        } else
-                               routes = route = xmalloc(sizeof(*routes));
+                               routes = route = calloc(1, sizeof(*routes));
+                       if (route == NULL) {
+                               syslog(LOG_ERR, "%s: %m", __func__);
+                               break;
+                       }
                        route->next = NULL;
                        memcpy(&route->dest.s_addr, p, 4);
                        p += 4;
@@ -759,10 +768,14 @@ get_option_routes(struct interface *ifp, const struct dhcp_message *dhcp)
                e = p + len;
                while (p < e) {
                        if (route) {
-                               route->next = xzalloc(sizeof(*route));
+                               route->next = calloc(1, sizeof(*route));
                                route = route->next;
                        } else
-                               routes = route = xzalloc(sizeof(*route));
+                               routes = route = calloc(1, sizeof(*route));
+                       if (route == NULL) {
+                               syslog(LOG_ERR, "%s: %m", __func__);
+                               break;
+                       }
                        memcpy(&route->gate.s_addr, p, 4);
                        p += 4;
                }
@@ -848,7 +861,9 @@ make_message(struct dhcp_message **message,
        const struct dhcp_lease *lease = &state->lease;
        time_t up = uptime() - state->start_uptime;
 
-       dhcp = xzalloc(sizeof (*dhcp));
+       dhcp = calloc(1, sizeof (*dhcp));
+       if (dhcp == NULL)
+               return -1;
        m = (uint8_t *)dhcp;
        p = dhcp->options;
 
@@ -1394,7 +1409,9 @@ dhcp_makeudppacket(uint8_t **p, const uint8_t *data, size_t length,
        struct ip *ip;
        struct udphdr *udp;
 
-       udpp = xzalloc(sizeof(*udpp));
+       udpp = calloc(1, sizeof(*udpp));
+       if (udpp == NULL)
+               return -1;
        ip = &udpp->ip;
        udp = &udpp->udp;
 
@@ -1499,6 +1516,8 @@ send_message(struct interface *iface, int type,
                }
        } else {
                len = dhcp_makeudppacket(&udp, (uint8_t *)dhcp, len, from, to);
+               if (len == -1)
+                       return;
                r = ipv4_sendrawpacket(iface, ETHERTYPE_IP, udp, len);
                free(udp);
                /* If we failed to send a raw packet this normally means
@@ -1825,7 +1844,9 @@ dhcp_message_new(struct in_addr *addr, struct in_addr *mask)
        struct dhcp_message *dhcp;
        uint8_t *p;
 
-       dhcp = xzalloc(sizeof(*dhcp));
+       dhcp = calloc(1, sizeof(*dhcp));
+       if (dhcp == NULL)
+               return NULL;
        dhcp->yiaddr = addr->s_addr;
        p = dhcp->options;
        if (mask && mask->s_addr != INADDR_ANY) {
@@ -2319,8 +2340,13 @@ dhcp_handlepacket(void *arg)
                            iface->name, inet_ntoa(from));
                        continue;
                }
-               if (!dhcp)
-                       dhcp = xzalloc(sizeof(*dhcp));
+               if (dhcp == NULL) {
+                       dhcp = calloc(1, sizeof(*dhcp));
+                       if (dhcp == NULL) {
+                               syslog(LOG_ERR, "%s: calloc: %m", __func__);
+                               break;
+                       }
+               }
                memcpy(dhcp, pp, bytes);
                if (dhcp->cookie != htonl(MAGIC_COOKIE)) {
                        syslog(LOG_DEBUG, "%s: bogus cookie from %s",
@@ -2386,9 +2412,15 @@ dhcp_dump(const char *ifname)
        struct interface *ifp;
        struct dhcp_state *state;
 
-       ifaces = ifp = xzalloc(sizeof(*ifp));
-       ifp->if_data[IF_DATA_DHCP] = state = xzalloc(sizeof(*state));
-       ifp->options = xzalloc(sizeof(*ifp->options));
+       ifaces = ifp = calloc(1, sizeof(*ifp));
+       if (ifp == NULL)
+               goto eexit;
+       ifp->if_data[IF_DATA_DHCP] = state = calloc(1, sizeof(*state));
+       if (state == NULL)
+               goto eexit;
+       ifp->options = calloc(1, sizeof(*ifp->options));
+       if (ifp->options == NULL)
+               goto eexit;
        strlcpy(ifp->name, ifname, sizeof(ifp->name));
        snprintf(state->leasefile, sizeof(state->leasefile),
            LEASEFILE, ifp->name);
@@ -2406,6 +2438,10 @@ dhcp_dump(const char *ifname)
        }
        state->reason = "DUMP";
        return script_runreason(ifp, state->reason);
+
+eexit:
+       syslog(LOG_ERR, "%s: %m", __func__);
+       return -1;
 }
 
 void
index d2a7b87558d3ef0613dd82983345f60a1936089d..ad294553a86680301a562933d30d01e13b23d90c 100644 (file)
--- a/dhcpcd.c
+++ b/dhcpcd.c
@@ -471,7 +471,7 @@ init_state(struct interface *ifp, int argc, char **argv)
        configure_interface(ifp, argc, argv);
        ifo = ifp->options;
 
-       if (if_options->options & DHCPCD_LINK && linkfd == -1) {
+       if (ifo->options & DHCPCD_LINK && linkfd == -1) {
                linkfd = open_link_socket();
                if (linkfd == -1) {
                        syslog(LOG_ERR, "open_link_socket: %m");
index e1c1ff6b05278a8d9db4d19eebfaab3ec6f632eb..3240e388cc36e8871aeb88dcbad424175d14b94a 100644 (file)
@@ -516,7 +516,9 @@ if_address(const struct interface *iface,
        struct nlma *nlm;
        int retval = 0;
 
-       nlm = xzalloc(sizeof(*nlm));
+       nlm = calloc(1, sizeof(*nlm));
+       if (nlm == NULL)
+               return -1;
        nlm->hdr.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
        nlm->hdr.nlmsg_flags = NLM_F_REQUEST;
        if (action >= 0) {
@@ -549,7 +551,9 @@ if_route(const struct rt *rt, int action)
        int retval = 0;
        struct dhcp_state *state;
 
-       nlm = xzalloc(sizeof(*nlm));
+       nlm = calloc(1, sizeof(*nlm));
+       if (nlm == NULL)
+               return -1;
        nlm->hdr.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
        nlm->hdr.nlmsg_type = RTM_NEWROUTE;
        if (action == 0)
@@ -614,7 +618,9 @@ if_address6(const struct interface *ifp, const struct ipv6_addr *ap, int action)
        struct ifa_cacheinfo cinfo;
        int retval = 0;
 
-       nlm = xzalloc(sizeof(*nlm));
+       nlm = calloc(1, sizeof(*nlm));
+       if (nlm == NULL)
+               return -1;
        nlm->hdr.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
        nlm->hdr.nlmsg_flags = NLM_F_REQUEST;
        if (action >= 0) {
@@ -673,7 +679,9 @@ if_route6(const struct rt6 *rt, int action)
        struct rtattr *metrics = (void *)metricsbuf;
        int retval = 0;
 
-       nlm = xzalloc(sizeof(*nlm));
+       nlm = calloc(1, sizeof(*nlm));
+       if (nlm == NULL)
+               return -1;
        nlm->hdr.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
        nlm->hdr.nlmsg_type = RTM_NEWROUTE;
        nlm->hdr.nlmsg_flags = NLM_F_REQUEST;
index ebabb5dde3b31076487b22192f9fd4f9a16b71c3..47a1a514239d81a3fdb1c4d2e522bb172e9c1712 100644 (file)
@@ -707,7 +707,12 @@ parse_option(struct if_options *ifo, int opt, const char *arg)
                        while (*np == ' ')
                                np++;
                        if (ifo->routes == NULL) {
-                               rt = ifo->routes = xmalloc(sizeof(*rt));
+                               rt = ifo->routes = calloc(1, sizeof(*rt));
+                               if (rt == NULL) {
+                                       syslog(LOG_ERR, "%s: %m", __func__);
+                                       *fp = ' ';
+                                       return -1;
+                               }
                        } else {
                                rt = ifo->routes;
                                while (rt->next)
@@ -725,7 +730,11 @@ parse_option(struct if_options *ifo, int opt, const char *arg)
                        *fp = ' ';
                } else if (strncmp(arg, "routers=", strlen("routers=")) == 0) {
                        if (ifo->routes == NULL) {
-                               rt = ifo->routes = xzalloc(sizeof(*rt));
+                               rt = ifo->routes = calloc(1, sizeof(*rt));
+                               if (rt == NULL) {
+                                       syslog(LOG_ERR, "%s: %m", __func__);
+                                       return -1;
+                               }
                        } else {
                                rt = ifo->routes;
                                while (rt->next)
@@ -868,7 +877,11 @@ read_config(const char *file,
        int skip = 0, have_profile = 0;
 
        /* Seed our default options */
-       ifo = xzalloc(sizeof(*ifo));
+       ifo = calloc(1, sizeof(*ifo));
+       if (ifo == NULL) {
+               syslog(LOG_ERR, "%s: %m", __func__);
+               return NULL;
+       }
        ifo->options |= DHCPCD_DAEMONISE | DHCPCD_LINK;
 #ifdef INET
        ifo->options |= DHCPCD_IPV4 | DHCPCD_IPV4LL;
index 970293f225df0e9a3d768f2a3cd8de328707eeb1..d2e069fcdf02daafc21a33c8690c300ba604ed8b 100644 (file)
--- a/ipv4ll.c
+++ b/ipv4ll.c
@@ -47,7 +47,9 @@ ipv4ll_make_lease(uint32_t addr)
        struct dhcp_message *dhcp;
        uint8_t *p;
 
-       dhcp = xzalloc(sizeof(*dhcp));
+       dhcp = calloc(1, sizeof(*dhcp));
+       if (dhcp == NULL)
+               return NULL;
        /* Put some LL options in */
        dhcp->yiaddr = addr;
        p = dhcp->options;
@@ -116,8 +118,12 @@ ipv4ll_start(void *arg)
                state->offer = ipv4ll_find_lease(addr);
        else
                state->offer = ipv4ll_make_lease(addr);
-       state->lease.frominfo = 0;
-       arp_probe(ifp);
+       if (state->offer == NULL)
+               syslog(LOG_ERR, "%s: %m", __func__);
+       else {
+               state->lease.frominfo = 0;
+               arp_probe(ifp);
+       }
 }
 
 void
diff --git a/ipv6.c b/ipv6.c
index fc15883d9297195197ef73e95e3fea7aaad21d7c..5c2f3503119cad1ed23213de42b69244378192cd 100644 (file)
--- a/ipv6.c
+++ b/ipv6.c
@@ -370,7 +370,11 @@ make_route(const struct interface *ifp, struct ra *rap)
 {
        struct rt6 *r;
 
-       r = xzalloc(sizeof(*r));
+       r = calloc(1, sizeof(*r));
+       if (r == NULL) {
+               syslog(LOG_ERR, "%s: %m", __func__);
+               return NULL;
+       }
        r->ra = rap;
        r->iface = ifp;
        r->metric = ifp->metric;
@@ -390,6 +394,8 @@ make_prefix(const struct interface * ifp,struct ra *rap, struct ipv6_addr *addr)
                return NULL;
 
        r = make_route(ifp, rap);
+       if (r == NULL)
+               return r;
        r->dest = addr->prefix;
        ipv6_mask(&r->net, addr->prefix_len);
        r->gate = in6addr_any;
@@ -403,6 +409,8 @@ make_router(struct ra *rap)
        struct rt6 *r;
 
        r = make_route(rap->iface, rap);
+       if (r == NULL)
+               return NULL;
        r->dest = in6addr_any;
        r->net = in6addr_any;
        r->gate = rap->from;
index ac30346c5c13bb0741c15060a609f57856fc0ea0..a1bd9d14b1cffa048334b6b8d88e3a3653f78292 100644 (file)
--- a/ipv6rs.c
+++ b/ipv6rs.c
@@ -207,7 +207,7 @@ ipv6rs_makeprobe(struct interface *ifp)
        state = RS_STATE(ifp);
        free(state->rs);
        state->rslen = sizeof(*rs) + ROUNDUP8(ifp->hwlen + 2);
-       state->rs = xzalloc(state->rslen);
+       state->rs = calloc(1, state->rslen);
        if (state->rs == NULL)
                return -1;
        rs = (struct nd_router_solicit *)(void *)state->rs;
@@ -512,7 +512,11 @@ ipv6rs_handledata(_unused void *arg)
                new_data = 0;
 
        if (rap == NULL) {
-               rap = xzalloc(sizeof(*rap));
+               rap = calloc(1, sizeof(*rap));
+               if (rap == NULL) {
+                       syslog(LOG_ERR, "%s: %m", __func__);
+                       return;
+               }
                rap->iface = ifp;
                memcpy(rap->from.s6_addr, from.sin6_addr.s6_addr,
                    sizeof(rap->from.s6_addr));
diff --git a/net.c b/net.c
index e66b609b5a76be55fc4e7186a0c86dac636d903e..028dd9fa0942bc9f161ad0f209013af0c0c0e989 100644 (file)
--- a/net.c
+++ b/net.c
@@ -283,7 +283,9 @@ discover_interfaces(int argc, char * const *argv)
                        p = ifa->ifa_name;
                }
 
-               ifp = xzalloc(sizeof(*ifp));
+               ifp = calloc(1, sizeof(*ifp));
+               if (ifp == NULL)
+                       return NULL;
                strlcpy(ifp->name, p, sizeof(ifp->name));
                ifp->flags = ifa->ifa_flags;