by ahoka · 11mo ago · 0 comments
Well if you bother looking up that it's originally created for non null-terminated strings, then it kinda makes sense.
The real problem begun when static analyzers started to recommend using it instead of strcpy (the real alternative used to be snprintf, now strlcpy).
https://pubs.opengroup.org/onlinepubs/9799919799/functions/s...
(I agree with the author of the piece that strlcpy doesn't actually solve the real problem.)
I looked up the actual reason for its inception:
---
Rationale for the ANSI C Programming Language", Silicon Press 1990.
4.11.2.4 The strncpy function
strncpy was initially introduced into the C library to deal with fixed-length name fields in structures such as directory entries. Such fields are not used in the same way as strings: the trailing null is unnecessary for a maximum-length field, and setting trailing bytes for shorter names to null assures efficient field-wise comparisons. strncpy is not by origin a "bounded strcpy," and the Committee has preferred to recognize existing practice rather than alter the function to better suit it to such use.Padded and terminated strings are completely different beasts. And the text you quote tells you black on white that strncpy deals in padded strings.
“the trailing null is unnecessary for a maximum-length field”
That is a non–null terminated string.