The general technique for fixing this kind of problem is to apply valgrind
which will give you more detailed error messages pointing right at the problem.
Your specific problem is here:
pathFull = (char*)realloc(pathFull, sizeof(path));//increase memory allocation
The second argument to realloc
is the total number of bytes you want, not the number of bytes by which to increase the allocation. You need to track the current size of pathFull
and do realloc(pathFull, oldSize + sizeof(path))
or similar.
Do you really want to read the entire nmap output into memory before processing it at all, or is your actual goal to read entire lines into memory before processing them? Your code is trying to do the former, but you probably meant to do the latter, in which case getline
may be easier to use than mucking about with fgets
.
Obligatory nitpick: when coding in C do not cast the return value of malloc
nor realloc
. It is unnecessary and may prevent the compiler from noticing when you forget to #include <stdlib.h>
.