public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/63203] New: Self-initialization of reference not diagnosed if it occurs within a loop
@ 2014-09-08  5:23 jzwinck at gmail dot com
  2014-09-08  9:17 ` [Bug c++/63203] " redi at gcc dot gnu.org
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: jzwinck at gmail dot com @ 2014-09-08  5:23 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=63203

            Bug ID: 63203
           Summary: Self-initialization of reference not diagnosed if it
                    occurs within a loop
           Product: gcc
           Version: 4.9.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: jzwinck at gmail dot com

The following is nonsensical yet compiles cleanly with "g++ -Wall -Wextra
-Werror -Winit-self" (I tested GCC 4.7.2 and 4.9.0):

#include <iostream>
#include <string>

int main()
{
  for (int ii = 0; ii < 1; ++ii)
  {
    const std::string& str = str; // !!
    std::cout << str << std::endl;
  }
}

The line marked !! results in undefined behavior, yet is not diagnosed by GCC.
However, commenting out the for line makes GCC complain:

    error: ‘str’ is used uninitialized in this function [-Werror=uninitialized]

If you change std::string to int and turn on optimization, GCC will diagnose
the error even with the loop.

Ref:
http://stackoverflow.com/questions/25717740/why-is-gcc-tricked-into-allowing-undefined-behavior-simply-by-putting-it-in-a-lo
>From gcc-bugs-return-461397-listarch-gcc-bugs=gcc.gnu.org@gcc.gnu.org Mon Sep 08 07:10:10 2014
Return-Path: <gcc-bugs-return-461397-listarch-gcc-bugs=gcc.gnu.org@gcc.gnu.org>
Delivered-To: listarch-gcc-bugs@gcc.gnu.org
Received: (qmail 11378 invoked by alias); 8 Sep 2014 07:10:09 -0000
Mailing-List: contact gcc-bugs-help@gcc.gnu.org; run by ezmlm
Precedence: bulk
List-Id: <gcc-bugs.gcc.gnu.org>
List-Archive: <http://gcc.gnu.org/ml/gcc-bugs/>
List-Post: <mailto:gcc-bugs@gcc.gnu.org>
List-Help: <mailto:gcc-bugs-help@gcc.gnu.org>
Sender: gcc-bugs-owner@gcc.gnu.org
Delivered-To: mailing list gcc-bugs@gcc.gnu.org
Received: (qmail 11348 invoked by uid 48); 8 Sep 2014 07:10:04 -0000
From: "ville.voutilainen at gmail dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c++/63201] Full specialization of a member variable template of a class template does not work
Date: Mon, 08 Sep 2014 07:10:00 -0000
X-Bugzilla-Reason: CC
X-Bugzilla-Type: changed
X-Bugzilla-Watch-Reason: None
X-Bugzilla-Product: gcc
X-Bugzilla-Component: c++
X-Bugzilla-Version: 5.0
X-Bugzilla-Keywords: rejects-valid
X-Bugzilla-Severity: normal
X-Bugzilla-Who: ville.voutilainen at gmail dot com
X-Bugzilla-Status: NEW
X-Bugzilla-Priority: P3
X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org
X-Bugzilla-Target-Milestone: ---
X-Bugzilla-Flags:
X-Bugzilla-Changed-Fields: bug_status cf_reconfirmed_on everconfirmed
Message-ID: <bug-63201-4-siFVxPH2rK@http.gcc.gnu.org/bugzilla/>
In-Reply-To: <bug-63201-4@http.gcc.gnu.org/bugzilla/>
References: <bug-63201-4@http.gcc.gnu.org/bugzilla/>
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: 7bit
X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/
Auto-Submitted: auto-generated
MIME-Version: 1.0
X-SW-Source: 2014-09/txt/msg01231.txt.bz2
Content-length: 414

https://gcc.gnu.org/bugzilla/show_bug.cgi?idc201

Ville Voutilainen <ville.voutilainen at gmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2014-09-08
     Ever confirmed|0                           |1


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug c++/63203] Self-initialization of reference not diagnosed if it occurs within a loop
  2014-09-08  5:23 [Bug c++/63203] New: Self-initialization of reference not diagnosed if it occurs within a loop jzwinck at gmail dot com
@ 2014-09-08  9:17 ` redi at gcc dot gnu.org
  2014-09-08 10:16 ` redi at gcc dot gnu.org
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: redi at gcc dot gnu.org @ 2014-09-08  9:17 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=63203

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |diagnostic
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2014-09-08
     Ever confirmed|0                           |1

--- Comment #1 from Jonathan Wakely <redi at gcc dot gnu.org> ---
IMHO we should have a specific check for self-initialized references, instead
of relying on the compiler to identify the use of an uninitialized variable in
later code.

Doing self-init for references to silence uninitialized warnings makes no
sense, because you can't re-bind the reference later, so it will always stay
uselessly bound to itself.


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug c++/63203] Self-initialization of reference not diagnosed if it occurs within a loop
  2014-09-08  5:23 [Bug c++/63203] New: Self-initialization of reference not diagnosed if it occurs within a loop jzwinck at gmail dot com
  2014-09-08  9:17 ` [Bug c++/63203] " redi at gcc dot gnu.org
