]> git.puffer.fish Git - matthieu/frr.git/commitdiff
lib: make sockunion2str safer to use
authorTimo Teräs <timo.teras@iki.fi>
Sat, 23 May 2015 08:08:38 +0000 (11:08 +0300)
committerDaniel Walton <dwalton@cumulusnetworks.com>
Thu, 26 May 2016 15:33:30 +0000 (15:33 +0000)
It's mostly used for logging, and the return value is never
checked, so try to make it valid.

Signed-off-by: Timo Teräs <timo.teras@iki.fi>
Signed-off-by: David Lamparter <equinox@opensourcerouting.org>
(cherry picked from commit 53009d387a633997b16d32224b50451b5c81b61a)

lib/sockunion.c

index fdff61f542cc587d37b1afa230255a2d7d439445..4fd5f1444c39635bf445c648dcf44ab3016bb810 100644 (file)
@@ -166,13 +166,20 @@ str2sockunion (const char *str, union sockunion *su)
 const char *
 sockunion2str (union sockunion *su, char *buf, size_t len)
 {
-  if  (su->sa.sa_family == AF_INET)
-    return inet_ntop (AF_INET, &su->sin.sin_addr, buf, len);
+  switch (sockunion_family(su))
+    {
+    case AF_UNSPEC:
+      snprintf (buf, len, "(unspec)");
+      return buf;
+    case AF_INET:
+      return inet_ntop (AF_INET, &su->sin.sin_addr, buf, len);
 #ifdef HAVE_IPV6
-  else if (su->sa.sa_family == AF_INET6)
-    return inet_ntop (AF_INET6, &su->sin6.sin6_addr, buf, len);
+    case AF_INET6:
+      return inet_ntop (AF_INET6, &su->sin6.sin6_addr, buf, len);
 #endif /* HAVE_IPV6 */
-  return NULL;
+    }
+  snprintf (buf, len, "(af %d)", sockunion_family(su));
+  return buf;
 }
 
 union sockunion *