public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug libstdc++/26123]  New: Segmentation fault in constructor of std::ostream::sentry::sentry
@ 2006-02-06 12:44 broeni at osb-systems dot com
  2006-02-06 12:52 ` [Bug libstdc++/26123] " pinskia at gcc dot gnu dot org
                   ` (11 more replies)
  0 siblings, 12 replies; 13+ messages in thread
From: broeni at osb-systems dot com @ 2006-02-06 12:44 UTC (permalink / raw)
  To: gcc-bugs

When compiled with:
  g++-4.0 -g -Wall   -c -o try.o try.cc
  g++-4.0 -g -Wall   -c -o foo.o foo.cc
  g++-4.0 -g -Wall -o try try.o foo.o
the program below creates a segmentation fault.

UNIX> gdb ./try
(gdb) r
Starting program: /data/home/osb/temp/clcpp/try 

Program received signal SIGSEGV, Segmentation fault.
0xb7f83de5 in std::ostream::sentry::sentry () from /usr/lib/libstdc++.so.6

try.h
=====
#ifndef TRY_H
#define TRY_H

int mkCint();
const int cInt = mkCint();

#endif

try.cc
======
#include "try.h"
#include <iostream>

int main()
{
    std::cout << cInt << std::endl;
}

int mkCint()
{
    std::cout << "mkCint()" << std::endl;
    return 2;
}

foo.cc
======
#include "try.h"

Notes
If stdout instead of std::cout is used OR
if compiled with g++-3.3 the program runs as expected:
UNIX> ./try
mkCint()
mkCint()
2
UNIX>


-- 
           Summary: Segmentation fault in constructor of
                    std::ostream::sentry::sentry
           Product: gcc
           Version: 4.0.3
            Status: UNCONFIRMED
          Severity: major
          Priority: P3
         Component: libstdc++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: broeni at osb-systems dot com
 GCC build triplet: i486-pc-linux-gnu
  GCC host triplet: i486-pc-linux-gnu
GCC target triplet: i486-pc-linux-gnu


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


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

* [Bug libstdc++/26123] Segmentation fault in constructor of std::ostream::sentry::sentry
  2006-02-06 12:44 [Bug libstdc++/26123] New: Segmentation fault in constructor of std::ostream::sentry::sentry broeni at osb-systems dot com
@ 2006-02-06 12:52 ` pinskia at gcc dot gnu dot org
  2006-02-06 12:54 ` pinskia at gcc dot gnu dot org
                   ` (10 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2006-02-06 12:52 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #1 from pinskia at gcc dot gnu dot org  2006-02-06 12:51 -------
*** Bug 26124 has been marked as a duplicate of this bug. ***


-- 


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


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

* [Bug libstdc++/26123] Segmentation fault in constructor of std::ostream::sentry::sentry
  2006-02-06 12:44 [Bug libstdc++/26123] New: Segmentation fault in constructor of std::ostream::sentry::sentry broeni at osb-systems dot com
  2006-02-06 12:52 ` [Bug libstdc++/26123] " pinskia at gcc dot gnu dot org
