bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Bug-gnulib] Re: getaddrinfo


From: Bruno Haible
Subject: Re: [Bug-gnulib] Re: getaddrinfo
Date: Wed, 10 Nov 2004 13:54:36 +0100
User-agent: KMail/1.5

Paul Eggert wrote:
> Simon Josefsson <address@hidden> writes:
> > Btw, this construct:
> >
> >   if (hints
> > #if HAVE_IPV4
> >       && hints->ai_family != PF_INET
> > #endif
> > #if HAVE_IPV6
> >       && hints->ai_family != PF_INET6
> > #endif
> >       && hints->ai_family != PF_UNSPEC)
> >     /* FIXME: Support more families. */
> >     return EAI_FAMILY;
>
> it's really ugly.

But it presents the intent clearly: If the platform has IPV6, we test
for PF_INET6. Whereas this:

> #ifndef PF_INET
> # define PF_INET 0.5
> #endif
> #ifndef PF_INET6
> # define PF_INET6 0.5
> #endif
>
>   if (hints
>       && hints->ai_family != PF_INET
>       && hints->ai_family != PF_INET6
>       && hints->ai_family != PF_UNSPEC)

not only obfuscates the intent of code (it looks aesthetically better
but is more confusing), but also leaves a useless floating-point
computation in the code path. (Yes, gcc does not eliminate the
useless comparisons.)

If you want to avoid #if conditionals inside expressions, I suggest to
change the expression into multiple statements:

static inline bool
validate_family (int family)
{
#if HAVE_IPV4
  if (family == PF_INET)
    return true;
#endif
#if HAVE_IPV6
  if (family == PF_INET6)
    return true;
#endif
  if (family == PF_UNSPEC)
    return true;
  return false;
}

  if (hints && !validate_family (hints->ai_family))
    return EAI_FAMILY;

Bruno





reply via email to

[Prev in Thread] Current Thread [Next in Thread]