Search code examples
csegmentation-faultvalgrind

Errors in valgrind when reading a file - C


So, i'm having trouble figuring out what is the meaning of the errors valgrind is giving me.

My code receives a sudoku file and checks if it is correct or not. It receives the name of the file in command line, opens it and does everything it should, which is checking if it is correct.

Example:

./run sudoku.txt

The code is running absolutey fine, but when i runned valgrind, it accused two errors that i didn't understand at all, but i assumed it was in the file handling.

Here's the valgrind run:

==20853== Syscall param openat(filename) points to unaddressable byte(s)
==20853==    at 0x4F4BD9E: open (open64.c:47)
==20853==    by 0x4EC85F9: _IO_file_open (fileops.c:189)
==20853==    by 0x4EC85F9: _IO_file_fopen@@GLIBC_2.2.5 (fileops.c:281)
==20853==    by 0x4EBAF19: __fopen_internal (iofopen.c:78)
==20853==    by 0x4EBAF19: fopen@@GLIBC_2.2.5 (iofopen.c:89)
==20853==    by 0x108BD1: main (q1_acs2.c:90)
==20853==  Address 0x0 is not stack'd, malloc'd or (recently) free'd
==20853== 
==20853== Invalid read of size 4
==20853==    at 0x4EB82BD: __isoc99_fscanf (isoc99_fscanf.c:30)
==20853==    by 0x108D4E: main (q1_acs2.c:96)
==20853==  Address 0x0 is not stack'd, malloc'd or (recently) free'd
==20853== 
==20853== 
==20853== Process terminating with default action of signal 11 (SIGSEGV)
==20853==  Access not within mapped region at address 0x0
==20853==    at 0x4EB82BD: __isoc99_fscanf (isoc99_fscanf.c:30)
==20853==    by 0x108D4E: main (q1_acs2.c:96)
==20853==  If you believe this happened as a result of a stack
==20853==  overflow in your program's main thread (unlikely but
==20853==  possible), you can try to increase the size of the
==20853==  main thread stack using the --main-stacksize= flag.
==20853==  The main thread stack size used in this run was 8388608.
==20853== 
==20853== HEAP SUMMARY:
==20853==     in use at exit: 0 bytes in 0 blocks
==20853==   total heap usage: 1 allocs, 1 frees, 552 bytes allocated
==20853== 
==20853== All heap blocks were freed -- no leaks are possible
==20853== 
==20853== For counts of detected and suppressed errors, rerun with: -v
==20853== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)
Segmentation Fault

And here's the part of my code where i open the file and store it in a matrix: (I'll put the lines numbers in so you can see where it's pointing the errors)

85 int main(int argc, char *argv[])
86 {
87 int r = 9, c = 9, flag = 0;
88 FILE *px;
89
90 px = fopen(argv[1], "r");
91 int mat[r][c];
92
93 for (int i = 0; i < r; i++) {
94  for (int j = 0; j < c; j++)
95      if (j < 8)
96          fscanf(px, "%d,", &mat[i][j]);
97      else
98          fscanf(px, "%d", &mat[i][j]);
99 }
100 fclose(px);

Solution

  • Despite what you suggest in the text, the output from Valgrind shows that you haven't passed an argument to the program. You haven't checked that argv[1] is valid before you pass it to fopen(). Since you omitted a file name argument, argv[1] is a null pointer, and the system objects to you using a null pointer.

    ==20853==  Address 0x0 is not stack'd, malloc'd or (recently) free'd
    

    A null pointer… The errors reported all mention 'address 0x0`; your code is not doing appropriate error checking.

    You also do not check that fopen() succeeded, which will cause trouble if the given file is not openable. You also don't check that the calls to fscanf() succeed; that too can cause trouble if the content of the file is not as you expect.

    If, as you claim, you have passed the file name to the program, you would see that at the top of the output from Valgrind. You've not shown that information.