News for hackers. Stuff that matters.
Re:Everything I wish I knew when learning C by masklinn on Monday November 28, 2022 @08:54AM (#33773654)
strncpy does have its odd and rare use-case, but 100% agree that it is not at all a “fix” for strcpy, it’s not designed for that purpose, and unsuited to it, being both unsafe (does not guarantee NUL-termination) and unnecessary costly (fills the destination with NULs).

The strn* category was generally designed for fixed-size NUL-padded content (though not all of them because why be coherent?), the entire item is incorrect, and really makes the entire thing suspicious.

Re:Everything I wish I knew when learning C by AstralStorm on Monday November 28, 2022 @09:23AM (#33773930)
Then there are strn*_s since C11 (and available before that on many platforms) which do exactly what you want.
Re:Everything I wish I knew when learning C by masklinn on Monday November 28, 2022 @09:40AM (#33774128)
Lol no. These are the Annex K stuff which Microsoft got into the standard, which got standardised with a different behaviour than Windows’ (so even on windows following the spec doesn’t work) and which no one else wants to implement at all.

And they don’t actually “do exactly what you want”, see for instance N1967 (“field experience with annex k”) which is less than glowing.