Search code examples
csegmentation-faultgdb

Segmentation fault: 1902 vfscanf.c: No such file or directory


The program stops with segmentation fault at scanf() function.

int main() {
  int x = 0;
  printf("$ ");
  scanf("%i", x);

  return 0;
}

And GDB returns following error:

(gdb) run
Program received signal SIGSEGV, Segmentation fault.
0x00007ffff71f6794 in _IO_vfscanf_internal (s=<optimized out>, format=<optimized out>, argptr=argptr@entry=0x7fffffffe608, errp=errp@entry=0x0) at vfscanf.c:1902
1902    vfscanf.c: No such file or directory.

I tried searching the internet for vfscanf.c but haven't found much helpful information.

The program was compiled with g++, which is (same as GDB) newly updated.


Solution

  • scanf("%i", &x);
    //          ^ THIS IS THE IMPORTANT BIT
    

    gcc even tells you about this:

    foo.c: In function ‘main’:
    foo.c:6:9: warning: format ‘%i’ expects argument of type ‘int *’, but argument 2 has type ‘int’ [-Wformat=]
       scanf("%i", x);
             ^
    

    The vfscanf.c: No such file or directory is just gdb complaining that it can't find the source code for scanf.