@ 2014-09-08 10:16 ` redi at gcc dot gnu.org
  2014-11-23 20:16 ` paolo.carlini at oracle dot com
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: redi at gcc dot gnu.org @ 2014-09-08 10:16 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=63203

--- Comment #2 from Jonathan Wakely <redi at gcc dot gnu.org> ---
I tried something like this:

--- a/gcc/cp/decl.c
+++ b/gcc/cp/decl.c
@@ -6101,6 +6101,12 @@ initialize_local_var (tree decl, tree init)
             -Wno-init-self works (c++/34772).  */
          gcc_assert (TREE_OPERAND (init, 0) == decl);
          DECL_INITIAL (decl) = TREE_OPERAND (init, 1);
+
+          if (warn_init_self && TREE_CODE (type) == REFERENCE_TYPE
+              && TREE_OPERAND (init, 1) == decl)
+            warning_at (DECL_SOURCE_LOCATION (decl),
+                        OPT_Winit_self,
+                        "Reference %qD is initialized with itself", decl);
        }
       else
        {

but the TREE_OPERAND (init, 1) == decl condition is false and I'm not sure how
to fix it.


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug c++/63203] Self-initialization of reference not diagnosed if it occurs within a loop
  2014-09-08  5:23 [Bug c++/63203] New: Self-initialization of reference not diagnosed if it occurs within a loop jzwinck at gmail dot com
  2014-09-08  9:17 ` [Bug c++/63203] " redi at gcc dot gnu.org
  2014-09-08 10:16 ` redi at gcc dot gnu.org
@ 2014-11-23 20:16 ` paolo.carlini at oracle dot com
  2014-11-24 13:35 ` paolo at gcc dot gnu.org
  2014-11-24 13:57 ` paolo.carlini at oracle dot com
  4 siblings, 0 replies; 6+ messages in thread
From: paolo.carlini at oracle dot com @ 2014-11-23 20:16 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=63203

Paolo Carlini <paolo.carlini at oracle dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |ASSIGNED
           Assignee|unassigned at gcc dot gnu.org      |paolo.carlini at oracle dot com

--- Comment #3 from Paolo Carlini <paolo.carlini at oracle dot com> ---
Mine.


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug c++/63203] Self-initialization of reference not diagnosed if it occurs within a loop
  2014-09-08  5:23 [Bug c++/63203] New: Self-initialization of reference not diagnosed if it occurs within a loop jzwinck at gmail dot com
                   ` (2 preceding siblings ...)
  2014-11-23 20:16 ` paolo.carlini at oracle dot com
@ 2014-11-24 13:35 ` paolo at gcc dot gnu.org
  2014-11-24 13:57 ` paolo.carlini at oracle dot com
  4 siblings, 0 replies; 6+ messages in thread
From: paolo at gcc dot gnu.org @ 2014-11-24 13:35 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=63203

--- Comment #4 from paolo at gcc dot gnu.org <paolo at gcc dot gnu.org> ---
Author: paolo
Date: Mon Nov 24 13:35:08 2014
New Revision: 218017

URL: https://gcc.gnu.org/viewcvs?rev=218017&root=gcc&view=rev
Log:
/cp
2014-11-24  Jonathan Wakely  <jwakely@redhat.com>
        Paolo Carlini  <paolo.carlini@oracle.com>

    PR c++/63203
    * decl.c (initialize_local_var): Add -Winit-self warning for
    references initialized with themselves.

/testsuite
2014-11-24  Jonathan Wakely  <jwakely@redhat.com>
        Paolo Carlini  <paolo.carlini@oracle.com>

    PR c++/63203
    * g++.dg/warn/Winit-self-2.C: New.

Added:
    trunk/gcc/testsuite/g++.dg/warn/Winit-self-2.C
Modified:
    trunk/gcc/cp/ChangeLog
    trunk/gcc/cp/decl.c
    trunk/gcc/testsuite/ChangeLog


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug c++/63203] Self-initialization of reference not diagnosed if it occurs within a loop
  2014-09-08  5:23 [Bug c++/63203] New: Self-initialization of reference not diagnosed if it occurs within a loop jzwinck at gmail dot com
                   ` (3 preceding siblings ...)
  2014-11-24 13:35 ` paolo at gcc dot gnu.org
@ 2014-11-24 13:57 ` paolo.carlini at oracle dot com
  4 siblings, 0 replies; 6+ messages in thread
From: paolo.carlini at oracle dot com @ 2014-11-24 13:57 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=63203

Paolo Carlini <paolo.carlini at oracle dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|ASSIGNED                    |RESOLVED
         Resolution|---                         |FIXED
   Target Milestone|---                         |5.0

--- Comment #5 from Paolo Carlini <paolo.carlini at oracle dot com> ---
Fixed.


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2014-11-24 13:57 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-09-08  5:23 [Bug c++/63203] New: Self-initialization of reference not diagnosed if it occurs within a loop jzwinck at gmail dot com
2014-09-08  9:17 ` [Bug c++/63203] " redi at gcc dot gnu.org
2014-09-08 10:16 ` redi at gcc dot gnu.org
2014-11-23 20:16 ` paolo.carlini at oracle dot com
2014-11-24 13:35 ` paolo at gcc dot gnu.org
2014-11-24 13:57 ` paolo.carlini at oracle dot com

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).