diff --git a/lib/hostip.c b/lib/hostip.c
index c06961e7907334c33eeaede9d301bbf9622d2308..21227567e2bf13329784740bf74e7475c6367ee5 100644
--- a/lib/hostip.c
+++ b/lib/hostip.c
@@ -344,6 +344,11 @@ int Curl_resolv(struct connectdata *conn,
       
       if(data->share)
         Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
+
+      if(!dns) {
+        /* returned failure, bail out nicely */
+        Curl_freeaddrinfo(addr);
+      }
     }
   }