[cfe-dev] Warn on invalid fp-to-int conversions?

David Chisnall via cfe-dev cfe-dev at lists.llvm.org
Mon Jan 23 02:42:43 PST 2017


On 20 Jan 2017, at 16:52, Hal Finkel via cfe-dev <cfe-dev at lists.llvm.org> wrote:
> 
> On 01/20/2017 10:49 AM, Krzysztof Parzyszek via cfe-dev wrote:
>> Hi,
>> Consider this program:
>> 
>> #include <math.h>
>> int foo() {
>>  int a = (int)INFINITY;
>>  int b = (int)NAN;
>>  return a+b;
>> }
>> 
>> It compiles without warnings (using -Wall), however neither of the conversions will result in anything useful. Would it be reasonable to have a warning about these kinds of conversions?
> 
> As I recall, these conversions, as well as any out-of-range conversions, are UB, and we should warn.

A little while ago, we replaced some of the overloaded stuff from math.h with _Generic in FreeBSD and found quite a few bugs in programs (including a number of configure scripts) calling math.h functions with integer arguments when they intended to provide floating point values.  For example, no one intentionally calls isnan() with an int argument (unless via a load of template / macro instantiations).  It would have been nice to have a warning for these conversions as well, but it’s not clear exactly when you should warn.

David




More information about the cfe-dev mailing list