[bitc-dev] A few bugs

Aleksi Nurmi aleksi.nurmi at helsinki.fi
Sun Nov 16 20:12:09 CST 2008


2008/11/17 Aleksi Nurmi <aleksi.nurmi at helsinki.fi>:
>> I was unable to reproduce this bug. How are you running it?
>
> I'm sorry to hear that. Sometimes I'm unable to reproduce it myself.
> Let's hope there aren't any obscure memory corrupting pointers!

Ha, found it - a likely GCC (4.2.4-1ubuntu3) bug! I guess the
optimizer assumes that stdin is constant or something. The following
code segfaults if access_iostream is defined as inline and the -O2
optimization is turned on. A quick fix would be to remove the inline
keyword from the function fix_stdio_stream - might be necessary as
there are probably quite a few GCC 4.2s around even in the near
future.

#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;
}

Aleksi Nurmi


More information about the bitc-dev mailing list