fopen(NULL) causes segmentation fault

Andrew Wiseman a.wiseman@bandc.dircon.co.uk
Tue Mar 28 10:03:00 GMT 2000


VERSION
=======
This is 2.95.2 compiled for a Pentium
for Mandrake Linux 7.0 (Kernel 2.2.14-15mdk)

gcc -v reports:
Reading specs from /usr/lib/gcc-lib/i586-mandrake-linux/2.95.2/specs
gcc version 2.95.2 19991024 (release)

But this same "bug" can be found in
1. gcc version 2.7.2.1 under RedHat 5.2 (Kernel 2.0.36)
2. gcc version 2.7.2.3 under Corel Linux 1.0 (Kernel 2.2.12)

But it does NOT appear in
gcc version 2.7.0 under SunSoft Interactive Unix 4.1a (i/c maintenance
release)

SOURCE TO REPRODUCE BEHAVIOUR (also attached)
=============================
main()
{
     long *file_rep = 0x0;
     fclose(file_rep);
}

BEHAVIOUR
=========
When you compile the source above and run it...

gcc gcc2.c
./a.out

You get...

Segmentation fault

Now you might say that fclose(NULL) is a bit of a silly thing to do. (And in
the real world program that led me to discover this problem, file_rep above
was not supposed to be NULL but was due to a bug in my code.) Anyway, I
don't believe it should cause a segmentation fault. I have used the same
(buggy!) code on many different machines (Suns, PowerPC machines and others)
running a variety of operating systems (AIX, Solaris 1 & 2, SCO and others)
with a number of different compilers, and not one has ever given me a
segmentation fault in this way.

MACHINE DETAILS
===============
A PC containing a Cyrix running at 133 MHz (equivalent to a Pentium 166,
apparently).
Operating System: Mandrake Linux 7.0 (Kernel 2.2.14-15mdk)
Compiler was installed as is from the CDROM.
As mentioned above, this also occurs in other versions of GCC with other
versions of Linux.

--
Andrew Wiseman
Software Production Manager
BENSASSON & CHALMERS LIMITED
-------------- next part --------------
A non-text attachment was scrubbed...
Name: gcc2.c
Type: text/x-c
Size: 122 bytes
Desc: not available
URL: <http://gcc.gnu.org/pipermail/gcc-bugs/attachments/20000328/25aebcf8/attachment.bin>


More information about the Gcc-bugs mailing list