How to solve exit code 139 error when reading from file on unix

SIGSEGV are not always thrown due to a root cause of memory access problems...

Perl throws a 139 on Unix usually because of file I/O. You might have accidentally deleted your input files.


TL;DR: Your program tried to access a memory location it had no permissions to access, so the operating system killed it.


First: The code "139" doesn't matter, forget about the number. Your program was terminated after "getting a SIGSEGV", or a signall regarding a segmentation violation. Read about what that means here:

What causes a SIGSEGV

(never mind that question is about C++, same idea.)

Now, why would this happen? You must be making some assumptions you shouldn't be. Looking at your code, it might be:

  • Reading a very long string from the file which exceeds the bounds of the loginInformation array - and perhaps even the bounds of the memory region allocated to your program overall.
  • Scanning from an invalid-state/uninitialized/null file descriptor, as in @xuhdev's answer
  • (Unlikely/impossible) Ignoring some error generated by one of the fscanf() calls (you need to check errno if a scan failed).

I think that covers it although maybe I missed something. Instead of speculating you can actually check what happened using a debugger on the core dump:

How do I analyze a program's core dump file with GDB when it has command-line parameters?


It means the program crashed before it exited. You need to debug the program. For example, you need to check whether the file is successfully opened after fopen.

Tags:

Unix

C