Bug 66861 - [5/6 Regression] Segmentation fault in gcc/testsuite/gfortran.dg/streamio_5.f90
Summary: [5/6 Regression] Segmentation fault in gcc/testsuite/gfortran.dg/streamio_5.f90
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: libfortran (show other bugs)
Version: 5.1.1
: P3 normal
Target Milestone: 5.2
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2015-07-13 16:30 UTC by Rainer Emrich
Modified: 2015-07-15 07:10 UTC (History)
1 user (show)

See Also:
Host: x86_64-w64-mingw32
Target: x86_64-w64-mingw32
Build: x86_64-w64-mingw32
Known to work: 4.9.3
Known to fail: 5.1.0, 5.1.1
Last reconfirmed: 2015-07-13 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Rainer Emrich 2015-07-13 16:30:25 UTC
Since gcc-5 I get a lot of fortran testsuite failures. Most of these with IO involved and causing a timeout. This an regression. For trunk I'm not sure. I haven't had the time to build trunk yet. But I expect it has the same issue.

$ gfortran.exe -v
Es werden eingebaute Spezifikationen verwendet.
COLLECT_GCC=D:\opt\devel\gnu\gcc\MINGW_NT\x86_64-w64-mingw32\mingw-w64-runtime-trunk-svn\gcc-5.1.1\bin\gfortran.exe
COLLECT_LTO_WRAPPER=d:/opt/devel/gnu/gcc/mingw_nt/x86_64-w64-mingw32/mingw-w64-runtime-trunk-svn/gcc-5.1.1/bin/../libexec/gcc/x86_64-w64-mingw32/5.1.1/lto-wrapper.exe
Ziel: x86_64-w64-mingw32
Konfiguriert mit: ../../../../../../opt/devel/gnu/src/gcc-mingw-w64/gcc-5.1.1/configure --prefix=/opt/devel/gnu/gcc/MINGW_NT/x86_64-w64-mingw32/mingw-w64-runtime-trunk-svn/gcc-5.1.1 --with-gnu-as --with-as=/opt/devel/gnu/gcc/MINGW_NT/x86_64-w64-mingw32/mingw-w64-runtime-trunk-svn/gcc-5.1.1/bin/as --with-gnu-ld --with-ld=/opt/devel/gnu/gcc/MINGW_NT/x86_64-w64-mingw32/mingw-w64-runtime-trunk-svn/gcc-5.1.1/bin/ld --build=x86_64-w64-mingw32 --enable-threads=posix --enable-languages=c,ada,c++,fortran,java,lto,objc,obj-c++ --with-gmp-include=/opt/devel/SCRATCH/tmp.kzqcdNeWDQ/install/include --with-gmp-lib=/opt/devel/SCRATCH/tmp.kzqcdNeWDQ/install/lib64 --with-mpfr-include=/opt/devel/SCRATCH/tmp.kzqcdNeWDQ/install/include --with-mpfr-lib=/opt/devel/SCRATCH/tmp.kzqcdNeWDQ/install/lib64 --with-mpc-include=/opt/devel/SCRATCH/tmp.kzqcdNeWDQ/install/include --with-mpc-lib=/opt/devel/SCRATCH/tmp.kzqcdNeWDQ/install/lib64 --with-isl-include=/opt/devel/SCRATCH/tmp.kzqcdNeWDQ/install/include --with-isl-lib=/opt/devel/SCRATCH/tmp.kzqcdNeWDQ/install/lib64 --with-local-prefix=/opt/devel/tec/devel/MINGW_NT/x86_64-w64-mingw32/mingw-w64-runtime-trunk-svn/gcc-5.1.1 --enable-libgomp --enable-fully-dynamic-string --disable-multilib --enable-checking=release --disable-werror --with-sysroot=/x86_64-w64-trunk
Thread-Modell: posix
gcc-Version 5.1.1 20150712 [gcc-5-branch revision 225722] (GCC)



gfortran.exe -O2 /opt/devel/gnu/src/gcc/gcc-5-svn/gcc/testsuite/gfortran.dg/streamio_5.f90

$ gdb a.exe
GNU gdb (GDB) 7.9
Copyright (C) 2015 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-w64-mingw32".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from a.exe...done.
(gdb) run
Starting program: D:\msys64\home\rainer\temp\a.exe
[New Thread 4676.0x529c]

