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

Richard Smith richard at metafoo.co.uk
Wed Feb 27 13:18:08 PST 2013


On Wed, Feb 27, 2013 at 11:18 AM, David Blaikie <dblaikie at gmail.com> wrote:

> 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.
>

Yes, I also agree. Please file a bug!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20130227/955274a1/attachment.html>


More information about the cfe-dev mailing list