[PATCH]: Proof-of-concept for dynamic format checking

Alan Modra amodra@bigpond.net.au
Fri Aug 19 01:08:00 GMT 2005


On Thu, Aug 18, 2005 at 10:35:22AM -0400, Kaveh R. Ghazi wrote:
>  > > I don't know how wedded to this style the bfd folks are
>  > 
>  > Not at all.  In fact I don't like it, even though I wrote the code.
>  > It would be great if _bfd_default_error_handler used the natural arg
>  > positions for %A and %B.  I couldn't think of a way to do that without
>  > incorporating a whole lot of knowledge about printf into the bfd
>  > function.
> 
> Right, in GCC we ended up doing that except we only implemented the
> bits of printf commonly used.  So for example we don't implement all
> of the specifiers (floating point) or modifers (%h) or flags.  In fact
> the fortran front end has a format that only has %d %i %c and %s from
> printf, (plus two custom specifiers.)  No flags or even length
> modifiers!
> 
> It's likely that bfd doesn't use a big chunk of printf that you could
> leave out as well.  (I haven't actually audited bfd though).

$ sed -n -e 's,[^%]*\(%[0-9\.# hlL+-]*.\)[^%]*,\1,gp' < bfd/po/bfd.pot | sed -e 's,%,\
%,g' | sort | uniq

%
%"
%-7ld
%.2x
%.8lx
%02X
%02x
%04lx
%08lx
%08x
%4d
%4lx
%4x
%A
%B
%X
%d
%i
%ld
%lu
%lx
%p
%s
%u
%x

(The '%"' is line wrapping in bfd.pot.  I may have missed a few format
specifiers because of that.  And the '%' is really '%%'.)

> Another option is to require positional specifiers for out of order
> arguments.  E.g.

Ick.

> So I favor rewriting _bfd_default_error_handler to do the safer thing
> which is to use natural arg positions.  Then create a format check
> with only the stuff you need, not the whole printf style.

I'm not motivated to do that myself. :)  There aren't that many places
that don't have %A or %B first in the format string.

$ sed -n -e 's,[^%]*\(%[0-9\.# hlL+-]*.\)[^%]*,\1,gp' < bfd/po/bfd.pot | grep '[^%AB]%[AB]'
%B%lx%A
%B%d%B%d
%s%d%d%B
%B%d%B%"
%B%d%B%d
%B%x%A
%B%s%s%A
%s%B
%s%B
%s%B
%s%B
%%%d%s%B%s%B
%s%B%s%B
%s%s%B%B
%s%B%A%B
%s%B%B
%s%B%A%B
%s%B%B%A
%B%lx%lx%lx%A
%u%s%B%u%B
%s%lu%B%lu%B
%B%s%s%B
%B%s%A
%X%s%A%B%A
%B%lx%A
%B%s%s%lx%A%lx

It's a great pity that vfprintf doesn't return its va_list arg.  If it
did, you could chop the format string into pieces and have vprintf
process the normal parts, consuming args as it goes.

-- 
Alan Modra
IBM OzLabs - Linux Technology Centre



More information about the Gcc mailing list