aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJoão Valverde <joao.valverde@tecnico.ulisboa.pt>2017-10-29 19:22:39 +0000
committerJoão Valverde <j@v6e.pt>2017-10-29 21:10:23 +0000
commit62b870a72261f71f66ece9cb3b892d9999e805e3 (patch)
treeeadf2a0cb1769fb547147b9ec04454e07a2ec91d
parentbebd79aae9ccaf5c96666d38665b69484f1f3eda (diff)
Do not assert in ws_inet_ntop()
Change-Id: I9d420c5f6bc29ce94855017739169dc8e8ce4d48 Reviewed-on: https://code.wireshark.org/review/24173 Petri-Dish: João Valverde <j@v6e.pt> Tested-by: Petri Dish Buildbot Reviewed-by: João Valverde <j@v6e.pt>
-rw-r--r--wsutil/inet_addr.c16
-rw-r--r--wsutil/inet_addr.h12
2 files changed, 17 insertions, 11 deletions
diff --git a/wsutil/inet_addr.c b/wsutil/inet_addr.c
index b667c6d17a..dc3d054d50 100644
--- a/wsutil/inet_addr.c
+++ b/wsutil/inet_addr.c
@@ -23,6 +23,8 @@
#include "inet_addr.h"
+#include <errno.h>
+
#ifdef HAVE_ARPA_INET_H
#include <arpa/inet.h>
#endif
@@ -44,8 +46,8 @@
/*
* We only employ and require AF_INET/AF_INET6, so we can
- * have some stronger checks for correctness and convenience. It is a
- * programming error to pass a too-small buffer to inet_ntop.
+ * have some stronger checks for correctness and convenience (namely
+ * assert that EAFNOSUPPORT cannot happen).
*/
static inline gboolean
@@ -60,8 +62,14 @@ static inline const gchar *
_inet_ntop(int af, gconstpointer src, gchar *dst, guint dst_size)
{
const gchar *ret = inet_ntop(af, _NTOP_SRC_CAST_ src, dst, dst_size);
- g_assert(ret != NULL);
- return ret;
+ if (ret == NULL) {
+ g_assert(errno == ENOSPC);
+ /* set result to something that can't be confused with a valid conversion */
+ g_strlcpy(dst, "<<ENOSPC>>", dst_size);
+ /* set errno for caller */
+ errno = ENOSPC;
+ }
+ return dst;
}
const gchar *
diff --git a/wsutil/inet_addr.h b/wsutil/inet_addr.h
index 05670e8ae5..a3d45e25db 100644
--- a/wsutil/inet_addr.h
+++ b/wsutil/inet_addr.h
@@ -73,21 +73,19 @@
#endif
/*
- * 'dst_size' *must* be greater or equal to WS_INET_ADDRSTRLEN.
+ * To check for errors set errno to zero before calling ws_inet_ntop{4,6}.
+ * ENOSPC is set if the result exceeds the given buffer size.
*/
WS_DLL_PUBLIC WS_RETNONNULL const gchar *
ws_inet_ntop4(gconstpointer src, gchar *dst, guint dst_size);
-WS_DLL_PUBLIC gboolean
-ws_inet_pton4(const gchar *src, guint32 *dst);
-
-/*
- * 'dst_size' *must* be greater or equal to WS_INET6_ADDRSTRLEN.
- */
WS_DLL_PUBLIC WS_RETNONNULL const gchar *
ws_inet_ntop6(gconstpointer src, gchar *dst, guint dst_size);
WS_DLL_PUBLIC gboolean
+ws_inet_pton4(const gchar *src, guint32 *dst);
+
+WS_DLL_PUBLIC gboolean
ws_inet_pton6(const gchar *src, ws_in6_addr *dst);
#endif