[cfe-dev] ubsan false positives with float<->double NaN conversions?

Sean McBride sean at rogue-research.com
Tue Mar 19 17:42:31 PDT 2013


Hi all,

I'm not a language lawyer, but...

----------------
  float f = sqrtf(-1.0f);
  double d = (double)f;

  printf("%a -- %a\n", d, f);
----------------

$ clang++ -fsanitize=undefined Test.cxx

runtime error: value nan is outside the range of representable values of type 'double'

My quick googling of the C++ standard finds:

"4.6 Floating point promotion: A prvalue of type float can be converted to a prvalue of type double. The value is unchanged."

That suggests to me ubsan is wrong to complain.

ubsan warns about the opposite conversion too, that is:

----------------
  double d = sqrt(-1.0);
  float f = (float)d;
----------------

"4.8 Floating point conversions: A prvalue of floating point type can be converted to a prvalue of another floating point type. If the source value can be exactly represented in the destination type, the result of the conversion is that exact representation. If the source value is between two adjacent destination values, the result of the conversion is an implementation-defined choice of either of those values. Otherwise, the behavior is undefined."

This is less clear (to me anyway).  Is double-NaN exactly representable by float-NaN?  (I'd like to hope so, because otherwise -fsanitize=float-cast-overflow is going to be very noisy with various math/science codebases.)

Thanks,

-- 
____________________________________________________________
Sean McBride, B. Eng                 sean at rogue-research.com
Rogue Research                        www.rogue-research.com 
Mac Software Developer              Montréal, Québec, Canada






More information about the cfe-dev mailing list