Bug 23572

Summary: No warning for assigning a value to a 'float' variable that overflows with option -Wextra
Product: gcc Reporter: Yao Qi <qiyaoltc>
Component: otherAssignee: Not yet assigned to anyone <unassigned>
Status: RESOLVED FIXED    
Severity: normal CC: danglin, dcb314, gcc-bugs, janis187, manu
Priority: P2 Keywords: diagnostic
Version: 4.1.0   
Target Milestone: 4.3.0   
See Also: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103123
Host: ppc64-redhat-linux Target: ppc64-redhat-linux
Build: ppc64-redhat-linux Known to work:
Known to fail: 2.95.3 3.2.3 3.4.0 4.0.0 4.1.0 4.2.0 Last reconfirmed: 2006-03-05 03:58:10

Description Yao Qi 2005-08-26 07:52:01 UTC
I tried compilers 4.1.0 through mainline and none of them warn about nitializing
a float variable with a value that will overflow, underflow, or lose precision
for -W or -Wextra.

But in GCC info, It is said like this:
in GCC info(GCC 3.3.4).
 `-Wextra'
      (This option used to be called `-W'.  The older name is still
      supported, but the newer name is more descriptive.)  Print extra
      warning messages for these events:
 ........
      * Any of several floating-point events that often indicate
      errors, such as overflow, underflow, loss of precision, etc.


Is this a bug either in GCC or a bug in documentation?
Comment 1 Andrew Pinski 2005-08-26 12:51:01 UTC
Can you show an example?
Comment 2 Yao Qi 2005-08-29 02:57:46 UTC
OK.
GCC version: 3.4.3 20050227 (Red Hat 3.4.3-22.1)
GNU C Library: stable release version 2.3.4
OS : Red Hat Enterprise Linux AS release 4 (Nahant Update 1)

I write a small example named overflow-test.c, here is the source code:

      1 #include<stdio.h>
      2 int main()
      3 {
      4   /* overflow.  */
      5   float f1 = 3.5E+38;
      6   /* underflow.  */
      7   float f2 = 3.3E-46;
      8   /* overflow.  */
      9   double d1 = 1.9E+308;
     10   /* underflow.  */
     11   double d2 = 1.4E-325;
     12   /* overflow, 2**32.  */
     13   int i = 4294967296;
     14
     15   printf("%e,%e,%e,%e\n",f1,f2,d1,d2);
     16   printf("%d\n",i);
     17   return 0;
     18 }

