strdup(null) dumps core.
Tried in on ubuntu and freeBSD both.
why? Shouldn’t it return null?
char *b = NULL;
a = strdup(b);
This will dump core on strdup call.
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.
That’s quite ok.
The documentation implies that it’s argument must be string, if it’s something else, such as a null pointer, it’s anyones guess what’ll happen. In essence, you get undefined behavior when passing a NULL pointer to strdup.
It’s quite normal for functions to yield undefined behavor if you pass them something you’re not supposed to. Many standard C function such as strcpy, strlen does not accept null pointers either.