summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorderaadt <>2012-04-02 17:33:11 +0000
committerderaadt <>2012-04-02 17:33:11 +0000
commit0be23ed0f4fd50120e36d2885c172686ec809f3f (patch)
tree243ddb013ff4c755b2049c9808a9646db47be462
parentb2d1fd8918f095aaf70b8d435a3b7ee0ec0b5ef3 (diff)
downloadopenbsd-0be23ed0f4fd50120e36d2885c172686ec809f3f.tar.gz
openbsd-0be23ed0f4fd50120e36d2885c172686ec809f3f.tar.bz2
openbsd-0be23ed0f4fd50120e36d2885c172686ec809f3f.zip
simplify the strlcpy/strlcat manual page substantially. do less
explaining of "what a C string is", and make it more clear that these functiosn BEHAVE EXACTLY LIKE snprintf with "%s"! (anyone who wants to write a 'strlcpy considered harmful' paper should probably write a 'strlcpy and snprintf considered harmful' paper instead). note to those from other projects reading this commit message: It would be very good if this new manual was picked up in your project. ok jmc millert krw
-rw-r--r--src/lib/libc/string/strlcpy.3128
1 files changed, 58 insertions, 70 deletions
diff --git a/src/lib/libc/string/strlcpy.3 b/src/lib/libc/string/strlcpy.3
index ead54ec438..e04284d5c3 100644
--- a/src/lib/libc/string/strlcpy.3
+++ b/src/lib/libc/string/strlcpy.3
@@ -1,4 +1,4 @@
1.\" $OpenBSD: strlcpy.3,v 1.20 2011/07/25 00:38:53 schwarze Exp $ 1.\" $OpenBSD: strlcpy.3,v 1.21 2012/04/02 17:33:11 deraadt Exp $
2.\" 2.\"
3.\" Copyright (c) 1998, 2000 Todd C. Miller <Todd.Miller@courtesan.com> 3.\" Copyright (c) 1998, 2000 Todd C. Miller <Todd.Miller@courtesan.com>
4.\" 4.\"
@@ -14,7 +14,7 @@
14.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 14.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16.\" 16.\"
17.Dd $Mdocdate: July 25 2011 $ 17.Dd $Mdocdate: April 2 2012 $
18.Dt STRLCPY 3 18.Dt STRLCPY 3
19.Os 19.Os
20.Sh NAME 20.Sh NAME
@@ -24,72 +24,79 @@
24.Sh SYNOPSIS 24.Sh SYNOPSIS
25.Fd #include <string.h> 25.Fd #include <string.h>
26.Ft size_t 26.Ft size_t
27.Fn strlcpy "char *dst" "const char *src" "size_t size" 27.Fn strlcpy "char *dst" "const char *src" "size_t dstsize"
28.Ft size_t 28.Ft size_t
29.Fn strlcat "char *dst" "const char *src" "size_t size" 29.Fn strlcat "char *dst" "const char *src" "size_t dstsize"
30.Sh DESCRIPTION 30.Sh DESCRIPTION
31The 31The
32.Fn strlcpy 32.Fn strlcpy
33and 33and
34.Fn strlcat 34.Fn strlcat
35functions copy and concatenate strings respectively. 35functions copy and concatenate strings with the
36They are designed 36same input parameters and output result as
37to be safer, more consistent, and less error prone replacements for 37.Xr snprintf 3 .
38They are designed to be safer, more consistent, and less error
39prone replacements for the easily misused functions
38.Xr strncpy 3 40.Xr strncpy 3
39and 41and
40.Xr strncat 3 . 42.Xr strncat 3 .
41Unlike those functions, 43.Pp
42.Fn strlcpy
43and
44.Fn strlcat
45take the full size of the buffer (not just the length) and guarantee to
46NUL-terminate the result (as long as
47.Fa size
48is larger than 0 or, in the case of
49.Fn strlcat ,
50as long as there is at least one byte free in
51.Fa dst ) .
52Note that a byte for the NUL should be included in
53.Fa size .
54Also note that
55.Fn strlcpy 44.Fn strlcpy
56and 45and
57.Fn strlcat 46.Fn strlcat
58only operate on true 47take the full size of the destination buffer and guarantee
59.Dq C 48NUL-termination if there is room.
60strings. 49Note that room for the NUL should be included in
61This means that for 50.Fa dstsize .
62.Fn strlcpy
63.Fa src
64must be NUL-terminated and for
65.Fn strlcat
66both
67.Fa src
68and
69.Fa dst
70must be NUL-terminated.
71.Pp 51.Pp
72The
73.Fn strlcpy 52.Fn strlcpy
74function copies up to 53copies up to
75.Fa size 54.Fa dstsize
76- 1 characters from the NUL-terminated string 55\- 1 characters from the string
77.Fa src 56.Fa src
78to 57to
79.Fa dst , 58.Fa dst ,
80NUL-terminating the result. 59NUL-terminating the result if
60.Fa dstsize
61is not 0.
81.Pp 62.Pp
82The
83.Fn strlcat 63.Fn strlcat
84function appends the NUL-terminated string 64appends string
85.Fa src 65.Fa src
86to the end of 66to the end of
87.Fa dst . 67.Fa dst .
88It will append at most 68It will append at most
89.Fa size 69.Fa dstsize
90- strlen(dst) - 1 bytes, NUL-terminating the result. 70\- strlen(dst) \- 1 characters.
71It will then NUL-terminate, unless
72.Fa dstsize
73is 0 or the original
74.Fa dst
75string was longer than
76.Fa dstsize
77(in practice this should not happen
78as it means that either
79.Fa dstsize
80is incorrect or that
81.Fa dst
82is not a proper string).
91.Sh RETURN VALUES 83.Sh RETURN VALUES
92The 84Besides quibbles over the return type
85.Pf ( Va size_t
86versus
87.Va int )
88and signal handler safety
89.Xr ( snprintf 3
90is not entirely safe on some systems), the
91following two are equivalent:
92.Bd -literal -offset indent
93n = strlcpy(dst, src, len);
94n = snprintf(dst, len, "%s", src);
95.Ed
96.Pp
97Like
98.Xr snprintf 3 ,
99the
93.Fn strlcpy 100.Fn strlcpy
94and 101and
95.Fn strlcat 102.Fn strlcat
@@ -105,29 +112,12 @@ that means the initial length of
105plus 112plus
106the length of 113the length of
107.Fa src . 114.Fa src .
108While this may seem somewhat confusing, it was done to make
109truncation detection simple.
110.Pp 115.Pp
111Note, however, that if 116If the return value is
112.Fn strlcat 117.Cm >=
113traverses 118.Va dstsize ,
114.Fa size 119the output string has been truncated.
115characters without finding a NUL, the length of the string is considered 120It is the caller's responsibility to handle this.
116to be
117.Fa size
118and the destination string will not be NUL-terminated (since there was
119no space for the NUL).
120This keeps
121.Fn strlcat
122from running off the end of a string.
123In practice this should not happen (as it means that either
124.Fa size
125is incorrect or that
126.Fa dst
127is not a proper
128.Dq C
129string).
130The check exists to prevent potential security problems in incorrect code.
131.Sh EXAMPLES 121.Sh EXAMPLES
132The following code fragment illustrates the simple case: 122The following code fragment illustrates the simple case:
133.Bd -literal -offset indent 123.Bd -literal -offset indent
@@ -179,16 +169,14 @@ As a matter of fact, the first version of this manual page got it wrong.
179.Xr strncpy 3 , 169.Xr strncpy 3 ,
180.Xr wcslcpy 3 170.Xr wcslcpy 3
181.Sh HISTORY 171.Sh HISTORY
182The
183.Fn strlcpy 172.Fn strlcpy
184and 173and
185.Fn strlcat 174.Fn strlcat
186functions first appeared in 175first appeared in
187.Ox 2.4 . 176.Ox 2.4 .
188.Sh AUTHORS 177.Sh AUTHORS
189The
190.Fn strlcpy 178.Fn strlcpy
191and 179and
192.Fn strlcat 180.Fn strlcat
193functions were created by 181were created by
194.An Todd C. Miller Aq Todd.Miller@courtesan.com . 182.An Todd C. Miller Aq Todd.Miller@courtesan.com .