Designing a better strcpy (saagarjha.com)
recommended by 1 user | 2021 | 127 points by signa11 | c and c++ | original thread |
One user recommended this thread. What they said:
howtofly Jul 31, 2021 | on: strcpy: A niche function you don't need | context

I found the article kind of misleading: using memcpy for c-string is generally a bad idea, unless string length is bound with string buffer like std::string. Otherwise it will make code review very difficult.

In our team c-string is prefixed with sz_, e.g. char sz_name[13], and we always use a safe subset(or a safe replacement) of strxxx functions with these sz_ prefixed variables. Using memxxx with sz_ variables is explicitly forbidden, since it may break the NULL-terminating contract.

The sz_ prefix convention is by no ways like the hungarian naming nonsense. Suppose that you have "char sz_name[13]" in a structure of configuration parameters, sz_ tells the guy changing the field to keep it NULL-terminated, if they don't, it's their fault. On the other side, users of this field can safely use printf("%s", sz_name) without the risk of crashing the program.

For safe replacements of strcpy, I recommend: https://news.ycombinator.com/item?id=27537900