]> git.ipfire.org Git - thirdparty/mtr.git/commitdiff
reliability: always check malloc() return value
authorSami Kerola <kerolasa@iki.fi>
Mon, 29 Aug 2016 17:53:32 +0000 (18:53 +0100)
committerSami Kerola <kerolasa@iki.fi>
Mon, 29 Aug 2016 17:58:07 +0000 (18:58 +0100)
Exit if allocation fails.

asn.c
dns.c
net.c
utils.c
utils.h

diff --git a/asn.c b/asn.c
index a8491c413adbca49b00d44319122237f4a48a9a5..d31524b96d988a7fc2d731f90622c19b6acf20ac 100644 (file)
--- 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 458988672cf56dd5f3281788def8cac08645752f..6148b0b4f195714195e055e56307f5ce4e07365d 100644 (file)
--- 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 122701733f4964923e56a19d2694e429f8489323..1cd97a3f6b43231df729c3feacd0c22821abf0d1 100644 (file)
--- 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 b1fef50222a839c3eb8470c5be244679f46b6935..2ba3654d788d19c3e20025200008add2609a5047 100644 (file)
--- 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 4a39fd055c863935fb94743d6a0738f490d25d15..db3c0dd9a239b00df127470ec0627ed49adb77b1 100644 (file)
--- 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);