@ 2006-02-06 12:54 ` pinskia at gcc dot gnu dot org
  2006-02-06 12:56 ` [Bug libstdc++/26123] [3.4/4.0/4.1/4.2 Regression] " pinskia at gcc dot gnu dot org
                   ` (9 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2006-02-06 12:54 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #2 from pinskia at gcc dot gnu dot org  2006-02-06 12:54 -------
foo.cc has nothing to do with the problem here.  It all has to do with the
order of the execution of the initializers.

The following code is enough to reproduce the issue:
int mkCint();
const int cInt = mkCint();
#include <iostream>
int main()
{
    std::cout << cInt << std::endl;
}
int mkCint()
{
    std::cout << "mkCint()" << std::endl;
    return 2;
}


-- 


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


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

* [Bug libstdc++/26123] [3.4/4.0/4.1/4.2 Regression] Segmentation fault in constructor of std::ostream::sentry::sentry
  2006-02-06 12:44 [Bug libstdc++/26123] New: Segmentation fault in constructor of std::ostream::sentry::sentry broeni at osb-systems dot com
  2006-02-06 12:52 ` [Bug libstdc++/26123] " pinskia at gcc dot gnu dot org
  2006-02-06 12:54 ` pinskia at gcc dot gnu dot org
@ 2006-02-06 12:56 ` pinskia at gcc dot gnu dot org
  2006-02-06 13:10 ` pcarlini at suse dot de
                   ` (8 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2006-02-06 12:56 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #3 from pinskia at gcc dot gnu dot org  2006-02-06 12:56 -------
Confirmed, though I don't know if this is just undefined code or not.  Also the
global initializer between 3.3 and 3.4 (and 4.0/4.1/4.2) did not change so I
don't think this is a front-end bug.


-- 

pinskia at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
     Ever Confirmed|0                           |1
      Known to fail|                            |3.4.0 4.0.0 4.1.0 4.2.0
      Known to work|                            |3.3.3
   Last reconfirmed|0000-00-00 00:00:00         |2006-02-06 12:56:45
               date|                            |
            Summary|Segmentation fault in       |[3.4/4.0/4.1/4.2 Regression]
                   |constructor of              |Segmentation fault in
                   |std::ostream::sentry::sentry|constructor of
                   |                            |std::ostream::sentry::sentry
   Target Milestone|---                         |4.0.3


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


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

* [Bug libstdc++/26123] [3.4/4.0/4.1/4.2 Regression] Segmentation fault in constructor of std::ostream::sentry::sentry
  2006-02-06 12:44 [Bug libstdc++/26123] New: Segmentation fault in constructor of std::ostream::sentry::sentry broeni at osb-systems dot com
                   ` (2 preceding siblings ...)
  2006-02-06 12:56 ` [Bug libstdc++/26123] [3.4/4.0/4.1/4.2 Regression] " pinskia at gcc dot gnu dot org
@ 2006-02-06 13:10 ` pcarlini at suse dot de
  2006-02-06 13:13 ` [Bug libstdc++/26123] [3.4/4.0 " pinskia at gcc dot gnu dot org
                   ` (7 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: pcarlini at suse dot de @ 2006-02-06 13:10 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #4 from pcarlini at suse dot de  2006-02-06 13:10 -------
For some reason, on x86-linux, I can reproduce only with 3.4 and 4.0, not with
mainline and 4.1...


-- 


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


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

* [Bug libstdc++/26123] [3.4/4.0 Regression] Segmentation fault in constructor of std::ostream::sentry::sentry
  2006-02-06 12:44 [Bug libstdc++/26123] New: Segmentation fault in constructor of std::ostream::sentry::sentry broeni at osb-systems dot com
                   ` (3 preceding siblings ...)
  2006-02-06 13:10 ` pcarlini at suse dot de
@ 2006-02-06 13:13 ` pinskia at gcc dot gnu dot org
  2006-02-06 13:16 ` pcarlini at suse dot de
                   ` (6 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2006-02-06 13:13 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #5 from pinskia at gcc dot gnu dot org  2006-02-06 13:13 -------
Oh, I was using 4.0.2's shared library still when testing, woops.


-- 

pinskia at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
  GCC build triplet|i486-pc-linux-gnu           |
   GCC host triplet|i486-pc-linux-gnu           |
 GCC target triplet|i486-pc-linux-gnu           |
      Known to fail|3.4.0 4.0.0 4.1.0 4.2.0     |3.4.0 4.0.0 4.0.2
      Known to work|3.3.3                       |3.3.3 4.1.0 4.2.0
            Summary|[3.4/4.0/4.1/4.2 Regression]|[3.4/4.0 Regression]
                   |Segmentation fault in       |Segmentation fault in
                   |constructor of              |constructor of
                   |std::ostream::sentry::sentry|std::ostream::sentry::sentry


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


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

* [Bug libstdc++/26123] [3.4/4.0 Regression] Segmentation fault in constructor of std::ostream::sentry::sentry
  2006-02-06 12:44 [Bug libstdc++/26123] New: Segmentation fault in constructor of std::ostream::sentry::sentry broeni at osb-systems dot com
                   ` (4 preceding siblings ...)
  2006-02-06 13:13 ` [Bug libstdc++/26123] [3.4/4.0 " pinskia at gcc dot gnu dot org
@ 2006-02-06 13:16 ` pcarlini at suse dot de
  2006-02-06 15:07 ` rguenth at gcc dot gnu dot org
                   ` (5 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: pcarlini at suse dot de @ 2006-02-06 13:16 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #6 from pcarlini at suse dot de  2006-02-06 13:16 -------
Ok. mainline is also confirmed fine on ia64-linux.


-- 


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


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

* [Bug libstdc++/26123] [3.4/4.0 Regression] Segmentation fault in constructor of std::ostream::sentry::sentry
  2006-02-06 12:44 [Bug libstdc++/26123] New: Segmentation fault in constructor of std::ostream::sentry::sentry broeni at osb-systems dot com
                   ` (5 preceding siblings ...)
  2006-02-06 13:16 ` pcarlini at suse dot de
@ 2006-02-06 15:07 ` rguenth at gcc dot gnu dot org
  2006-02-06 19:28 ` broeni at osb-systems dot com
                   ` (4 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: rguenth at gcc dot gnu dot org @ 2006-02-06 15:07 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #7 from rguenth at gcc dot gnu dot org  2006-02-06 15:07 -------
I think this is undefined.  Reduced testcase:

extern "C" void *malloc(__SIZE_TYPE__ n);
int mkCint();
const int cInt = mkCint();
struct Ptr {
  Ptr() { ptr = (int *)malloc(sizeof(int)); }
  int *ptr;
};
Ptr p;
int main()
{
  *p.ptr = mkCint();
}
int mkCint()
{
  return *p.ptr;
}

how should we know to initialize std::cout first?


-- 


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


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

* [Bug libstdc++/26123] [3.4/4.0 Regression] Segmentation fault in constructor of std::ostream::sentry::sentry
  2006-02-06 12:44 [Bug libstdc++/26123] New: Segmentation fault in constructor of std::ostream::sentry::sentry broeni at osb-systems dot com
                   ` (6 preceding siblings ...)
  2006-02-06 15:07 ` rguenth at gcc dot gnu dot org
@ 2006-02-06 19:28 ` broeni at osb-systems dot com
  2006-02-07 12:52 ` broeni at osb-systems dot com
                   ` (3 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: broeni at osb-systems dot com @ 2006-02-06 19:28 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #8 from broeni at osb-systems dot com  2006-02-06 19:28 -------
Posted code as from comment #2 to c.l.c++.m to verify if the initalization of
std::cout is well defined (comment #3): "Initialization order of std::cout".
Stephan


-- 


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


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

* [Bug libstdc++/26123] [3.4/4.0 Regression] Segmentation fault in constructor of std::ostream::sentry::sentry
  2006-02-06 12:44 [Bug libstdc++/26123] New: Segmentation fault in constructor of std::ostream::sentry::sentry broeni at osb-systems dot com
                   ` (7 preceding siblings ...)
  2006-02-06 19:28 ` broeni at osb-systems dot com
@ 2006-02-07 12:52 ` broeni at osb-systems dot com
  2006-02-07 13:19 ` broeni at osb-systems dot com
                   ` (2 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: broeni at osb-systems dot com @ 2006-02-07 12:52 UTC (permalink / raw)
  To: gcc-bugs

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 2047 bytes --]



------- Comment #9 from broeni at osb-systems dot com  2006-02-07 12:52 -------
Obviously the behavior of the test program is undefined.
The following is the reply from Alberto Ganesh Barbati in c.l.c++.m

Apart from the fact that you need to include <ostream> in order to be
able to write on std::cout, yes: it's well defined. However, its
behaviour is unspecified. See below.

> In particular: Does the standard guarantee that std::cout is
> initialized before the constant `cInt'?

No, the standard does not guarantee that (although it non-normatively
encourages implementations to do it). According to §27.3/2: "The objects
[std::cin, std::cout, etc.] are constructed, and the associations are
established at some time prior to or during first time an object of
class ios_base::Init is constructed, and in any case before the body of
main begins execution."

So in order to ensure that the program behave as expected, you need to
construct a variable of type ios_base::Init before using std::cout. You
can either use a global variable like this:

int mkCint();
std::ios_base::Init gInitIostreams;
const int cInt = mkCint();

or use a local variable in function mkCint():

int mkCint()
{
     static std::ios_base::Init initIostreams;
     std::cout << "mkCint()" << std::endl;
     return 2;
}

Comment:
While the use of a global variable works fine for the reduced test #2 the
originally posted code (#1) only works with a local std::ios_base::Init in
mkCint() because of the undefined initialization order of globals.

The reply of James Kanze confirms the undefined behavior:
> Is the following program well defined?
> In particular: Does the standard guarantee that std::cout is
> initialized before the constant `cInt'?

No.

With the classical implementation of <iostream.h>, it was
guaranteed IF your code included <iostream.h> before defining
cInt.  Many (most?, all?) current implementations of <iostream>
also provide this guarantee, but the standard doesn't require
it.


-- 


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


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

* [Bug libstdc++/26123] [3.4/4.0 Regression] Segmentation fault in constructor of std::ostream::sentry::sentry
  2006-02-06 12:44 [Bug libstdc++/26123] New: Segmentation fault in constructor of std::ostream::sentry::sentry broeni at osb-systems dot com
                   ` (8 preceding siblings ...)
  2006-02-07 12:52 ` broeni at osb-systems dot com
@ 2006-02-07 13:19 ` broeni at osb-systems dot com
  2006-02-07 13:59 ` pcarlini at suse dot de
  2006-04-22 19:54 ` pcarlini at suse dot de
  11 siblings, 0 replies; 13+ messages in thread
From: broeni at osb-systems dot com @ 2006-02-07 13:19 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #10 from broeni at osb-systems dot com  2006-02-07 13:19 -------
See comment #9 for reason.
Stephan


-- 

broeni at osb-systems dot com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |INVALID


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


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

* [Bug libstdc++/26123] [3.4/4.0 Regression] Segmentation fault in constructor of std::ostream::sentry::sentry
  2006-02-06 12:44 [Bug libstdc++/26123] New: Segmentation fault in constructor of std::ostream::sentry::sentry broeni at osb-systems dot com
                   ` (9 preceding siblings ...)
  2006-02-07 13:19 ` broeni at osb-systems dot com
@ 2006-02-07 13:59 ` pcarlini at suse dot de
  2006-04-22 19:54 ` pcarlini at suse dot de
  11 siblings, 0 replies; 13+ messages in thread
From: pcarlini at suse dot de @ 2006-02-07 13:59 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #11 from pcarlini at suse dot de  2006-02-07 13:59 -------
Thanks a lot for the torough clarification!


-- 


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


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

* [Bug libstdc++/26123] [3.4/4.0 Regression] Segmentation fault in constructor of std::ostream::sentry::sentry
  2006-02-06 12:44 [Bug libstdc++/26123] New: Segmentation fault in constructor of std::ostream::sentry::sentry broeni at osb-systems dot com
                   ` (10 preceding siblings ...)
  2006-02-07 13:59 ` pcarlini at suse dot de
@ 2006-04-22 19:54 ` pcarlini at suse dot de
  11 siblings, 0 replies; 13+ messages in thread
From: pcarlini at suse dot de @ 2006-04-22 19:54 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #12 from pcarlini at suse dot de  2006-04-22 19:54 -------
*** Bug 27258 has been marked as a duplicate of this bug. ***


-- 

pcarlini at suse dot de changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |tim at yeung dot com


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


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

end of thread, other threads:[~2006-04-22 19:54 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-02-06 12:44 [Bug libstdc++/26123] New: Segmentation fault in constructor of std::ostream::sentry::sentry broeni at osb-systems dot com
2006-02-06 12:52 ` [Bug libstdc++/26123] " pinskia at gcc dot gnu dot org
2006-02-06 12:54 ` pinskia at gcc dot gnu dot org
2006-02-06 12:56 ` [Bug libstdc++/26123] [3.4/4.0/4.1/4.2 Regression] " pinskia at gcc dot gnu dot org
2006-02-06 13:10 ` pcarlini at suse dot de
2006-02-06 13:13 ` [Bug libstdc++/26123] [3.4/4.0 " pinskia at gcc dot gnu dot org
2006-02-06 13:16 ` pcarlini at suse dot de
2006-02-06 15:07 ` rguenth at gcc dot gnu dot org
2006-02-06 19:28 ` broeni at osb-systems dot com
2006-02-07 12:52 ` broeni at osb-systems dot com
2006-02-07 13:19 ` broeni at osb-systems dot com
2006-02-07 13:59 ` pcarlini at suse dot de
2006-04-22 19:54 ` pcarlini at suse dot de

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).