Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

Segmentation faults when reading simple files of certain number of lines

Matti_Taskinen
Beginner
558 Views

Intel Fortran compiler versions 17 seem to produce segmentation faults in Linux for simple files of certain number of lines.

The following program creates a file in a loop so that the number of lines in the file increases in each step of the loop. After writing the file with line numbers (as real numbers!) on each line, the program tries to read the file back.

program test_error
  implicit none
  
  character(len=*), parameter :: file  = "test_error.txt"
  integer, parameter :: unit = 1, jmax = 2**16
  integer :: N, io, i, j
  real :: x
  
  do j = 1, jmax

    ! Write j (real valued) lines to file:
    print *,"Number of lines in file " // trim(file) // ": ", j
    open(unit, file=file, status='replace')
    do i = 1, j
      x = i
      write(unit,*) x  ! x gives error, i does not!!!
    end do
    close(unit)

    ! Read lines from file until EOF (or error):
    open(unit, file=file, status='old')
    N = 0
    do
      read(unit, *, iostat=io) x
      if (io /= 0) exit
      N = N + 1
      if (N .GT. j) print *,'  Error: Extra line ', N, ' value: ',x
    end do
    print *,'  IO status:  ', io
    print *,'  Lines read: ', N
    close(unit)
    
  end do

end program test_error

This works most of the time but with the file of 1023 lines the program manages to read also an imaginary 1024th line containing value 512 and then segfaults:

...
 Number of lines in file test_error.txt:         1021
   IO status:            -1
   Lines read:         1021
 Number of lines in file test_error.txt:         1022
   IO status:            -1
   Lines read:         1022
 Number of lines in file test_error.txt:         1023
   Error: Extra line         1024  value:    512.0000    
forrtl: severe (174): SIGSEGV, segmentation fault occurred
Image              PC                Routine            Line        Source             
test_error         0000000000482921  Unknown               Unknown  Unknown
test_error         0000000000480A5B  Unknown               Unknown  Unknown
test_error         000000000043AC44  Unknown               Unknown  Unknown
test_error         000000000043AA56  Unknown               Unknown  Unknown
test_error         0000000000406A09  Unknown               Unknown  Unknown
test_error         0000000000409FD6  Unknown               Unknown  Unknown
libpthread-2.17.s  00007F83A1A45100  Unknown               Unknown  Unknown
test_error         000000000045808C  Unknown               Unknown  Unknown
test_error         000000000041D11D  Unknown               Unknown  Unknown
test_error         00000000004030AB  MAIN__                     24  test_error.f90
test_error         000000000040295E  Unknown               Unknown  Unknown
libc-2.17.so       00007F83A1492B15  __libc_start_main     Unknown  Unknown
test_error         0000000000402869  Unknown               Unknown  Unknown

If the loop is continued (manually) the error appears also after every "half kiloline" (512), i.e. with files of 1023, 1535, 2047, 2559, 3071 (and so on) lines and the extra value read is similarly 512, 1024, 1536, 2048, 2560, ...

This happens in CentOS 7.2 and Ubuntu 16.04 with compiler versions 17.0.0, 17.0.1, and 17.0.2. Earlier Intel Fortran compilers (tested 14.0.1, 15.0.1, 15.0.2, and 16.0.1) do not produce the error.

Can you replicate the error? Is this a bug in the compiler or on the Linux side?

Thanks,

  Matti

0 Kudos
3 Replies
jimdempseyatthecove
Honored Contributor III
558 Views
0 Kudos
Matti_Taskinen
Beginner
558 Views

Both -assume buffered_io and -assume nobuffered_io gave the same extra lines and segfaults. Option -heap-arrays, however, prevents the segfaults but still the  program reads the extra lines.

0 Kudos
Kevin_D_Intel
Employee
558 Views

I can reproduce this with 16.x and 17.x compilers. I cannot with 15.x. With 16.x or later, the failure appears related to the blocksize. By increasing via the env.-variable, FORT_BLOCKSIZE=16384, the program runs a bit further reaching 4096 iterations for the outer loop, but I just saw your update about -heap-arrays.

I reported it to our Developers. Thank you for the convenient reproducer.

(Internal tracking id: DPD200419642)

0 Kudos
Reply