From: Roy Marples Date: Fri, 15 Feb 2013 21:45:08 +0000 (+0000) Subject: Remove xzalloc function X-Git-Tag: v5.99.6~73 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=10e17e3f635f816cbd8717ced563b4edfe2f5aea;p=thirdparty%2Fdhcpcd.git Remove xzalloc function --- diff --git a/common.c b/common.c index 15fe3771..a3e9aee6 100644 --- 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; -} diff --git a/common.h b/common.h index 37b7a2c8..af390937 100644 --- 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 962cae91..c0749808 100644 --- 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 diff --git a/dhcpcd.c b/dhcpcd.c index d2a7b875..ad294553 100644 --- 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"); diff --git a/if-linux.c b/if-linux.c index e1c1ff6b..3240e388 100644 --- a/if-linux.c +++ b/if-linux.c @@ -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; diff --git a/if-options.c b/if-options.c index ebabb5dd..47a1a514 100644 --- a/if-options.c +++ b/if-options.c @@ -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; diff --git a/ipv4ll.c b/ipv4ll.c index 970293f2..d2e069fc 100644 --- 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 fc15883d..5c2f3503 100644 --- 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; diff --git a/ipv6rs.c b/ipv6rs.c index ac30346c..a1bd9d14 100644 --- 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 e66b609b..028dd9fa 100644 --- 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;