*** stack smashing detected ***

Hi, i'm having some trouble with this program.
Every time i run it i get the "stack smashing detected" message and it terminates.
Any ideas ?
Thanks.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <string.h>

int main (int argc, char *argv[]){
char *ip;
ip=argv[1];
char str[]="ping -c 3 ";
strcat (str,ip);//concatenates str and ip. 

 
strcat (str," > Ping");

system(str); //executes  "ping -c 3 ip > Ping"

return 0;
}
That's probably the coolest error message I've ever heard of. Only "fandango on core" would be cooler.

Yes, str is too short. A quick fix could be
1
2
3
char str[1000];
strcpy(str,"ping -c 3 ");
//(Leave the rest as it was.) 

If this is C++, though
1
2
//(I don't feel like complaining about system() right now.)
system((std::string("ping -c 3")+argv[1]+"> Ping").c_str());
Last edited on
It worked !
Thanks a lot !
Topic archived. No new replies allowed.