1 From b24b19f719fdd9c3d61a0c93552cd64d832d964c Mon Sep 17 00:00:00 2001
2 From: Stephen Hemminger <shemminger@vyatta.com>
3 Date: Tue, 6 Dec 2011 14:09:18 +0400
4 Subject: [PATCH] lib: fix memory leak on connect() failure
6 Change sockunion_log() to not use strdup(). This fixes a small memory
7 leak that occurs on every failed connect(), and is simpler/cleaner.
9 lib/sockunion.c | 23 +++++++++++------------
10 1 files changed, 11 insertions(+), 12 deletions(-)
14 @@ -297,27 +297,24 @@ sockunion_sizeof (union sockunion *su)
17 /* return sockunion structure : this function should be revised. */
19 -sockunion_log (union sockunion *su)
21 +sockunion_log (union sockunion *su, char *buf, size_t len)
23 - static char buf[SU_ADDRSTRLEN];
25 switch (su->sa.sa_family)
28 - snprintf (buf, SU_ADDRSTRLEN, "%s", inet_ntoa (su->sin.sin_addr));
30 + return inet_ntop(AF_INET, &su->sin.sin_addr, buf, len);
34 - snprintf (buf, SU_ADDRSTRLEN, "%s",
35 - inet_ntop (AF_INET6, &(su->sin6.sin6_addr), buf, SU_ADDRSTRLEN));
36 + return inet_ntop(AF_INET6, &(su->sin6.sin6_addr), buf, len);
38 #endif /* HAVE_IPV6 */
41 - snprintf (buf, SU_ADDRSTRLEN, "af_unknown %d ", su->sa.sa_family);
43 + snprintf (buf, len, "af_unknown %d ", su->sa.sa_family);
46 - return (XSTRDUP (MTYPE_TMP, buf));
49 /* sockunion_connect returns
50 @@ -379,8 +376,10 @@ sockunion_connect (int fd, union sockuni
52 if (errno != EINPROGRESS)
54 + char str[SU_ADDRSTRLEN];
55 zlog_info ("can't connect to %s fd %d : %s",
56 - sockunion_log (&su), fd, safe_strerror (errno));
57 + sockunion_log (&su, str, sizeof str),
58 + fd, safe_strerror (errno));