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>
This commit is contained in:
João Valverde 2017-10-29 19:22:39 +00:00 committed by João Valverde
parent bebd79aae9
commit 62b870a722
2 changed files with 17 additions and 11 deletions

View File

@ -23,6 +23,8 @@
#include "inet_addr.h" #include "inet_addr.h"
#include <errno.h>
#ifdef HAVE_ARPA_INET_H #ifdef HAVE_ARPA_INET_H
#include <arpa/inet.h> #include <arpa/inet.h>
#endif #endif
@ -44,8 +46,8 @@
/* /*
* We only employ and require AF_INET/AF_INET6, so we can * We only employ and require AF_INET/AF_INET6, so we can
* have some stronger checks for correctness and convenience. It is a * have some stronger checks for correctness and convenience (namely
* programming error to pass a too-small buffer to inet_ntop. * assert that EAFNOSUPPORT cannot happen).
*/ */
static inline gboolean static inline gboolean
@ -60,8 +62,14 @@ static inline const gchar *
_inet_ntop(int af, gconstpointer src, gchar *dst, guint dst_size) _inet_ntop(int af, gconstpointer src, gchar *dst, guint dst_size)
{ {
const gchar *ret = inet_ntop(af, _NTOP_SRC_CAST_ src, dst, dst_size); const gchar *ret = inet_ntop(af, _NTOP_SRC_CAST_ src, dst, dst_size);
g_assert(ret != NULL); if (ret == NULL) {
return ret; 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 * const gchar *

View File

@ -73,20 +73,18 @@
#endif #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_DLL_PUBLIC WS_RETNONNULL const gchar *
ws_inet_ntop4(gconstpointer src, gchar *dst, guint dst_size); 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_DLL_PUBLIC WS_RETNONNULL const gchar *
ws_inet_ntop6(gconstpointer src, gchar *dst, guint dst_size); 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_DLL_PUBLIC gboolean
ws_inet_pton6(const gchar *src, ws_in6_addr *dst); ws_inet_pton6(const gchar *src, ws_in6_addr *dst);