[cfe-dev] warning with conditional operator and printf, is this a bug ?

John McCall rjmccall at apple.com
Fri Nov 4 14:10:50 PDT 2011


On Nov 4, 2011, at 12:10 PM, Abramo Bagnara wrote:
> Il 04/11/2011 08:20, John McCall ha scritto:
>> It's a clear false positive in our format checker.  Promoting an unsigned value to a wider signed type performs a zero-extend, so the distinction between signed and unsigned formats is irrelevant and either %d or %ud should be acceptable.  (The reverse would not be true:  we should still warn about using unsigned formats with promoted signed types).
> 
> It is not clear to me how you intend to do that: conditional operator
> operands are promoted *before* to evaluate the operator. Unless you use
> some complex way to track the original type despite the promotions I
> don't see any way to avoid this warning.

> 
> Also I don't believe it is so clear that it is a false positive: indeed
> we are passing an integer argument to an unsigned short format.

The purpose of this warning is to warn about the possible
misformatting of a value or mishandling of varargs.  There is no
possibility of that here.  Therefore it's a false positive.

It's really  quite easy to track the expected range of a value
above and beyond its static type, and we do exactly that for
-Wconversion and -Wsign-compare.  Re-using that analysis
here would be simple.  It's not quite sufficient because we
should also be diagnosing varargs promotion/size problems,
but it's the right way to catch the misformatting issues.

John.



More information about the cfe-dev mailing list