[SJF Logo]
Steve Friedl's Weblog

September 15, 2002
GNU C __attribute__

This morning I spent too much time tracking down a bug in my Kodak 8500 Photo Printer Ghostscript driver, and it ended up being a mismatch between the format and parameters on a printf-like function. This can normally happen to anybody, but not to me, because I always use the GNU __attribute__ mechanism to tell the compiler how to check this out, but I forgot this time. Doh!

GNU C compilers support a __attribute__ keyword that allows associating "attributes" with function declarations, and one of these tells the compiler that the function is printf-like, and it will perform substantial inspection of the parameters and format string. It's a lifesaver, and easy to set up.

Just above the printf-like function definition, I added a new declaration that included the proper __attribute__ syntax. This done, the compiler will inspect all the calls to writecmd and will let me know when I mismatch a string with an int or simply forget a parameter.

static int writecmd(FILE *ofp, const char *format, ... )        __attribute__((format(printf, 2, 3)));

static int writecmd(FILE *ofp, const char *format, ... )
{
va_list args;
char buf[64];

    va_start(args, format);
    vsprintf(buf, format, args);
    va_end(args);
...

I've written up a Tech Tip on this: Using GNU C __attribute__

Posted by Steve at September 15, 2002 03:07 PM | TrackBack
Comments
Post a comment
Name:


Email Address:


URL:


Comments:


Remember info?