Why does this return no warnings? What is supposed to be wrong with the code?
char *str = malloc(strlen("hello" + 1));
strcpy(str, "hello");
Thanks!
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
This
is nearly equivalent to:
so
temp + 1is pointer math (it returns a pointer toello, andstrcpydoesn’t check if enough memory is present at destination (“standard” memory corruption caused by faulty code in C)The end result is that
strlenreturns 4,strcpyuses 6 bytes of memory and a random piece of heap is trashed.