Program received signal SIGSEGV, Segmentation fault.
0x000007fefd675960 in strcmp () from C:\Windows\system32\msvcrt.dll
(gdb) where
#0  0x000007fefd675960 in strcmp () from C:\Windows\system32\msvcrt.dll
#1  0x000000006f6eaa94 in find_file0 (u=<optimized out>,
    id=id@entry=162129586587129478, path=path@entry=0x786300 "teststream")
    at ../../../../../../../../opt/devel/gnu/src/gcc-mingw-w64/gcc-5.1.1/libgfortran/io/unix.c:1573
#2  0x000000006f6c9209 in _gfortrani_find_file (file=<optimized out>,
    file_len=<optimized out>)
    at ../../../../../../../../opt/devel/gnu/src/gcc-mingw-w64/gcc-5.1.1/libgfortran/io/unix.c:1615
#3  0x000000006f6cf385 in _gfortrani_new_unit (opp=opp@entry=0x22fc10,
    u=u@entry=0x785ff0, flags=flags@entry=0x22fb80)
    at ../../../../../../../../opt/devel/gnu/src/gcc-mingw-w64/gcc-5.1.1/libgfortran/io/open.c:486
#4  0x000000006f6bd922 in already_open (flags=<optimized out>,
    u=<optimized out>, opp=<optimized out>)
    at ../../../../../../../../opt/devel/gnu/src/gcc-mingw-w64/gcc-5.1.1/libgfortran/io/open.c:672
#5  _gfortran_st_open (opp=0x22fc10)
    at ../../../../../../../../opt/devel/gnu/src/gcc-mingw-w64/gcc-5.1.1/libgfortran/io/open.c:837
#6  0x000000000040179c in MAIN__ ()
#7  0x0000000000403080 in main ()
(gdb) exit
Undefined command: "exit".  Try "help".
(gdb) quit
A debugging session is active.

        Inferior 1 [process 4676] will be killed.

Quit anyway? (y or n) [answered Y; input not from terminal]


So, the crash appears in msvcrt.dll. But the call looks suspicious:
find_file0 (u=<optimized out>, id=id@entry=162129586587129478, path=path@entry=0x786300 "teststream")

Using gcc-4.9.3 is fine, no issue here.
Comment 1 Rainer Emrich 2015-07-13 17:14:22 UTC
I suspect the following commit causing the issue:

215307:

File size: 43008 byte(s)

PR libfortran/62768 Handle filenames with embedded null characters.

testsuite ChangeLog:

2014-09-17  Janne Blomqvist  <jb@gcc.gnu.org>

	PR libfortran/62768
	* gfortran.dg/filename_null.f90: New test.

libgfortran ChangeLog:

2014-09-17  Janne Blomqvist  <jb@gcc.gnu.org>

	PR libfortran/62768
	* io/io.h (gfc_unit): Store C string for the filename.
	* io/close.c (st_close): Use gfc_unit.filename.
	* io/inquire.c (inquire_via_unit): Likewise.
	* io/open.c (new_unit): Likewise.
	(already_open): Likewise, unlink file before freeing filename.
	* io/unit.c (init_units): Likewise.
	(close_unit_1): Likewise.
	(filename_from_unit): Likewise.
	* io/unix.c (compare_file_filename): Likewise.
	(find_file0): Likewise.
	(delete_file): Likewise.
Comment 2 Janne Blomqvist 2015-07-13 18:48:30 UTC
Hmm, does the following patch fix it?

diff --git a/libgfortran/io/unix.c b/libgfortran/io/unix.c
index e5fc6e1..0048e24 100644
--- a/libgfortran/io/unix.c
+++ b/libgfortran/io/unix.c
@@ -1570,7 +1570,7 @@ find_file0 (gfc_unit *u, FIND_FILE0_DECL)
     }
   else
 # endif
-    if (strcmp (u->filename, path) == 0)
+    if (u->filename && strcmp (u->filename, path) == 0)
       return u;
 #endif


(Sorry, I don't have a windows machine to test on myself..)
Comment 3 Janne Blomqvist 2015-07-13 18:56:22 UTC
Or rather, also fixing another similar potential issue, you might instead want to test this:

diff --git a/libgfortran/io/unix.c b/libgfortran/io/unix.c
index e5fc6e1..a1ce9a3 100644
--- a/libgfortran/io/unix.c
+++ b/libgfortran/io/unix.c
@@ -1525,7 +1525,10 @@ compare_file_filename (gfc_unit *u, const char *name, int len)
       goto done;
     }
 # endif
-  ret = (strcmp(path, u->filename) == 0);
+  if (u->filename)
+    ret = (strcmp(path, u->filename) == 0);
+  else
+    ret = 0;
 #endif
  done:
   free (path);
@@ -1570,7 +1573,7 @@ find_file0 (gfc_unit *u, FIND_FILE0_DECL)
     }
   else
 # endif
