I am just attempting to concatenate two strings in C and I don’t know why but I’m getting segfault errors. I tracked it down using gdb to the first line in the lines of code below.
strcat(tempString, "uptime");
pFile = fopen (tempString,"r");
tempString is = “/proc/”. I just want to append the string “uptime” to the tempString and then attempt to open the file if it exists in the /proc folder. When memcpy() is called by strcat() is when the actual segfault occurs.
You have not shown some code before these lines, but it is likely tempString is assigned using
Which makes it a constant (with constant mem size allocation)
The 2nd line
strcatwill atempt to overwrite the string constant, which puts 6 bytes beyond your buffer into unknown territory (*if it succeeded).Use strcat responsibly: http://beej.us/guide/bgc/output/html/multipage/strcat.html
* as JeremyP points out, if it points to the text segment, it is read only and will cause the segfault