public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/38167]  New: Inline accessor to stdin is breaks on -O2 optimization
@ 2008-11-17  2:00 aleksi dot nurmi at helsinki dot fi
  2008-11-17  2:03 ` [Bug c/38167] " aleksi dot nurmi at helsinki dot fi
                   ` (8 more replies)
  0 siblings, 9 replies; 10+ messages in thread
From: aleksi dot nurmi at helsinki dot fi @ 2008-11-17  2:00 UTC (permalink / raw)
  To: gcc-bugs

It appears that the following inline accessor function to stdin doesn't do it's
job if -O2 optimization is turned on, thus letting a null pointer pass and
causing a segfault. This doesn't happen if the function is not declared inline. 

Version: gcc (GCC) 4.2.4 (Ubuntu 4.2.4-1ubuntu3)
Command line: gcc -O2 -o fault fault.c
GNU libc 2.7 is known for its non-constant stdin and is probably required to
actually get a segfault.

Code in readable format:

#include <stdio.h>

struct iostream {
    FILE *f;
};

static struct iostream our_stdin = { 0 };

inline void access_iostream(struct iostream* ios)
{
    if (ios->f == 0)
        if (ios == &our_stdin)
            ios->f = stdin;
}

void read_char(struct iostream* ios)
{
    access_iostream(ios);
    fgetc(ios->f);
}

int main()
{  
    read_char(&our_stdin);

    return 0;
}


-- 
           Summary: Inline accessor to stdin is breaks on -O2 optimization
           Product: gcc
           Version: 4.2.4
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: aleksi dot nurmi at helsinki dot fi
  GCC host triplet: x86_64-pc-linux-gnu


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


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

* [Bug c/38167] Inline accessor to stdin is breaks on -O2 optimization
  2008-11-17  2:00 [Bug c/38167] New: Inline accessor to stdin is breaks on -O2 optimization aleksi dot nurmi at helsinki dot fi
@ 2008-11-17  2:03 ` aleksi dot nurmi at helsinki dot fi
  2008-11-17  8:47 ` jakub at gcc dot gnu dot org
                   ` (7 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: aleksi dot nurmi at helsinki dot fi @ 2008-11-17  2:03 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #1 from aleksi dot nurmi at helsinki dot fi  2008-11-17 02:01 -------
Created an attachment (id=16707)
 --> (http://gcc.gnu.org/bugzilla/attachment.cgi?id=16707&action=view)
preprocessed source


-- 


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


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

* [Bug c/38167] Inline accessor to stdin is breaks on -O2 optimization
  2008-11-17  2:00 [Bug c/38167] New: Inline accessor to stdin is breaks on -O2 optimization aleksi dot nurmi at helsinki dot fi
  2008-11-17  2:03 ` [Bug c/38167] " aleksi dot nurmi at helsinki dot fi
