Skip to contentSkip to conversation

iputils: fix memory leaks

PATCH In review jb 2 participants 3 messages Mar 7, 2013

Subsystems (2)
lore
Share & tools
lore

jb

author
#0 lore raw

PATCH 1/1 iputils: fix memory leaks

Fix memory leaks

---
 ping.c | 9 +++++++++
 1 file changed, 9 insertions(+)
ping.c +9 -0
index c0366cd..04f5596 100644
@@ -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;
#1 lore raw
Quoted 1 line

Fix memory leaks

...

ping.c main(int argc, char **argv) · line 289

Quoted 5 lines
ping.c +4 -0
+#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

author
#2 lore raw
Quoted 12 lines · David Laight <David.Laight <at> ACULAB.COM> writes:
Quoted 2 lines

Fix memory leaks

...

ping.c main(int argc, char **argv) · line 289

Quoted 5 lines
ping.c +4 -0
+#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.

Quoted 1 line

David

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