cURL / Mailing Lists / curl-library / Single Mail

curl-library

Re: printf-style format strings

From: Jamie Lokier <jamie_at_shareable.org>
Date: Thu, 28 Jan 2010 11:12:44 +0000

Yang Tse wrote:
> When the statement involved is actually a printf-family one some
> compilers will warn us about mismatched usage. But they will miss our
> infof() and failf() macros (actually Curl_infof() and Curl_failf()
> functions) and others which also use printf-style format strings.
>
> Manually verifying all usages of these is prohibitively
> time-consuming, and even if done, later changes of variables data
> types which do not verify if used in print-like statements would bring
> breakage back again.
>
> So. I wonder if someone has some some neat idea or code that could be
> used to verify if the printf-style format string used matches the one
> required for the data type being used or not.

Look up GCC's __attribute__((__format__(__printf__(...))). It does
exactly what you asked for, assuming the format string styles are the
same as the platform's own printf.

It won't work on non-GCC platforms, but building on GCC platforms is
usually enough to ensure the code is checked.

The attribute is available starting from GCC 2.7. From GCC 3.3 you
can add __attribute__((__nonnull__(...))) too.

-- Jamie
-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette: http://curl.haxx.se/mail/etiquette.html
Received on 2010-01-28