From fc454736e3f228fff47e3da03694857a1da78455 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Tim=20R=C3=BChsen?= <tim.ruehsen@gmx.de>
Date: Mon, 17 Feb 2020 21:39:20 +0100
Subject: [PATCH 37/60] ping: Fix memleak in ping_set_packetsize

---
 ping/libping.c | 33 +++++++++++++++++++--------------
 ping/ping.h    |  2 +-
 2 files changed, 20 insertions(+), 15 deletions(-)

diff --git a/ping/libping.c b/ping/libping.c
index 9ae5d671..5217c31a 100644
--- a/ping/libping.c
+++ b/ping/libping.c
@@ -287,19 +287,20 @@ ping_set_packetsize (PING * ping, size_t size)
 }
 
 int
-ping_set_dest (PING * ping, char *host)
+ping_set_dest (PING * ping, const char *host)
 {
 #if HAVE_DECL_GETADDRINFO
   int rc;
   struct addrinfo hints, *res;
-  char *p;
+  char *rhost;
 
 # ifdef HAVE_IDN
-  rc = idna_to_ascii_lz (host, &p, 0);	/* P is allocated.  */
+  rc = idna_to_ascii_lz (host, &rhost, 0);	/* P is allocated.  */
   if (rc)
     return 1;
-# else /* !HAVE_IDN */
-  p = host;
+  host = rhost;
+#else
+  rhost = NULL;
 # endif
 
   memset (&hints, 0, sizeof (hints));
@@ -312,20 +313,24 @@ ping_set_dest (PING * ping, char *host)
   hints.ai_flags |= AI_CANONIDN;
 # endif
 
-  rc = getaddrinfo (p, NULL, &hints, &res);
+  rc = getaddrinfo (host, NULL, &hints, &res);
 
   if (rc)
-    return 1;
+    {
+      free (rhost);
+      return 1;
+    }
 
   memcpy (&ping->ping_dest.ping_sockaddr, res->ai_addr, res->ai_addrlen);
   if (res->ai_canonname)
     ping->ping_hostname = strdup (res->ai_canonname);
   else
-    ping->ping_hostname = strdup (p);
-
 # ifdef HAVE_IDN
-  free (p);
+    ping->ping_hostname = host;
+#else
+    ping->ping_hostname = strdup (host);
 # endif
+
   freeaddrinfo (res);
 
   return 0;
@@ -342,14 +347,14 @@ ping_set_dest (PING * ping, char *host)
     {
       struct hostent *hp;
 # ifdef HAVE_IDN
-      char *p;
+      char *rhost;
       int rc;
 
-      rc = idna_to_ascii_lz (host, &p, 0);
+      rc = idna_to_ascii_lz (host, &rhost, 0);
       if (rc)
 	return 1;
-      hp = gethostbyname (p);
-      free (p);
+      hp = gethostbyname (rhost);
+      free (rhost);
 # else /* !HAVE_IDN */
       hp = gethostbyname (host);
 # endif
diff --git a/ping/ping.h b/ping/ping.h
index 82fbddca..b2689d3c 100644
--- a/ping/ping.h
+++ b/ping/ping.h
@@ -25,7 +25,7 @@ PING *ping_init (int type, int ident);
 void ping_reset (PING * p);
 void ping_set_type (PING * p, int type);
 void ping_set_packetsize (PING * ping, size_t size);
-int ping_set_dest (PING * ping, char *host);
+int ping_set_dest (PING * ping, const char *host);
 int ping_set_pattern (PING * p, int len, unsigned char * pat);
 void ping_set_event_handler (PING * ping, ping_efp fp, void *closure);
 int ping_recv (PING * p);
-- 
2.26.0.292.g33ef6b2f38

