The source files in this attachment demonstrate an apparent bug in g++'s -O2
optimizer.

To reproduce the bug, do the following:

1) make
2) ./testmessage

If the bug is reproduced, you should see output like this:

[EMAIL PROTECTED]:~/bug> ./testmessage
COMPILER BUG!  _iterList is 0xbfc371e0, should be NULL at this point!

If the bug is not reproduced, the output will be this:

[EMAIL PROTECTED]:~/bug> ./testmessage
Bug not reproduced, hmm.

Note that the bug only occurs if I use the -O2 compiler setting... -O0, -O1,
and -O3 compile and
run with no noticeable problems.

I've reproduced this bug in the following environment:

Ubuntu Linux 8.04, with the stock g++ 4.3.1 installed, on a MacPro
(octocore-Xeon) (under VMWare Fusion).
[EMAIL PROTECTED]:~/gcc_optimizer_bug$ g++ --version
g++ (Ubuntu 20080404-0ubuntu1) 4.3.1 20080404 (prerelease) [gcc-4_3-branch
revision 133917]
Copyright (C) 2008 Free Software Foundation, Inc.

The bug does not occur under MacOS/X 10.5.5 with g++ 4.0.1.  A similar bug
occurs under SUSE 10.0
with gcc 4.3.2 installed, but the entry conditions are different such that a
different unit test
(gcc_optimizer_bug.zip) must be run instead, in order to trigger it there.


-----------------------
Code details:

I've simplified the code in this unit test as much as I can; I would have
removed more code since
a lot of it never gets executed by the unit test and therefore shouldn't be
relevant; however, removing
any more code makes the symptom go away(!)

The background for the code is this:  I have a templated Hashtable class, which
can be iterated over using the associated HashtableIterator class.  When a
HashtableIterator is created, it adds itself to a linked list of "registered
iterators" in its Hashtable (see Hashtable::RegisterIterator()), so that if the
Hashtable is modified during the iteration, the associated HashtableIterators
can be updated appropriately.  When the HashtableIterator is destroyed, its
destructor calls Hashtable::UnregisterIterator() so that the iterator will no
longer appear in the linked list.

My code also has a Message class, which is implemented using a Hashtable
object, and there is a an associated MessageFieldNameIterator class that is
implemented using a HashtableIterator.

The problem is that when -O2 optimization is enabled, the
MessageFieldNameIterator's
HashtableIterator object (_iter) doesn't unregister itself from the Hashtable's
_iterList correctly, and the
Hashtable's _iterList pointer is left pointing to an invalid memory location,
causing the program to crash shortly thereafter.  The invalid pointer seems to
point to the stack just after the MessageFieldNameIterator object, for what
that's worth.

This code has worked fine for years, at least it did until I upgraded to g++
4.x (where x>=1).

Sincerely,

Jeremy Friesner
[EMAIL PROTECTED]
626 487 3194


-- 
           Summary: g++ 4.3.1 generates incorrect output when using -O2
                    (other optimization levels work okay)
           Product: gcc
           Version: 4.3.1
            Status: UNCONFIRMED
          Severity: major
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: jaf at meyersound dot com


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=37830

Reply via email to