Re: OFF-Topic glibc behavior

H. Peter Anvin (hpa@transmeta.com)
22 Oct 1998 04:37:59 GMT


Followup to: <Pine.LNX.3.95.981021131237.241A-100000@chaos.analogic.com>
By author: "Richard B. Johnson" <root@chaos.analogic.com>
In newsgroup: linux.dev.kernel
>
> The following code will write "Hello World!" to the screen on
> the following platforms:
>
> MS-DOS with Micro$oft 'C' 6.0 and Micro$oft 7.01 compilers
> MS-DOS with Borland Turbo-C 3.0
> VAX/VMS VAX-C V3.0
>
> It just waits in a CPU-eating race on SunOs 5.5.1
> With glibc on Linux, it seg-faults.
>
> This is not meant to be flame-bait. I know this is not how to
> write code. However, the effect of this kind of coding does
> happen when structures containing file-pointers are duplicated
> so it can (does) happen.
>

No. You can duplicate a file POINTER. You can't duplicate a file
STRUCTURE. There is no excuse for it either -- your user program
should never mess with a FILE structure but only have pointers to it.

>
> Glibc doesn't check the contents of the FILE structures, just
> the addresses and since it "knows" the pointer received was not
> one it provided, it generates signal 11 on its own. Is this
> REALLY what is supposed to happen? After all, the function call
> did get a perfectly valid pointer to the required structure.
>

No it didn't. You duplicated the FILE *structure*, which may itself
contain pointers to other structures inside libc.

Your program is totally and utterly broken. SIGSEGV is a perfectly
acceptable and quite reasonable.

-hpa

-- 
    PGP: 2047/2A960705 BA 03 D3 2C 14 A8 A8 BD  1E DF FE 69 EE 35 BD 74
    See http://www.zytor.com/~hpa/ for web page and full PGP public key
        I am Bahá'í -- ask me about it or see http://www.bahai.org/
   "To love another person is to see the face of God." -- Les Misérables

- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.rutgers.edu Please read the FAQ at http://www.tux.org/lkml/