And I compile it like this:
[qiyao@patch]$ gcc -o overflow-test -Wextra -Wall -g overflow-test.c
overflow-test.c: In function `main':
overflow-test.c:13: warning: integer constant is too large for "long" type
overflow-test.c:13: warning: overflow in implicit constant conversion

I run it,
[qiyao@patch]$ ./overflow-test
inf,0.000000e+00,inf,0.000000e+00
0


I hope this example would be helpful.  Any comments are highly aprreciated!
Comment 3 Andrew Pinski 2005-08-29 03:10:00 UTC
I think there are some real interesting issues here.
First with -pedantic we get a warning for line 9 but nothing more.  If we add f to the end of the 
constant on line 5, we then get a warning.  

And yes your example was useful. 

The interesting thing is that we don't get a warning for the following code:
  double d1 = 1.9E+308 + 1.;
which should warn.
Comment 4 Yao Qi 2005-08-29 05:00:23 UTC
I add suffix f to the end of float constants at line 5 and line 7, and add
option -pedantic, the output from gcc is like this:

overflow-test.c:5: warning: floating constant exceeds range of "float"
overflow-test.c:9: warning: floating constant exceeds range of "double"
overflow-test.c:13: warning: integer constant is too large for "long" type
overflow-test.c:13: warning: overflow in implicit constant conversion

It seems that gcc has checked initialization which caused overflow, but do not
check underflow, that is to say:
1 GCC does not check underflow event, at least options -pedantic does not enable
underflow check.
2 If there is some mechanism in GCC internal to check underflow and overflow,
there is a bug in gcc info about the description of -Wextra option.

Could you tell me what I could do to this problem?  Maybe, it is impossible for
me to fix it, but I just want to do something more.  Please give me some
suggestions and I will try to do it.

Thanks.
Comment 5 Manuel López-Ibáñez 2006-11-25 15:24:02 UTC
Why this is marked as "other" ? This is either a problem on the C/C++ front-ends or it is a problem in the middle-end that doesn't handle the overflow/underflow correctly during conversion, isn't it?

Also, this happens also for i686-pc-gnu-linux and I guess for almost any possible host/target, so perhaps Host and Target should say something different.

Comment 6 Manuel López-Ibáñez 2007-01-09 15:09:49 UTC
(In reply to comment #0)
> 
> Is this a bug either in GCC or a bug in documentation?
> 

It is a bug in the documentation. As today in GCC 4.3 there is not a single Wextra warning that has anything to do with floating-point events.
Comment 7 patchapp@dberlin.org 2007-01-21 16:01:07 UTC
Subject: Bug number PR 23572

A patch for this bug has been added to the patch tracker.
The mailing list url for the patch is http://gcc.gnu.org/ml/gcc-patches/2007-01/msg01730.html
Comment 8 Manuel López-Ibáñez 2007-01-22 17:05:08 UTC
*** Bug 20764 has been marked as a duplicate of this bug. ***
Comment 9 patchapp@dberlin.org 2007-03-14 18:15:37 UTC
Subject: Bug number PR 23572

A patch for this bug has been added to the patch tracker.
The mailing list url for the patch is http://gcc.gnu.org/ml/gcc-patches/2007-03/msg00943.html
Comment 10 patchapp@dberlin.org 2007-03-21 21:05:21 UTC
Subject: Bug number PR 23572

A patch for this bug has been added to the patch tracker.
The mailing list url for the patch is http://gcc.gnu.org/ml/gcc-patches/2007-03/msg01437.html
Comment 11 Manuel López-Ibáñez 2007-03-22 23:04:42 UTC
Subject: Bug 23572

Author: manu
Date: Thu Mar 22 23:04:24 2007
New Revision: 123137

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=123137
Log:
2007-03-22  Manuel Lopez-Ibanez  <manu@gcc.gnu.org>

	PR other/23572
	* c-lex.c (interpret_float): On overflow, emit pedantic warning if
	infinities not supported, otherwise emit warning if -Woverflow. On
	underflow, emit warning if -Woverflow.
	* real.c (real_from_string): Return -1 if underflow, +1 if overflow
	and 0 otherwise.
	* real.h (real_from_string): Update declaration
testsuite/
	* gcc.dg/float-range-4.c: New.
	* gcc.dg/float-range-1.c: Update. Test for a warning.
	* gcc.dg/float-range-3.c: New.
	* gcc.dg/float-range-5.c: New.

Added:
    trunk/gcc/testsuite/gcc.dg/float-range-3.c
    trunk/gcc/testsuite/gcc.dg/float-range-4.c
    trunk/gcc/testsuite/gcc.dg/float-range-5.c
Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/c-lex.c
    trunk/gcc/real.c
    trunk/gcc/real.h
    trunk/gcc/testsuite/ChangeLog
    trunk/gcc/testsuite/gcc.dg/float-range-1.c

Comment 12 Manuel López-Ibáñez 2007-03-22 23:12:34 UTC
Fixed for GCC 4.3
Comment 13 Andrew Pinski 2007-04-06 00:58:21 UTC
These testcases fail on spu-elf:
+FAIL: gcc.dg/float-range-1.c  (test for warnings, line 11)
+FAIL: gcc.dg/float-range-1.c (test for excess errors)
+FAIL: gcc.dg/float-range-3.c (test for excess errors)
+FAIL: gcc.dg/float-range-4.c (test for excess errors)
+FAIL: gcc.dg/float-range-5.c  (test for warnings, line 9)
+FAIL: gcc.dg/float-range-5.c  (test for warnings, line 10)
+FAIL: gcc.dg/float-range-5.c (test for excess errors)


FAIL: gcc.dg/float-range-1.c (test for excess errors) Excess errors:
/home/apinski/src/local/gcc/gcc/testsuite/gcc.dg/float-range-1.c:11: error: floating constant exceeds range of 'float'

Excess errors:
/home/apinski/src/local/gcc/gcc/testsuite/gcc.dg/float-range-3.c:10: error: 'FP_INFINITE' undeclared (first use in this function)
/home/apinski/src/local/gcc/gcc/testsuite/gcc.dg/float-range-3.c:10: error: (Each undeclared identifier is reported only once
/home/apinski/src/local/gcc/gcc/testsuite/gcc.dg/float-range-3.c:10: error: for each function it appears in.)


Comment 14 Manuel López-Ibáñez 2007-04-06 12:25:23 UTC
> /home/apinski/src/local/gcc/gcc/testsuite/gcc.dg/float-range-3.c:10: error:
> 'FP_INFINITE' undeclared (first use in this function)

Is FP_INFINITE undeclared in spu-elf?
Comment 15 John David Anglin 2007-04-06 19:58:12 UTC
The same errors occur on hppa*-*-hpux*.  FP_INFINITE isn't defined in
HP-UX 10.  For HP-UX 11, we need "-D_HPUX_SOURCE" to get math.h to define
FP_INFINITE.