简体   繁体   中英

Error when concatenating strings in C

I am new to C and I am trying to join 2 strings in a char array, using the following code:

char url[300];
strcpy(url, "http://example.com/script.php?email=example@example.com");
char * mail = lpObj->Email;
strcat(url, mail);

I am running this on a VPS with Visual Studio, and my program always crashes and it goes to a file with asm code. lpObj->Email; is assigned and it has a well-formed string.

Does somebody knows why this happens?

There are at least two cases that will cause error, the first case is when mail is too large for the destination url . I was able to get this code to seg fault but the length required to achieve the overflow may vary:

char url[50];
strcpy(url, "http://example.com/script.php?email=example@example.com");
char  *mail = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
strcat(url, mail);

The second case is if mail is NULL :

char  *mail = NULL ; 
strcat(url, mail) ;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM