Patchwork iputils: fix memory leaks

login
register
mail settings
Submitter jb
Date March 7, 2013, 9:52 a.m.
Message ID <loom.20130307T103516-549@post.gmane.org>
Download mbox | patch
Permalink /patch/225786/
State Not Applicable
Delegated to: David Miller
Headers show

Comments

jb - March 7, 2013, 9:52 a.m.
Fix memory leaks
    
    Signed-off-by: JB <jb.1234abcd@gmail.com>
---
 ping.c | 9 +++++++++
 1 file changed, 9 insertions(+)




--
To unsubscribe from this list: send the line "unsubscribe netdev" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
David Laight - March 7, 2013, 10:06 a.m.
>     Fix memory leaks
...
> +#ifdef USE_IDN
> +                               free(idn);
> +                               idn = NULL;
> +#endif
>                                 exit(2);

There is no point calling free() before exit(), nor any need
to free items that aren't allocated inside loops.

	David



--
To unsubscribe from this list: send the line "unsubscribe netdev" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
jb - March 7, 2013, 11:18 a.m.
David Laight <David.Laight <at> ACULAB.COM> writes:

> 
> >     Fix memory leaks
> ...
> > +#ifdef USE_IDN
> > +                               free(idn);
> > +                               idn = NULL;
> > +#endif
> >                                 exit(2);
> 
> There is no point calling free() before exit(), nor any need
> to free items that aren't allocated inside loops.
> 
> 	David

A matter of good style ? Overzealous ?
Flame ?
http://stackoverflow.com/questions/654754/what-really-happens-when-you-dont-free-after-malloc
jb



--
To unsubscribe from this list: send the line "unsubscribe netdev" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Patch

diff --git a/ping.c b/ping.c
index c0366cd..04f5596 100644
--- a/ping.c
+++ b/ping.c
@@ -282,10 +282,15 @@  main(int argc, char **argv)
                        hp = gethostbyname(idn);
                        if (!hp) {
                                fprintf(stderr, "ping: unknown host %s\n", target);
+#ifdef USE_IDN
+                               free(idn);
+                               idn = NULL;
+#endif
                                exit(2);
                        }
 #ifdef USE_IDN
                        free(idn);
+                       idn = NULL;
 #endif
                        memcpy(&whereto.sin_addr, hp->h_addr, 4);
 #ifdef USE_IDN
@@ -301,6 +306,10 @@  main(int argc, char **argv)
                        hnamebuf[sizeof(hnamebuf) - 1] = 0;
 #endif
                        hostname = hnamebuf;
+#ifdef USE_IDN
+                       free(hnamebuf);
+                       hnamebuf = NULL;
+#endif
                }
                if (argc > 1)
                        route[nroute++] = whereto.sin_addr.s_addr;