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

David Blaikie dblaikie at gmail.com
Wed Feb 27 11:18:25 PST 2013


On Wed, Feb 27, 2013 at 10:55 AM, Sean McBride <sean at rogue-research.com> wrote:
> Hi all,
>
> Consider this C++:
>
> -------------------
> #include <stdint.h>
>
> int main (void)
> {
>   double d = 255.5;
>   uint8_t c = (uint8_t)d;
>
>   return c;
> }
> -------------------
>
> then:
>
> $ 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?

I tend to agree with you, +Richard Smith to make him aware of/get his opinion.




More information about the cfe-dev mailing list