Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I don't understand why more people don't define local helper functions. I've worked on FOSS codebases with hundreds of invocations of strncpy, each carefully followed with an additional NUL-termination check to work around strncpy's well-known problem. Why wouldn't you just write a little wrapper?


In my opinion the C library was one of C's most best innovations. But there are also some dark spots. strncpy is one of it -- and one of the things, where the C library is gravely outdated today.


Programmers just use the wrong tool for the job - arguably because strncpy is poorly named. strncpy is intended for filling zero-padded fixed-size strings, not copying normal single-zero-terminated strings.

I think the main benefit of adding functions like reallocarray is that they make the right tool for the job available by default. Plain realloc for array resizing is a timebomb in the same way as strncpy for normal strings - it will work (by accident) until it doesn't, and when it fails, it's likely to be an exploitable security hole.




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

Search: