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

Segmentation fault during read at end of file

Robin_T_
Novice
554 Views

I read a file using the following code 

program read_eof_test
  use, intrinsic :: ISO_FORTRAN_ENV
  IMPLICIT NONE
  
  real (real64) :: a, b, c, d, e
  
  integer :: fn_fid, ios, in_shot_fid  
  
  character (len = 511) :: in_shot_fn  
    
  in_shot_fn = 'file_orig.dat'
 
  open (newunit = in_shot_fid, file = trim (in_shot_fn), status = 'old')

  do
        
    read (in_shot_fid, *, iostat = ios) a, b, c, d, e
    print *, a, b, c, d, e
    print *, ios          
    if (ios < 0) exit
    if (ios > 0) stop 'ERROR reading file.' 
      
  enddo
  
  close (in_shot_fid)
  
end program read_eof_test

which is compiled using this makefile

DEBUG = -O0 -g -traceback -C -debug all

GENERAL = -m64 -fpp -fopenmp $(DEBUG) -e08

all: read_eof_test.x 
read_eof_test.x: read_eof_test.o
	ifort read_eof_test.o $(GENERAL) -o read_eof_test.x
	
read_eof_test.o: read_eof_test.f90
	ifort $(GENERAL) -c read_eof_test.f90	

Usually, this works fine. However for some files, a segmentation fault is caused on end of file. For example, a file ending with these lines

34 167.36134   20.07923 0.7672  370268674.194989
41 167.36220   20.06267 0.7792  370268674.319989

will give 

   34.0000000000000        167.361340000000        20.0792300000000     
  0.767200000000000        370268674.194989     
           0
   41.0000000000000        167.362200000000        20.0626700000000     
  0.779200000000000        370268674.319989     
           0
   8624.19499000000        1.50000000000000        166.945570000000     
   26.3012800000000       -2.48660000000000     
           0
forrtl: severe (174): SIGSEGV, segmentation fault occurred
Image              PC                Routine            Line        Source             
read_eof_test.x    000000000040B12D  Unknown               Unknown  Unknown
libpthread-2.22.s  00002B2F9D958B20  Unknown               Unknown  Unknown
read_eof_test.x    000000000045A483  Unknown               Unknown  Unknown
read_eof_test.x    000000000041D616  Unknown               Unknown  Unknown
read_eof_test.x    0000000000402DAC  MAIN__                     18  read_eof_test.f90
read_eof_test.x    0000000000402B1E  Unknown               Unknown  Unknown
libc-2.22.so       00002B2F9DD896D5  __libc_start_main     Unknown  Unknown
read_eof_test.x    0000000000402A29  Unknown               Unknown  Unknown

If I then change only one character in the file

34 167.36134   20.07923 0.7672  370268674.194989
41 167.3622   20.06267 0.7792  370268674.319989

the file is read fine

   34.0000000000000        167.361340000000        20.0792300000000     
  0.767200000000000        370268674.194989     
           0
   41.0000000000000        167.362200000000        20.0626700000000     
  0.779200000000000        370268674.319989     
           0
   41.0000000000000        167.362200000000        20.0626700000000     
  0.779200000000000        370268674.319989     
          -1

So, I guess this is somehow related to the specific file, but I do not see why the segmentation fault occurs sometimes. I have a large amount of files that need to be read, and many of them exhibit this behaviour, but I do not have a way to tell in advance.

The example file is attached. It is quite long, but I cannot shorten it without modifying the outcome.

0 Kudos
2 Replies
Juergen_R_R
Valued Contributor I
554 Views

Dear Robin,

I do get the segmentation fault as you have described with ifort v17.0.8, but not with v18.0.5 or v19.0.1. (neither do I get it with gfortran 5.4 nor nagfor 6.2 nor PGI v18.7) So could it be that you are using an old Intel compiler? (and this is a problem that has been known to Intel and has been fixed meanwhile)

 

0 Kudos
Robin_T_
Novice
554 Views

Hi Jürgen, thanks for testing it. I am indeed using ifort version 18.0.0. I will see if I can update my compiler.

0 Kudos
Reply