Hacker News new | past | comments | ask | show | jobs | submit login

I hope you meant strlen in place of sizeof



Most people use sizeof when they are copying between static buffers on the stack. Strlen would be just as bad of an idea in this example because they're using the src buffer as the limit on the copy rather than the dst buffer which is still vulnerable to overflow. Should be:

strncpy(dst, src, sizeof(dst)-1);

Edit: bad code


or sizeof(dst), and not forgetting dst[sizeof(dst)-1]='\0';


That's why strlcpy is safer than strncpy, it makes sure that the destination string will always be zero-terminated.




Consider applying for YC's Summer 2025 batch! Applications are open till May 13

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: