5 ms·
Hold on, strncat and strncpy are considered dangerous too, now? Not just the older versions without the size_t num argument?
by alxmdev 7y ago
Hold on, strncat and strncpy are considered dangerous too, now? Not just the older versions without the size_t num argument?
- unilynx 7y agoThey don't \0-terminate the target on overflow, so you still need to test for that condition. So most people will have a wrapper around those to ensure the \0 is there.
- guitarbill 7y agoI think BSD has strlcpy and strlcat for exactly this reason
- jandrese 7y agostrlcpy has the braindamage that it returns the length of the source buffer, which means it has to traverse the entire buffer to figure out the length. If you want to copy out the first line from a buffer that happens to be a 10TB mapped file, that strlcpy call will take a long time to finish. If you are using strncpy/strlcpy because you don't trust the src buffer is properly null terminated but you still want to stop the copy at the first null or when the buffer is full, well, you're out of luck because strlcpy is going to blast past the end of the source buffer regardless. I would have been much happier if it had just returned a flag indicating either successful copy (0), buffer was truncated (1), or an error occurred and errno was set (-1). Possible errors could be that the src or dest was NULL or the size was 0 (ERR_BAD_ARGUMENT).
- deathanatos 7y agostrncpy() acts as you describe, but strncat() will terminate; from its man page[1], > the resulting string in dest is always null-terminated. [1]: https://linux.die.net/man/3/strncat https://linux.die.net/man/3/strncat
- deathanatos 7y agoIn addition to what unilynx mentions about strncpy(), the size arguments are also, effectively, the remaining space in the destination buffer, not the entire space in the destination buffer. So, you have to figure that out. It isn't hard (hell, it's trivial) but I think if you're either going to be aware of the pitfalls — and then these functions are mostly not going to help you — or you're not, in which case you're just as likely to pass the wrong value for the size (dest's size/src's size) and overflow the buffer anyways. Honestly, if I had to do more than a trivial amount of string manipulation in C, I'd be wrapping that in a mini library to manage some sort of stronger string type or finding such a library (glib? ICU?) very quickly, depending on needs. std::string was one of the things in C++ that made me question why anyone was still using C, given how much less error-prone it is, comparatively. (std::string is not without problems / only as compared to char * in C.)