Avoid asserting with EAFNOSUPPORT

We require support for AF_INET/AF_INET6 to run and assume this is available
on all supported platforms. If and when reality clashes with that assumption just
avoid aborting. Errors are to be expected as long as they don't produce a crash.

Change-Id: I5c107b1a8fd64441eb96f48381412e180b66feb7
Reviewed-on: https://code.wireshark.org/review/24187
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-30 08:42:26 +00:00 committed by João Valverde
parent e4ac91091d
commit 9a464fd9bd
1 changed files with 34 additions and 9 deletions

View File

@ -20,10 +20,10 @@
*/
#include "config.h"
#include "inet_addr.h"
#include <errno.h>
#include <string.h>
#ifdef HAVE_ARPA_INET_H
#include <arpa/inet.h>
@ -45,16 +45,29 @@
#endif
/*
* We only employ and require AF_INET/AF_INET6, so we can
* have some stronger checks for correctness and convenience (namely
* assert that EAFNOSUPPORT cannot happen).
* We assume and require an inet_pton/inet_ntop that supports AF_INET
* and AF_INET6.
*/
static inline gboolean
_inet_pton(int af, const gchar *src, gpointer dst)
{
gint ret = inet_pton(af, src, dst);
g_assert(ret >= 0);
if (G_UNLIKELY(ret < 0)) {
/* EAFNOSUPPORT */
if (af == AF_INET) {
memset(dst, 0, sizeof(struct in_addr));
g_critical("ws_inet_pton4: EAFNOSUPPORT");
}
else if (af == AF_INET6) {
memset(dst, 0, sizeof(struct in6_addr));
g_critical("ws_inet_pton6: EAFNOSUPPORT");
}
else {
g_assert(0);
}
errno = EAFNOSUPPORT;
}
return ret == 1;
}
@ -62,12 +75,24 @@ 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);
if (ret == NULL) {
g_assert(errno == ENOSPC);
if (G_UNLIKELY(ret == NULL)) {
int saved_errno = errno;
gchar *errmsg = "<<ERROR>>";
switch (errno) {
case EAFNOSUPPORT:
errmsg = "<<EAFNOSUPPORT>>";
g_critical("ws_inet_ntop: EAFNOSUPPORT");
break;
case ENOSPC:
errmsg = "<<ENOSPC>>";
break;
default:
break;
}
/* set result to something that can't be confused with a valid conversion */
g_strlcpy(dst, "<<ENOSPC>>", dst_size);
g_strlcpy(dst, errmsg, dst_size);
/* set errno for caller */
errno = ENOSPC;
errno = saved_errno;
}
return dst;
}