From: Sami Kerola Date: Mon, 29 Aug 2016 17:53:32 +0000 (+0100) Subject: reliability: always check malloc() return value X-Git-Tag: v0.88~26^2~19 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1b3558666f59988581a1d2e4181a21d4d530ccf2;p=thirdparty%2Fmtr.git reliability: always check malloc() return value Exit if allocation fails. --- diff --git a/asn.c b/asn.c index a8491c4..d31524b 100644 --- a/asn.c +++ b/asn.c @@ -132,8 +132,7 @@ static char *ipinfo_lookup(const char *domain) { txtlen = NAMELEN; if (iihash) { - if (!(txt = malloc(txtlen + 1))) - return NULL; + txt = xmalloc(txtlen + 1); } else txt = (char*)txtrec; diff --git a/dns.c b/dns.c index 4589886..6148b0b 100644 --- a/dns.c +++ b/dns.c @@ -40,6 +40,7 @@ #include "mtr.h" #include "dns.h" #include "net.h" +#include "utils.h" struct dns_results { ip_t ip; @@ -275,7 +276,7 @@ extern char *dns_lookup2(struct mtr_ctl *ctl, ip_t * ip) else return strlongip (ctl, ip); } else { - r = malloc (sizeof (struct dns_results)); + r = xmalloc (sizeof (struct dns_results)); //r->ip = *ip; memcpy (&r->ip, ip, sizeof (r->ip)); r->name = NULL; diff --git a/net.c b/net.c index 1227017..1cd97a3 100644 --- a/net.c +++ b/net.c @@ -259,9 +259,7 @@ static int udp_checksum(struct mtr_ctl *ctl, void *pheader, void *udata, char *csumpacket; int ret; - csumpacket = malloc(tsize); - if (!csumpacket) - error(EXIT_FAILURE, errno, "cannot allocate %zu bytes", tsize); + csumpacket = xmalloc(tsize); memset(csumpacket, (unsigned char) abs(ctl->bitpattern), tsize); if (alt_checksum && dsize >= 2) { csumpacket[psize + sizeof(struct UDPHeader)] = 0; @@ -684,7 +682,7 @@ static void net_send_query(struct mtr_ctl *ctl, int index) case IPPROTO_UDP: /* checksum is not mandatory. only calculate if we know ip->saddr */ if (udp->checksum) { - udpp = (struct UDPv4PHeader *)(malloc(sizeof(struct UDPv4PHeader))); + udpp = (struct UDPv4PHeader *)(xmalloc(sizeof(struct UDPv4PHeader))); udpp->saddr = ip->saddr; udpp->daddr = ip->daddr; udpp->protocol = ip->protocol; @@ -693,7 +691,7 @@ static void net_send_query(struct mtr_ctl *ctl, int index) packet[iphsize + sizeof(struct UDPHeader)] = ((char *)&checksum_result)[0]; packet[iphsize + sizeof(struct UDPHeader) + 1] = ((char *)&checksum_result)[1]; } else if (ip->saddr) { - udpp = (struct UDPv4PHeader *)(malloc(sizeof(struct UDPv4PHeader))); + udpp = (struct UDPv4PHeader *)(xmalloc(sizeof(struct UDPv4PHeader))); udpp->saddr = ip->saddr; udpp->daddr = ip->daddr; udpp->protocol = ip->protocol; diff --git a/utils.c b/utils.c index b1fef50..2ba3654 100644 --- a/utils.c +++ b/utils.c @@ -88,3 +88,12 @@ extern float strtofloat_or_err(const char *str, const char *errmesg) error(EXIT_FAILURE, errno, "%s: '%s'", errmesg, str); return 0; } + +extern void *xmalloc(const size_t size) +{ + void *ret = malloc(size); + + if (!ret && size) + error(EXIT_FAILURE, errno, "cannot allocate %zu bytes", size); + return ret; +} diff --git a/utils.h b/utils.h index 4a39fd0..db3c0dd 100644 --- a/utils.h +++ b/utils.h @@ -32,3 +32,5 @@ static inline void xstrncpy(char *dest, const char *src, size_t n) strncpy(dest, src, n - 1); dest[n - 1] = 0; } + +extern void *xmalloc(const size_t size);