-    if (strcmp (u->filename, path) == 0)
+    if (u->filename && strcmp (u->filename, path) == 0)
       return u;
 #endif
Comment 4 Rainer Emrich 2015-07-13 21:49:46 UTC
Am 13.07.2015 um 20:56 schrieb jb at gcc dot gnu.org:
> https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66861
> 
> --- Comment #3 from Janne Blomqvist <jb at gcc dot gnu.org> --- Or rather,
> also fixing another similar potential issue, you might instead want to test
> this:
> 
> diff --git a/libgfortran/io/unix.c b/libgfortran/io/unix.c index
> e5fc6e1..a1ce9a3 100644 --- a/libgfortran/io/unix.c +++
> b/libgfortran/io/unix.c @@ -1525,7 +1525,10 @@ compare_file_filename
> (gfc_unit *u, const char *name, int len) goto done; } # endif -  ret =
> (strcmp(path, u->filename) == 0); +  if (u->filename) +    ret =
> (strcmp(path, u->filename) == 0); +  else +    ret = 0; #endif done: free
> (path); @@ -1570,7 +1573,7 @@ find_file0 (gfc_unit *u, FIND_FILE0_DECL) } 
> else # endif -    if (strcmp (u->filename, path) == 0) +    if (u->filename
> && strcmp (u->filename, path) == 0) return u; #endif
> 
I will test this tomorrow.
Comment 5 Rainer Emrich 2015-07-14 12:30:52 UTC
(In reply to Janne Blomqvist from comment #3)
> Or rather, also fixing another similar potential issue, you might instead
> want to test this:
> 
> diff --git a/libgfortran/io/unix.c b/libgfortran/io/unix.c
> index e5fc6e1..a1ce9a3 100644
> --- a/libgfortran/io/unix.c
> +++ b/libgfortran/io/unix.c
> @@ -1525,7 +1525,10 @@ compare_file_filename (gfc_unit *u, const char *name,
> int len)
>        goto done;
>      }
>  # endif
> -  ret = (strcmp(path, u->filename) == 0);
> +  if (u->filename)
> +    ret = (strcmp(path, u->filename) == 0);
> +  else
> +    ret = 0;
>  #endif
>   done:
>    free (path);
> @@ -1570,7 +1573,7 @@ find_file0 (gfc_unit *u, FIND_FILE0_DECL)
>      }
>    else
>  # endif
> -    if (strcmp (u->filename, path) == 0)
> +    if (u->filename && strcmp (u->filename, path) == 0)
>        return u;
>  #endif

This solves the issue!

Richard set target milestone to 5.2. So I suppose this should go into trunk and the gcc-5 branch.
Comment 6 Janne Blomqvist 2015-07-14 20:26:38 UTC
Author: jb
Date: Tue Jul 14 20:26:06 2015
New Revision: 225788

URL: https://gcc.gnu.org/viewcvs?rev=225788&root=gcc&view=rev
Log:
PR 66861 Fix null pointer crash on mingw.

2015-07-14  Janne Blomqvist  <jb@gcc.gnu.org>

	PR libfortran/66861
	* io/unix.c (compare_file_filename): Verify that u->filename is
	non-NULL before strcmp.
	(find_file0): Likewise.

Modified:
    trunk/libgfortran/ChangeLog
    trunk/libgfortran/io/unix.c
Comment 7 Janne Blomqvist 2015-07-14 20:29:19 UTC
Fixed on trunk. I'm not sure if the milestone thing implies an Ok to commit to the gcc-5 branch, as we're already on 5.2 rc2, I'll ask explicitly for an Ok.
Comment 8 Janne Blomqvist 2015-07-15 07:00:55 UTC
Author: jb
Date: Wed Jul 15 07:00:23 2015
New Revision: 225805

URL: https://gcc.gnu.org/viewcvs?rev=225805&root=gcc&view=rev
Log:
PR 66861 Fix null pointer crash on mingw.

2015-07-15  Janne Blomqvist  <jb@gcc.gnu.org>

	PR libfortran/66861
	* io/unix.c (compare_file_filename): Verify that u->filename is
	non-NULL before strcmp.
	(find_file0): Likewise.

Modified:
    branches/gcc-5-branch/libgfortran/ChangeLog
    branches/gcc-5-branch/libgfortran/io/unix.c
Comment 9 Janne Blomqvist 2015-07-15 07:10:39 UTC
Fixed on gcc-5 branch as well, release manager approval at https://gcc.gnu.org/ml/gcc/2015-07/msg00184.html .

Closing as fixed, thanks Rainer for reporting the issue and testing!