Bug 28986 - Failure to diagnose overflow in constant expression
Summary: Failure to diagnose overflow in constant expression
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.2.0
: P3 normal
Target Milestone: 4.3.0
Assignee: Manuel López-Ibáñez
URL:
Keywords: accepts-invalid
Depends on:
Blocks: 29843
  Show dependency treegraph
 
Reported: 2006-09-08 18:39 UTC by Jorn Wolfgang Rennecke
Modified: 2007-01-10 18:56 UTC (History)
4 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail: 2.95 3.2.3 3.3.3 3.4.0 4.0.0 4.1.0 4.2.0
Last reconfirmed: 2006-12-09 21:51:31


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Jorn Wolfgang Rennecke 2006-09-08 18:39:07 UTC
g++ doesn't diagnose the overflow (clause 5 paragraph 5) in the following
constant expression:

#include <limits.h>

long l = LONG_MAX+1;
Comment 1 Andrew Pinski 2006-09-09 04:14:01 UTC
Confirmed, not a regression and only happens with the C++ front-end.
Comment 2 Manuel López-Ibáñez 2006-11-25 14:06:59 UTC
As far as I can see, the C++ front-end fails to call overflow_warning (c-common.c) from build_binary_op (cp/typeck.c) in the same way as the C front-end does in parser_build_binary_op(c-typeck.c).
Comment 3 Manuel López-Ibáñez 2006-11-28 22:35:17 UTC
Roger,

The patch below fixes this bug. But it also introduces bug 19978 (multiple warnings) in the C++ front-end. Yet, all regression testcases pass. They pass because C++ overflow testcases are very limited and because the testsuite cannot detect that the same warning has been emitted two times at the same line. (Anyway to workaround the latter? How can we detect in the testcase below that there are two "integer overflow in expression" and put a XFAIL to one of them?) 

What do you think of this? 


Index: gcc/testsuite/g++.dg/conversion/nullptr1.C
===================================================================
--- gcc/testsuite/g++.dg/conversion/nullptr1.C  (revision 119259)
+++ gcc/testsuite/g++.dg/conversion/nullptr1.C  (working copy)
@@ -6,5 +6,5 @@
 void *p = 0;

 void *q = 0 * (INT_MAX + 1);  // { dg-error "invalid conversion" }
+// { dg-warning "integer overflow in expression" "" { target *-*-* } 8 }

-
Index: gcc/cp/typeck.c
===================================================================
--- gcc/cp/typeck.c     (revision 119259)
+++ gcc/cp/typeck.c     (working copy)
@@ -3719,6 +3719,9 @@
   result = fold_if_not_in_template (result);
   if (final_type != 0)
     result = cp_convert (final_type, result);
+
+  overflow_warning (result);
+
   return result;
 }
 ^L
Comment 4 Manuel López-Ibáñez 2006-12-01 23:36:32 UTC
I am working in a patch but don't expect it too soon. Yet, I am quite advanced, that is why I am accepting it. If this is not the proper way to do it, please let me know.
Comment 5 Manuel López-Ibáñez 2006-12-09 21:51:31 UTC
Why is this "accepts-invalid"? Shouldn't it be "diagnostic" instead? I am trying to understand what is the expected output here: a warning or a pedantic error? 
Comment 6 Andrew Stubbs 2007-01-02 14:04:12 UTC
(In reply to comment #5)
> Why is this "accepts-invalid"? Shouldn't it be "diagnostic" instead? I am
> trying to understand what is the expected output here: a warning or a pedantic
> error? 

Clause 5 paragraph 5 says it is "ill-formed". Therefore accepts-invalid sounds right to me.

In pedantic mode this should definitely be an error in my opinion. What it should do otherwise is not clear to me. It seems sensible to accept it with -fpermissive.
Comment 7 Gabriel Dos Reis 2007-01-02 23:33:10 UTC
Subject: Re:  Failure to diagnose overflow in constant expression

"andrew dot stubbs at st dot com" <gcc-bugzilla@gcc.gnu.org> writes:

| ------- Comment #6 from andrew dot stubbs at st dot com  2007-01-02 14:04 -------
| (In reply to comment #5)
| > Why is this "accepts-invalid"? Shouldn't it be "diagnostic" instead? I am
| > trying to understand what is the expected output here: a warning or a pedantic
| > error? 
| 
| Clause 5 paragraph 5 says it is "ill-formed". Therefore accepts-invalid sounds
| right to me.

In C++, "ill-formed" implies "diagnostic is required" unless
explicitly noted otherwise. 

-- Gaby
Comment 8 patchapp@dberlin.org 2007-01-06 17:50:15 UTC
Subject: Bug number PR c++/28986

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/msg00473.html
Comment 9 Manuel López-Ibáñez 2007-01-07 23:40:06 UTC
Subject: Bug 28986

Author: manu
Date: Sun Jan  7 23:39:55 2007
New Revision: 120558

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

	PR c++/28986
cp/
	* typeck.c (build_binary_op): Call overflow_warning if
	TREE_OVERFLOW_P is true for the result and not for any of the
	operands.

testsuite/
	* g++.dg/conversion/nullptr1.C: Added overflow warning.
	* g++.dg/warn/overflow-warn-1.C: New.
	* g++.dg/warn/overflow-warn-3.C: New.
	* g++.dg/warn/overflow-warn-4.C: New.
	* g++.dg/warn/overflow-warn-5.C: New.
	* g++.dg/warn/overflow-warn-6.C: New.
	* g++.dg/warn/Woverflow-1.C: New.
	* g++.dg/warn/Woverflow-2.C: New.
	* g++.dg/warn/Woverflow-3.C: New.
	* g++.dg/warn/multiple-overflow-warn-2.C: New.

Added:
    trunk/gcc/testsuite/g++.dg/warn/Woverflow-1.C
    trunk/gcc/testsuite/g++.dg/warn/Woverflow-2.C
    trunk/gcc/testsuite/g++.dg/warn/Woverflow-3.C
    trunk/gcc/testsuite/g++.dg/warn/multiple-overflow-warn-2.C
    trunk/gcc/testsuite/g++.dg/warn/overflow-warn-1.C
    trunk/gcc/testsuite/g++.dg/warn/overflow-warn-3.C
    trunk/gcc/testsuite/g++.dg/warn/overflow-warn-4.C
    trunk/gcc/testsuite/g++.dg/warn/overflow-warn-5.C
    trunk/gcc/testsuite/g++.dg/warn/overflow-warn-6.C
Modified:
    trunk/gcc/cp/ChangeLog
    trunk/gcc/cp/typeck.c
    trunk/gcc/testsuite/ChangeLog
    trunk/gcc/testsuite/g++.dg/conversion/nullptr1.C

Comment 10 Manuel López-Ibáñez 2007-01-09 16:03:19 UTC
Fixed in GCC 4.3