printf() format-string parser wanted! 
Author Message
 printf() format-string parser wanted!



Quote:
>Does anyone out there have a parser to validate printf() style
>statements (ie. all those that take the format string in the
>standard library) to check that the number of arguments matches
>the format-string supplied?

Yes; on my machine, it's called

'gcc -Wformat'.  :-)

(Actually, I'd recommend -Wall in such a case.  Gcc is pretty good
about this kind of thing).
--

The joy of engineering is to find a straight line on a double
logarithmic diagram.



Sat, 29 Mar 1997 03:38:35 GMT  
 printf() format-string parser wanted!
Folks,

Does anyone out there have a parser to validate printf() style
statements (ie. all those that take the format string in the
standard library) to check that the number of arguments matches
the format-string supplied?  This pre-supposes that the format
string is a literal, naturally.

In order to be useful, I reckon it should be able to manage 95%
of such statements and flag the remainder.  To help understand
the extent of the task, here are a few hurdles that would need
to be overcome...

    format strings with any conversion specifiers (even the '*' ?)
    statments split over lines
    parameters that are functions themselves (inc. member functions)
    #define'd literal format strings

Surely someone somewhere has written such a program?

Ever hopefully,

Andrew.

PS. I realise this sort of thing *should* be obvious for anyone
with an iota of C programming experience, but in a project with
1,000,000s of lines of other people's code it becomes tiresome
to look oneself!

PPS. These bugs aren't don't always manifest themselves when the
program is first run: the most dangerous statements are those that
are executed (and therefore tested) the least frequently!



Sat, 29 Mar 1997 02:06:08 GMT  
 printf() format-string parser wanted!


Quote:
> Folks,

> Does anyone out there have a parser to validate printf() style

                                                           ^^^^^

Quote:
> statements (ie. all those that take the format string in the
> standard library) to check that the number of arguments matches
> the format-string supplied?  This pre-supposes that the format
> string is a literal, naturally.

Someone else (I think it was Koenig?) already mentioned you should
try gcc, or if you live on a PC: djgpp. I would like to add that
you can easily use its checking mechanism to check format strings
in other functions: if a function has the same argument list and
return type as printf(), compile it with

  #ifdef CHECK_FORMAT_STRINGS
  #define your_function printf
  #define another_func  scanf
  #endif

and don't forget to undefine CHECK_FORMAT_STRINGS when you want to
generate your executable. You could put -DCHECK_FORMAT_STRINGS in the
CFLAGS in a special makefile called, say, Makefile.check. When you
want to check, you'd run

  make -f Makefile.check

You could even make a macro that you could add right below each
prototype:

  #ifdef CHECK_FORMAT_STRINGS
  #define FORMATLIKE(x,y) y
  #else
  #define FORMATLIKE(x,y) x
  #endif

  int your_function (char *, ...);
  FORMATLIKE (your_function, printf)

Well, just a thought...

--
                                       ^^

University of Twente                 =x=  \        tel. +31 53 893747
Tele-Informatics & Open Systems        |   \       tfx. +31 53 333815
P.O. Box 217   7500 AE Enschede       /|__  \
The Netherlands                      (____)_/

Fortune's Real-Life Courtroom Quote #52:

Q:  What is your name?



Sat, 29 Mar 1997 20:36:20 GMT  
 printf() format-string parser wanted!

Quote:


>> Folks,

>> Does anyone out there have a parser to validate printf() style
>                                                           ^^^^^
>> statements (ie. all those that take the format string in the
>> standard library) to check that the number of arguments matches
>> the format-string supplied?  This pre-supposes that the format
>> string is a literal, naturally.
>Someone else (I think it was Koenig?) already mentioned you should
>try gcc, or if you live on a PC: djgpp. I would like to add that
>you can easily use its checking mechanism to check format strings
>in other functions: if a function has the same argument list and
>return type as printf(), compile it with
>[ Something hideous to get gcc to check the format of your
> printf-esque functions]

You can do this in a way that is considerably less ugly, but
unfortunately less portable to other compilers than gcc.

From the gcc info file:
===
format (ARCHETYPE, STRING-INDEX, FIRST-TO-CHECK)'
     The `format' attribute specifies that a function takes `printf'
or
     `scanf' style arguments which should be type-checked against a
     format string.  For example, the declaration:

          extern int
          my_printf (void *my_object, const char *my_format, ...)
                __attribute__ ((format (printf, 2, 3)));

     causes the compiler to check the arguments in calls to `my_printf'
     for consistency with the `printf' style format string argument
     `my_format'.
===
I suggest something like:
/* foo.h */

#ifdef __GNUC__
#define ATTRIBUTE(x) __attribute__ x
#else
#define ATTRIBUTE(x) /*NOTHING*/
#endif

extern int my_printf(void *my_object, const char *my_format, ...)
  ATTRIBUTE(((format (printf, 2, 3))));

This is off the top of my head, but should allow the same header file
to work properly with gcc and other C compilers.  And you don't need
to use a special compiler target to check this stuff when you're using
gcc.

Jeff
--
____ "And if I smile please tell me some bad news
\BI/  before I laugh and act like a fool"
 \/            -The Who "Behind Blue Eyes"
grep -vi obik       Running Linux 1.1 -- Free Unix for 386+ machines



Sun, 30 Mar 1997 01:41:51 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Format string as a parameter to printf()?

2. printf format string: how bad style is this?

3. printf -*s format string

4. Examples of format strings for scanf/printf ?

5. printf and formatting numeric strings

6. placing output of printf in memory: formatting strings

7. printf and variable length string format (asterisk)

8. Printf format strings

9. printf Format string

10. Source to format strings like printf()

11. STL strings & printf-type formatting

12. HTML parser wanted

 

 
Powered by phpBB® Forum Software