This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c/38167] New: Inline accessor to stdin is breaks on -O2 optimization
- From: "aleksi dot nurmi at helsinki dot fi" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: 17 Nov 2008 01:59:34 -0000
- Subject: [Bug c/38167] New: Inline accessor to stdin is breaks on -O2 optimization
- Reply-to: gcc-bugzilla at gcc dot gnu dot org
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