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

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