[cfe-dev] sanitize float-cast-overflow false positive with 255.5 to uint8_t?

Sean McBride sean at rogue-research.com
Wed Feb 27 10:55:07 PST 2013

Hi all,

Consider this C++:

#include <stdint.h>

int main (void)
  double d = 255.5;
  uint8_t c = (uint8_t)d;
  return c;


$ clang++ -fsanitize=float-cast-overflow test.cxx

runtime error: value 255.5 is outside the range of representable values of type 'unsigned char'

Now, I'm no language lawyer, but the standard seems to say "An rvalue of a floating point type can be converted to an rvalue of an integer type. The conversion truncates; that is, the fractional part is discarded. The behavior is undefined if the ***truncated*** value cannot be represented in the destination type."  (emphasis mine)

So is clang wrong to complain here?


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