[OPEN-ILS-DEV] C nits: invalid conversion specification

Scott McKellar mck9 at swbell.net
Sun May 6 23:41:33 EDT 2007


In my copy of the code base (slightly out of date by now) I have
found 17 occurrences of the conversion specification "%lf" for
printf-style formatting.

So far as I can tell, this conversion specification is unrecognized
by the Standard, and therefore invokes undefined behavior.  In 
practice, gcc appears silently to ignore the extraneous length 
modifier 'l', but it is not required to.  There is no assurance that 
any other compiler, or any other version of gcc, will behave the 
same way.

The obvious intent is to format a double.  There is presumably a
misconception that one should use "%f" for a float and "%lf" for a
double, just as one uses "%d" for an int and "%ld" for a long.

Such an analogy is faulty.  In fact there is no conversion 
specifier for floats.  There doesn't need to be, because printf()
and it's kindred will never see a float.  If you try to pass a float,
it will be promoted to a double before the called function ever sees
it.  

The usual conversion specifier for a double is simply '%f', possibly
modified for width, precision, and so forth.  For a long double, of
course, you can use "%Lf", and I saw a couple of those.

If you like, I can prepare a collection of patches, but that seems
like overkill for such a trivial fix.  The best way to identify the
offending lines is to execute the following command from the ILS
directory:

    grep -r '%lf' * | grep '\.[ch]:'

This command reports .c and .h files.  I also saw some Perl modules
containing "%lf", but I don't know enough about Perl to know whether 
it's valid.

Scott McKellar
http://home.swbell.net/mck9/aargh/



More information about the Open-ils-dev mailing list