I wrote this simple C program and couldn't quite figure out this bizarre behavior of strcat
long sum(long col, char* path, char* path2){
printf("%s\n",path2);
strcat(path,".endlines");
printf("%s\n",path2);
return 0;
}
int main(int argc, char* argv[]) {
int n=atoi(argv[1]);
sum(n,argv[2],argv[3]);
exit(EXIT_SUCCESS);
}
strcat is applied on path, but path2 is eventually modified as well. I would very much appreciate if someone let me know what was happening :) thanks
Run
./program 3 example/mountdir/location2.csv example/rootdir/location2.csv
output:
example/rootdir/location2.csv
endlines
You are overrunning a buffer. The original argv[2] and argv[3] are very likely consecutive in memory. When you strcat onto the end of argv[2], it is writing onto the memory argv[3] points at. You need to allocate new buffers to hold the larger strings you are trying to make.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With