This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: Warning for converting (possibly) negative float/double to unsigned int
- From: Jakub Jelinek <jakub at redhat dot com>
- To: Andrew Haley <aph at redhat dot com>
- Cc: Bradley Lucier <lucier at math dot purdue dot edu>, gcc at gcc dot gnu dot org
- Date: Sat, 27 Feb 2016 12:53:53 +0100
- Subject: Re: Warning for converting (possibly) negative float/double to unsigned int
- Authentication-results: sourceware.org; auth=none
- References: <56D0C36A dot 5060303 at math dot purdue dot edu> <56D17CFF dot 5080600 at redhat dot com>
- Reply-to: Jakub Jelinek <jakub at redhat dot com>
On Sat, Feb 27, 2016 at 10:39:59AM +0000, Andrew Haley wrote:
> On 26/02/16 21:28, Bradley Lucier wrote:
> > Any advice on how to proceed? I'd be willing to write and test the few
> > lines of code myself if I knew where to put them.
>
> The best thing, rather than warning, would be to define this
> conversion as a GCC extension and implement it consistently
> everywhere. Then we wouldn't need a warning, and there'd be
> one fewer trap.
I disagree. That would slow down most of uses that use it when they know
the floating point value must be non-negative, for the benefit
of the few that invoke implementation defined behavior.
Just use -fsanitize=float-cast-overflow and you will be notified at runtime
when you run into this.
E.g.
(/lib64/libubsan.so.0+0x9202): runtime error: value -2 is outside the range of representable values of type 'unsigned int'
Jakub