GCC Bugzilla – Bug 28986
Failure to diagnose overflow in constant expression
Last modified: 2007-01-10 18:56:40 UTC
g++ doesn't diagnose the overflow (clause 5 paragraph 5) in the following constant expression: #include <limits.h> long l = LONG_MAX+1;
Confirmed, not a regression and only happens with the C++ front-end.
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).
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
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.
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?
(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.
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
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
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
Fixed in GCC 4.3