gfortran segfault on continuation at end of program followed by two or more newlines

Bug #1720699 reported by Andrew Gaspar
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
gcc
Confirmed
Medium
gcc-5 (Ubuntu)
Incomplete
Undecided
Unassigned
gcc-6 (Ubuntu)
New
Undecided
Unassigned
gcc-7 (Ubuntu)
New
Undecided
Unassigned

Bug Description

If you end a FORTRAN program with the continuation character '&' followed by two newlines, gfortran SEGFAULTs when attempting to compile the program.

This issue has a very minimal repro:

file: test.f90
```
end program &

```

The two newlines at the end are significant. The problem does not repro with 1 or 0 newlines, though it does repro with more newlines. The problem also repros when any of the lines following the continuation contain a comment. The problem repros with both LF and CRLF line endings.

I believe this program should compile without error.

Putting a token anywhere after the continuation, even separated by 2 or more newlines, results in the expected error messages.

I'm running this on Windows Subsystem for Linux, though it's not likely that is causing this bug.

The SEGFAULT error encountered:
```
andre@ANDREW-DESKTOP:/mnt/c/Users/andre/Projects/scratch$ gfortran test.f90 -o hello
f951: internal compiler error: Segmentation fault
Please submit a full bug report,
with preprocessed source if appropriate.
See <file:///usr/share/doc/gcc-5/README.Bugs> for instructions.
```

gfortran --version
```
andre@ANDREW-DESKTOP:/mnt/c/Users/andre/Projects/scratch$ gfortran --version
GNU Fortran (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609
Copyright (C) 2015 Free Software Foundation, Inc.
```

lsb_release -rd
```
andre@ANDREW-DESKTOP:/mnt/c/Users/andre/Projects/scratch$ lsb_release -rd
Description: Ubuntu 16.04.3 LTS
Release: 16.04
```

apt-cache policy gfortran
```
andre@ANDREW-DESKTOP:/mnt/c/Users/andre/Projects/scratch$ apt-cache policy gfortran
gfortran:
  Installed: 4:5.3.1-1ubuntu1
  Candidate: 4:5.3.1-1ubuntu1
  Version table:
 *** 4:5.3.1-1ubuntu1 500
        500 http://archive.ubuntu.com/ubuntu xenial/main amd64 Packages
        100 /var/lib/dpkg/status
```

Revision history for this message
Matthias Klose (doko) wrote :

please recheck with 16.04 and 17.10.

Changed in gcc-5 (Ubuntu):
status: New → Incomplete
Revision history for this message
Andrew Gaspar (angasp) wrote :

Repros in 17.04 (Hyper-V VM). Looking into 17.10.

gfortran test.f90 -o hello
```
f951: internal compiler error: Segmentation fault
Please submit a full bug report,
with preprocessed source if appropriate.
See <file:///usr/share/doc/gcc-6/README.Bugs> for instructions.
```

gfortran --version
```
GNU Fortran (Ubuntu 6.3.0-12ubuntu2) 6.3.0 20170406
Copyright (C) 2016 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
```

lsb_release -rd
```
Description: Ubuntu 17.04
Release: 17.04
```

apt-cache policy gfortran
```
gfortran:
  Installed: 4:6.3.0-2ubuntu1
  Candidate: 4:6.3.0-2ubuntu1
  Version table:
*** 4:6.3.0-2ubuntu1 500
        500 http://archive.ubuntu.com/ubuntu zesty/main amd64 Packages
        100 /var/lib/dpkg/status
```

Revision history for this message
Andrew Gaspar (angasp) wrote :

I assumed you meant 17.04, anyway. The original bug was filed against 16.04.

Revision history for this message
Andrew Gaspar (angasp) wrote :

Also repros in 17.10 (Hyper-V VM). Additional warning in compilation now.

gfortran test.f90 -o hello
```
f951: Warning: No location in statement
f951: internal compiler error: Segmentation fault
Please submit a full bug report,
with preprocessed source if appropriate.
See <file:///usr/share/doc/gcc-7/README.Bugs> for instructions.
```

gfortran --version
```
GNU Fortran (Ubuntu 7.2.0-7ubuntu1) 7.2.0
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
```

lsb_release -rd
```
Description: Ubuntu Artful Aardvark (development branch)
Release: 17.10
```

apt-cache policy gfortran
```
gfortran:
  Installed: 4:7.2.0-1ubuntu1
  Candidate: 4:7.2.0-1ubuntu1
  Version table:
 *** 4:7.2.0-1ubuntu1 500
        500 http://archive.ubuntu.com/ubuntu artful/main amd64 Packages
        100 /var/lib/dpkg/status
```

Revision history for this message
In , Matthias Klose (doko) wrote :

seen on all active versions (test file ends with two newlines):

$ cat test.f90
end program &

$ /usr/lib/gcc-snapshot/bin/gfortran test.f90
f951: Warning: No location in statement
f951: internal compiler error: Segmentation fault
0x7f3381724fbf ???
        /build/glibc-hBZSf0/glibc-2.27/signal/../sysdeps/unix/sysv/linux/x86_64/sigaction.c:0
0x7f3381711b16 __libc_start_main
        ../csu/libc-start.c:310
Please submit a full bug report,
with preprocessed source if appropriate.
Please include the complete backtrace with any bug report.

Revision history for this message
In , Dominiq (dominiq) wrote :

Confirmed from 4.8 up to trunk (9.0). An instrumented compiler gives

f951: Warning: No location in statement
../../work/gcc/fortran/trans.c:1768:39: runtime error: member access within null pointer of type 'struct gfc_linebuf'
f951: internal compiler error: Segmentation fault: 11

Changed in gcc:
importance: Unknown → Medium
status: Unknown → Confirmed
Revision history for this message
In , Anlauf (anlauf) wrote :

(In reply to Dominique d'Humieres from comment #1)
> Confirmed from 4.8 up to trunk (9.0). An instrumented compiler gives
>
> f951: Warning: No location in statement
> ../../work/gcc/fortran/trans.c:1768:39: runtime error: member access within
> null pointer of type 'struct gfc_linebuf'
> f951: internal compiler error: Segmentation fault: 11

The warning in comment#0 would be restored by the patch:

Index: gcc/fortran/trans.c
===================================================================
--- gcc/fortran/trans.c (revision 267065)
+++ gcc/fortran/trans.c (working copy)
@@ -1765,8 +1765,11 @@
 void
 gfc_set_backend_locus (locus * loc)
 {
- gfc_current_backend_file = loc->lb->file;
- input_location = loc->lb->location;
+ if (loc->lb)
+ {
+ gfc_current_backend_file = loc->lb->file;
+ input_location = loc->lb->location;
+ }
 }

However, this papers over the issue that the locus is not properly set.

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.