ANNOUNCEMENT: Live Wireshark University & Allegro Packets online APAC Wireshark Training Session
April 17th, 2024 | 14:30-16:00 SGT (UTC+8) | Online

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

From: Jakub Zawadzki <darkjames-ws@xxxxxxxxxxxx>
Date: Wed, 11 Dec 2013 23:40:16 +0100
Hi,

On Wed, Dec 11, 2013 at 05:22:31PM -0500, Evan Huus wrote:
> 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.

We could use GCC extension: http://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html
(MSVC can't check format types anyway).

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

Or more portable: I'd try moving val_to_str() as inline function to value_string.h

and later just to test with something like: val_to_str(0, NULL, "%s");

Kuba.