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

asn.c
dns.c
mtr.c
utils.c
utils.h

diff --git a/asn.c b/asn.c
index d31524b96d988a7fc2d731f90622c19b6acf20ac..6bc2f17fe9c70345fed22174880112df74585f2d 100644 (file)
--- a/asn.c
+++ b/asn.c
@@ -90,7 +90,7 @@ static char *ipinfo_lookup(const char *domain) {
     if((len = res_query(domain, C_IN, T_TXT, answer, PACKETSZ)) < 0) {
         if (iihash)
             DEB_syslog(LOG_INFO, "Malloc-txt: %s", UNKN);
-        return (iihash)?strdup(UNKN):UNKN;
+        return (iihash) ? xstrdup(UNKN) : UNKN;
     }
 
     pt = answer + sizeof(HEADER);
@@ -251,7 +251,7 @@ static char *get_ipinfo(struct mtr_ctl *ctl, ip_t *addr){
         if ((val = split_txtrec(ctl, ipinfo_lookup(lookup_key)))) {
             DEB_syslog(LOG_INFO, "Looked up: %s", key);
             if (iihash)
-                if ((item.key = strdup(key))) {
+                if ((item.key = xstrdup(key))) {
                     item.data = items;
                     hsearch(item, ENTER);
                     DEB_syslog(LOG_INFO, "Insert into hash: %s", key);
diff --git a/dns.c b/dns.c
index 6148b0b4f195714195e055e56307f5ce4e07365d..ab6324a72e7275650ccaf755f07ff6e85d895a41 100644 (file)
--- a/dns.c
+++ b/dns.c
@@ -239,7 +239,7 @@ extern void dns_ack(struct mtr_ctl *ctl)
     longipstr (host, &hostip, ctl->af);
     r = findip (ctl, &hostip);
     if (r)  
-      r->name = strdup (name);
+      r->name = xstrdup (name);
     else 
       error (0, 0, "dns_ack: Couldn't find host %s", host);
   }
diff --git a/mtr.c b/mtr.c
index 99f4cb070cfaffeecf969a88a460beeb1766b493..14502e386bb8a78e7540f4d9902d0f6cc265f4d5 100644 (file)
--- a/mtr.c
+++ b/mtr.c
@@ -154,7 +154,7 @@ append_to_names(const char* item) {
   if (name == NULL) {
     error(EXIT_FAILURE, errno, "memory allocation failure");
   }
-  name->name = strdup(item);
+  name->name = xstrdup(item);
   name->next = names;
   names = name;
 }
diff --git a/utils.c b/utils.c
index 2ba3654d788d19c3e20025200008add2609a5047..f6c432681cd4a593bbf64c84b7b55f2b781f24e5 100644 (file)
--- a/utils.c
+++ b/utils.c
@@ -97,3 +97,15 @@ extern void *xmalloc(const size_t size)
     error(EXIT_FAILURE, errno, "cannot allocate %zu bytes", size);
   return ret;
 }
+
+extern char *xstrdup(const char *str)
+{
+  char *ret;
+
+  if (!str)
+    return NULL;
+  ret = strdup(str);
+  if (!ret)
+    error(EXIT_FAILURE, errno, "cannot duplicate string: %s", str);
+  return ret;
+}
diff --git a/utils.h b/utils.h
index db3c0dd9a239b00df127470ec0627ed49adb77b1..5f9609d02827795a6235248e2a3953315da1a8a3 100644 (file)
--- a/utils.h
+++ b/utils.h
@@ -34,3 +34,4 @@ static inline void xstrncpy(char *dest, const char *src, size_t n)
 }
 
 extern void *xmalloc(const size_t size);
+extern char *xstrdup(const char *str);