3 ms·
(I think it would be strcat() → strlcat(), since strncat() is not really safer / newer.)
by klodolph 4y ago
(I think it would be strcat() → strlcat(), since strncat() is not really safer / newer.)
- david2ndaccount 4y agoJust always use memcpy, you should always know the size of your buffers and if you don’t you will get a buffer overflow at some point.
- klodolph 4y agoI've heard this advice before, and the problem with this advice is that people who just use memcpy in practice often write code with bugs in it. So the advice is bad advice. Good advice will result in code that has fewer bugs, or bugs with lower severity.
- kevin_thibedeau 4y agoIt can also lead to accidentally quadratic behavior which is why the modernized string ops that get so much hate these days rely on one iteration over the input.
- torstenvl 4y agoDefinitely not safer (perhaps even more dangerous due to its unexpected semantics), but I do think it's slightly newer. It doesn't appear in K&R 1st edition, released in 1978, but does appear in K&R 2nd edition from 1988. OpenBSD claims it first appeared in Version 7 Unix, which was released in 1979. https://man.openbsd.org/strncat.3 https://man.openbsd.org/strncat.3 https://en.wikipedia.org/wiki/Version_7_Unix https://en.wikipedia.org/wiki/Version_7_Unix
- cpeterso 4y agobtw, strlcpy() is now being removed from Linux kernel code. strscpy() is the latest "secure" string copy function: https://lwn.net/Articles/905777/ https://lwn.net/Articles/905777/