@@ -106,23 +106,29 @@ NUL or newline terminated.
106106
107107strcpy()
108108--------
109- strcpy() performs no bounds checking on the destination
110- buffer. This could result in linear overflows beyond the
111- end of the buffer, leading to all kinds of misbehaviors. While
112- `CONFIG_FORTIFY_SOURCE=y ` and various compiler flags help reduce the
113- risk of using this function, there is no good reason to add new uses of
114- this function. The safe replacement is strscpy().
109+ strcpy() performs no bounds checking on the destination buffer. This
110+ could result in linear overflows beyond the end of the buffer, leading to
111+ all kinds of misbehaviors. While `CONFIG_FORTIFY_SOURCE=y ` and various
112+ compiler flags help reduce the risk of using this function, there is
113+ no good reason to add new uses of this function. The safe replacement
114+ is strscpy(), though care must be given to any cases where the return
115+ value of strcpy() was used, since strscpy() does not return a pointer to
116+ the destination, but rather a count of non-NUL bytes copied (or negative
117+ errno when it truncates).
115118
116119strncpy() on NUL-terminated strings
117120-----------------------------------
118- Use of strncpy() does not guarantee that the destination buffer
119- will be NUL terminated. This can lead to various linear read overflows
120- and other misbehavior due to the missing termination. It also NUL-pads the
121- destination buffer if the source contents are shorter than the destination
122- buffer size, which may be a needless performance penalty for callers using
123- only NUL-terminated strings. The safe replacement is strscpy().
124- (Users of strscpy() still needing NUL-padding should instead
125- use strscpy_pad().)
121+ Use of strncpy() does not guarantee that the destination buffer will
122+ be NUL terminated. This can lead to various linear read overflows and
123+ other misbehavior due to the missing termination. It also NUL-pads
124+ the destination buffer if the source contents are shorter than the
125+ destination buffer size, which may be a needless performance penalty
126+ for callers using only NUL-terminated strings. The safe replacement is
127+ strscpy(), though care must be given to any cases where the return value
128+ of strncpy() was used, since strscpy() does not return a pointer to the
129+ destination, but rather a count of non-NUL bytes copied (or negative
130+ errno when it truncates). Any cases still needing NUL-padding should
131+ instead use strscpy_pad().
126132
127133If a caller is using non-NUL-terminated strings, strncpy() can
128134still be used, but destinations should be marked with the `__nonstring
@@ -131,10 +137,12 @@ attribute to avoid future compiler warnings.
131137
132138strlcpy()
133139---------
134- strlcpy() reads the entire source buffer first, possibly exceeding
135- the given limit of bytes to copy. This is inefficient and can lead to
136- linear read overflows if a source string is not NUL-terminated. The
137- safe replacement is strscpy().
140+ strlcpy() reads the entire source buffer first (since the return value
141+ is meant to match that of strlen()). This read may exceed the destination
142+ size limit. This is both inefficient and can lead to linear read overflows
143+ if a source string is not NUL-terminated. The safe replacement is strscpy(),
144+ though care must be given to any cases where the return value of strlcpy()
145+ is used, since strscpy() will return negative errno values when it truncates.
138146
139147%p format specifier
140148-------------------
0 commit comments