@ 2008-11-17  8:47 ` jakub at gcc dot gnu dot org
  2008-11-17 14:08 ` aleksi dot nurmi at helsinki dot fi
                   ` (6 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: jakub at gcc dot gnu dot org @ 2008-11-17  8:47 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #2 from jakub at gcc dot gnu dot org  2008-11-17 08:46 -------
Works just fine here, with both 4.3 and 4.4, -O2 and -O0.


-- 


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


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

* [Bug c/38167] Inline accessor to stdin is breaks on -O2 optimization
  2008-11-17  2:00 [Bug c/38167] New: Inline accessor to stdin is breaks on -O2 optimization aleksi dot nurmi at helsinki dot fi
  2008-11-17  2:03 ` [Bug c/38167] " aleksi dot nurmi at helsinki dot fi
  2008-11-17  8:47 ` jakub at gcc dot gnu dot org
@ 2008-11-17 14:08 ` aleksi dot nurmi at helsinki dot fi
  2008-11-17 15:12 ` [Bug c/38167] Accessor " aleksi dot nurmi at helsinki dot fi
                   ` (5 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: aleksi dot nurmi at helsinki dot fi @ 2008-11-17 14:08 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #3 from aleksi dot nurmi at helsinki dot fi  2008-11-17 14:06 -------
Works for me on 4.1.3 20080308 (prerelease) (Ubuntu 4.1.2-21ubuntu1) and
(Ubuntu 20081013-0ubuntu2) 4.3.3 20081014 (prerelease), too.

But not in 4.2.4 (Ubuntu 4.2.4-1ubuntu3).


-- 


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


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

* [Bug c/38167] Accessor to stdin is breaks on -O2 optimization
  2008-11-17  2:00 [Bug c/38167] New: Inline accessor to stdin is breaks on -O2 optimization aleksi dot nurmi at helsinki dot fi
                   ` (2 preceding siblings ...)
  2008-11-17 14:08 ` aleksi dot nurmi at helsinki dot fi
@ 2008-11-17 15:12 ` aleksi dot nurmi at helsinki dot fi
  2008-11-17 15:55 ` [Bug c/38167] Accessor to stdin " aleksi dot nurmi at helsinki dot fi
                   ` (4 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: aleksi dot nurmi at helsinki dot fi @ 2008-11-17 15:12 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #4 from aleksi dot nurmi at helsinki dot fi  2008-11-17 15:11 -------
I found an even shorter way to make it segfault, with no inline functions at
all. It's an -O2 problem, then.

#include <stdio.h>

struct iostream {
    FILE *f;
};

static struct iostream our_stdin = { 0 };

void read_char(struct iostream* ios)
{
    if (ios->f == 0)
        if (ios == &our_stdin)
            ios->f = stdin;
    fgetc(ios->f);
}

int main()
{
    read_char(&our_stdin);

    return 0;
}


-- 

aleksi dot nurmi at helsinki dot fi changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|Inline accessor to stdin is |Accessor to stdin is breaks
                   |breaks on -O2 optimization  |on -O2 optimization


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


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

* [Bug c/38167] Accessor to stdin breaks on -O2 optimization
  2008-11-17  2:00 [Bug c/38167] New: Inline accessor to stdin is breaks on -O2 optimization aleksi dot nurmi at helsinki dot fi
                   ` (3 preceding siblings ...)
  2008-11-17 15:12 ` [Bug c/38167] Accessor " aleksi dot nurmi at helsinki dot fi
@ 2008-11-17 15:55 ` aleksi dot nurmi at helsinki dot fi
  2008-11-24 17:48 ` [Bug middle-end/38167] " pinskia at gcc dot gnu dot org
                   ` (3 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: aleksi dot nurmi at helsinki dot fi @ 2008-11-17 15:55 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #5 from aleksi dot nurmi at helsinki dot fi  2008-11-17 15:53 -------
GDB output showing that libc (debug) received a null pointer:

Program received signal SIGSEGV, Segmentation fault.
_IO_getc (fp=0x0) at getc.c:40
40        _IO_acquire_lock (fp);
(gdb) print fp
$1 = (FILE *) 0x0
(gdb) up
#1  0x000000000040051e in main () at fault.c:20
20          read_char(&our_stdin);
(gdb) print our_stdin.f
$2 = (FILE *) 0x7f75cc0c96a0
(gdb) print stdin
$3 = (struct _IO_FILE *) 0x7f75cc0c96a0


-- 

aleksi dot nurmi at helsinki dot fi changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|Accessor to stdin is breaks |Accessor to stdin breaks on
                   |on -O2 optimization         |-O2 optimization


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


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

* [Bug middle-end/38167] Accessor to stdin breaks on -O2 optimization
  2008-11-17  2:00 [Bug c/38167] New: Inline accessor to stdin is breaks on -O2 optimization aleksi dot nurmi at helsinki dot fi
                   ` (4 preceding siblings ...)
  2008-11-17 15:55 ` [Bug c/38167] Accessor to stdin " aleksi dot nurmi at helsinki dot fi
@ 2008-11-24 17:48 ` pinskia at gcc dot gnu dot org
  2008-11-24 18:19 ` aleksi dot nurmi at helsinki dot fi
                   ` (2 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2008-11-24 17:48 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #6 from pinskia at gcc dot gnu dot org  2008-11-24 17:46 -------
This is most likely one of the aliasing bugs.  Does -fno-strict-aliasing fix
the issue?


-- 

pinskia at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
          Component|c                           |middle-end
           Keywords|                            |wrong-code


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


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

* [Bug middle-end/38167] Accessor to stdin breaks on -O2 optimization
  2008-11-17  2:00 [Bug c/38167] New: Inline accessor to stdin is breaks on -O2 optimization aleksi dot nurmi at helsinki dot fi
                   ` (5 preceding siblings ...)
  2008-11-24 17:48 ` [Bug middle-end/38167] " pinskia at gcc dot gnu dot org
@ 2008-11-24 18:19 ` aleksi dot nurmi at helsinki dot fi
  2008-11-24 19:34 ` [Bug middle-end/38167] [4.2 Regression] " pinskia at gcc dot gnu dot org
  2009-03-31 15:51 ` jsm28 at gcc dot gnu dot org
  8 siblings, 0 replies; 10+ messages in thread
From: aleksi dot nurmi at helsinki dot fi @ 2008-11-24 18:19 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #7 from aleksi dot nurmi at helsinki dot fi  2008-11-24 18:17 -------
(In reply to comment #6)
> This is most likely one of the aliasing bugs.  Does -fno-strict-aliasing fix
> the issue?

Nope. I did a little research and it seems that -fno-tree-vrp fixes it.


-- 


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


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

* [Bug middle-end/38167] [4.2 Regression] Accessor to stdin breaks on -O2 optimization
  2008-11-17  2:00 [Bug c/38167] New: Inline accessor to stdin is breaks on -O2 optimization aleksi dot nurmi at helsinki dot fi
                   ` (6 preceding siblings ...)
  2008-11-24 18:19 ` aleksi dot nurmi at helsinki dot fi
@ 2008-11-24 19:34 ` pinskia at gcc dot gnu dot org
  2009-03-31 15:51 ` jsm28 at gcc dot gnu dot org
  8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2008-11-24 19:34 UTC (permalink / raw)
  To: gcc-bugs



-- 

pinskia at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
      Known to fail|                            |4.2.4
      Known to work|                            |4.4.0 4.3.0
            Summary|Accessor to stdin breaks on |[4.2 Regression] Accessor to
                   |-O2 optimization            |stdin breaks on -O2
                   |                            |optimization
   Target Milestone|---                         |4.2.5


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


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

* [Bug middle-end/38167] [4.2 Regression] Accessor to stdin breaks on -O2 optimization
  2008-11-17  2:00 [Bug c/38167] New: Inline accessor to stdin is breaks on -O2 optimization aleksi dot nurmi at helsinki dot fi
                   ` (7 preceding siblings ...)
  2008-11-24 19:34 ` [Bug middle-end/38167] [4.2 Regression] " pinskia at gcc dot gnu dot org
@ 2009-03-31 15:51 ` jsm28 at gcc dot gnu dot org
  8 siblings, 0 replies; 10+ messages in thread
From: jsm28 at gcc dot gnu dot org @ 2009-03-31 15:51 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #8 from jsm28 at gcc dot gnu dot org  2009-03-31 15:50 -------
Closing 4.2 branch, fixed in 4.3.


-- 

jsm28 at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
      Known to fail|4.2.4                       |4.2.4 4.2.5
         Resolution|                            |FIXED
   Target Milestone|4.2.5                       |4.3.0


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


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

end of thread, other threads:[~2009-03-31 15:51 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-11-17  2:00 [Bug c/38167] New: Inline accessor to stdin is breaks on -O2 optimization aleksi dot nurmi at helsinki dot fi
2008-11-17  2:03 ` [Bug c/38167] " aleksi dot nurmi at helsinki dot fi
2008-11-17  8:47 ` jakub at gcc dot gnu dot org
2008-11-17 14:08 ` aleksi dot nurmi at helsinki dot fi
2008-11-17 15:12 ` [Bug c/38167] Accessor " aleksi dot nurmi at helsinki dot fi
2008-11-17 15:55 ` [Bug c/38167] Accessor to stdin " aleksi dot nurmi at helsinki dot fi
2008-11-24 17:48 ` [Bug middle-end/38167] " pinskia at gcc dot gnu dot org
2008-11-24 18:19 ` aleksi dot nurmi at helsinki dot fi
2008-11-24 19:34 ` [Bug middle-end/38167] [4.2 Regression] " pinskia at gcc dot gnu dot org
2009-03-31 15:51 ` jsm28 at gcc dot gnu dot org

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