From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 4907 invoked by alias); 17 Nov 2008 02:00:57 -0000 Received: (qmail 25301 invoked by uid 48); 17 Nov 2008 01:59:34 -0000 Date: Mon, 17 Nov 2008 02:00:00 -0000 Subject: [Bug c/38167] New: Inline accessor to stdin is breaks on -O2 optimization X-Bugzilla-Reason: CC Message-ID: Reply-To: gcc-bugzilla@gcc.gnu.org To: gcc-bugs@gcc.gnu.org From: "aleksi dot nurmi at helsinki dot fi" Mailing-List: contact gcc-bugs-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-bugs-owner@gcc.gnu.org X-SW-Source: 2008-11/txt/msg01370.txt.bz2 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 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