Huge thanks to our Platinum Members Endace and LiveAction,
and our Silver Member Veeam, for supporting the Wireshark Foundation and project.

Wireshark-dev: [Wireshark-dev] Val_to_str as a macro

From: Evan Huus <eapache@xxxxxxxxx>
Date: Wed, 11 Dec 2013 17:22:31 -0500
I've been exploring a few options for the val_to_str function and
wmem. One of the tangential things I've been trying to achieve is to
make it into a macro so that the compiler will catch format-string
mismatches (which have been a source of bugs in the past). This is
easy to do inefficiently:

#define val_to_str(VAL, VS, POOL, FMT) (try_val_to_str(VAL, VS) ?
try_val_to_str(VAL, VS) : wmem_strdup_printf(POOL, FMT, VAL))

but I've been unable to find a nice way to avoid the second call to
try_val_to_str(). I can wrap it in a block and use a temporary
variable, but then it can't return a value and no longer behaves like
the original function. I can use a single temp global, but that's just
ugly and won't work if we ever do multithreading.

Can anybody else think of a nice way to do this, or is it just one of
those things that's impossible in C?

Evan

P.S. Languages like python will do what I want with expressions of the
form (a() || b()), but not C.