bug-inetutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[bug-inetutils] Re: ping: memory leaks


From: Debarshi 'Rishi' Ray
Subject: [bug-inetutils] Re: ping: memory leaks
Date: Sun, 22 Jul 2007 02:14:55 +0530

Here (both attached and inline) is a patch to fix this.

diff -urNp inetutils/ping/ping.c inetutils-build/ping/ping.c
--- inetutils/ping/ping.c       2007-07-16 00:01:53.000000000 +0530
+++ inetutils-build/ping/ping.c 2007-07-22 07:34:12.000000000 +0530
@@ -232,6 +232,7 @@ main (int argc, char **argv)
{
  int index;
  int one = 1;
+  int status;

  if (getuid () == 0)
    is_root = true;
@@ -263,7 +264,10 @@ main (int argc, char **argv)

  init_data_buffer (patptr, pattern_len);

-  return (*(ping_type)) (argc, argv);
+  status = (*(ping_type)) (argc, argv);
+  free (ping);
+  free (data_buffer);
+  return status;
}

int (*decode_type (const char *arg)) (int argc, char **argv)
@@ -385,6 +389,10 @@ ping_run (PING * ping, int (*finish) ())
          gettimeofday (&last, NULL);
        }
    }
+
+  free (ping->ping_buffer);
+  free (ping->ping_cktab);
+
  if (finish)
    return (*finish) ();
  return 0;
diff -urNp inetutils/ping/ping_echo.c inetutils-build/ping/ping_echo.c
--- inetutils/ping/ping_echo.c  2007-07-16 00:01:53.000000000 +0530
+++ inetutils-build/ping/ping_echo.c    2007-07-22 07:16:25.000000000 +0530
@@ -75,6 +75,8 @@ ping_echo (int argc, char **argv)
#ifdef IP_OPTIONS
  char rspace[3 + 4 * NROUTES + 1];     /* record route space */
#endif
+
+  int status;
  struct ping_stat ping_stat;

  if (options & OPT_FLOOD && options & OPT_INTERVAL)
@@ -110,7 +112,9 @@ ping_echo (int argc, char **argv)
          ping->ping_hostname,
          inet_ntoa (ping->ping_dest.sin_addr), data_length);

-  return ping_run (ping, echo_finish);
+  status = ping_run (ping, echo_finish);
+  free (ping->ping_hostname);
+  return status;
}

int
@@ -528,5 +532,5 @@ echo_finish ()
      printf ("round-trip min/avg/max/stddev = %.3f/%.3f/%.3f/%.3f ms\n",
              ping_stat->tmin, avg, ping_stat->tmax, nsqrt (vari, 0.0005));
    }
-  exit (ping->ping_num_recv == 0);
+  return (ping->ping_num_recv == 0);
}

Now we are left with the loss of 5 bytes, which seems to be argp's fault anyway.

address@hidden ping]$ sudo valgrind --tool=memcheck --leak-check=full
--show-reachable=yes ./ping localhost
Password:
......
==9617== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 17 from 1)
==9617== malloc/free: in use at exit: 5 bytes in 1 blocks.
==9617== malloc/free: 60 allocs, 59 frees, 6,825 bytes allocated.
......
==9617== 5 bytes in 1 blocks are definitely lost in loss record 1 of 1
==9617==    at 0x40054E5: malloc (vg_replace_malloc.c:149)
==9617==    by 0x782228: strndup (in /lib/libc-2.6.so)
==9617==    by 0x80503D7: xstrndup (xstrndup.c:33)
==9617==    by 0x804F9BF: base_name (basename.c:104)
==9617==    by 0x804F60A: argp_parse (argp-parse.c:561)
==9617==    by 0x8049940: main (ping.c:241)
==9617==
==9617== LEAK SUMMARY:
==9617==    definitely lost: 5 bytes in 1 blocks.
==9617==      possibly lost: 0 bytes in 0 blocks.
==9617==    still reachable: 0 bytes in 0 blocks.
==9617==         suppressed: 0 bytes in 0 blocks.
address@hidden ping]$

Happy hacking,
Debarshi
--
GPG key ID: 63D4A5A7
Key server: pgp.mit.edu

Attachment: ping-leak.diff
Description: Text Data


reply via email to

[Prev in Thread] Current Thread [Next in Thread]