diff options
Diffstat (limited to 'src/lib/libc/string/strlcpy.3')
-rw-r--r-- | src/lib/libc/string/strlcpy.3 | 190 |
1 files changed, 190 insertions, 0 deletions
diff --git a/src/lib/libc/string/strlcpy.3 b/src/lib/libc/string/strlcpy.3 new file mode 100644 index 0000000000..b103588b96 --- /dev/null +++ b/src/lib/libc/string/strlcpy.3 | |||
@@ -0,0 +1,190 @@ | |||
1 | .\" $OpenBSD: strlcpy.3,v 1.14 2002/04/30 16:31:42 mpech Exp $ | ||
2 | .\" | ||
3 | .\" Copyright (c) 1998, 2000 Todd C. Miller <Todd.Miller@courtesan.com> | ||
4 | .\" All rights reserved. | ||
5 | .\" | ||
6 | .\" Redistribution and use in source and binary forms, with or without | ||
7 | .\" modification, are permitted provided that the following conditions | ||
8 | .\" are met: | ||
9 | .\" 1. Redistributions of source code must retain the above copyright | ||
10 | .\" notice, this list of conditions and the following disclaimer. | ||
11 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
12 | .\" notice, this list of conditions and the following disclaimer in the | ||
13 | .\" documentation and/or other materials provided with the distribution. | ||
14 | .\" 3. The name of the author may not be used to endorse or promote products | ||
15 | .\" derived from this software without specific prior written permission. | ||
16 | .\" | ||
17 | .\" THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, | ||
18 | .\" INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY | ||
19 | .\" AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL | ||
20 | .\" THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | ||
21 | .\" EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | ||
22 | .\" PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; | ||
23 | .\" OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | ||
24 | .\" WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR | ||
25 | .\" OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF | ||
26 | .\" ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
27 | .\" | ||
28 | .Dd June 22, 1998 | ||
29 | .Dt STRLCPY 3 | ||
30 | .Os | ||
31 | .Sh NAME | ||
32 | .Nm strlcpy , | ||
33 | .Nm strlcat | ||
34 | .Nd size-bounded string copying and concatenation | ||
35 | .Sh SYNOPSIS | ||
36 | .Fd #include <string.h> | ||
37 | .Ft size_t | ||
38 | .Fn strlcpy "char *dst" "const char *src" "size_t size" | ||
39 | .Ft size_t | ||
40 | .Fn strlcat "char *dst" "const char *src" "size_t size" | ||
41 | .Sh DESCRIPTION | ||
42 | The | ||
43 | .Fn strlcpy | ||
44 | and | ||
45 | .Fn strlcat | ||
46 | functions copy and concatenate strings respectively. | ||
47 | They are designed | ||
48 | to be safer, more consistent, and less error prone replacements for | ||
49 | .Xr strncpy 3 | ||
50 | and | ||
51 | .Xr strncat 3 . | ||
52 | Unlike those functions, | ||
53 | .Fn strlcpy | ||
54 | and | ||
55 | .Fn strlcat | ||
56 | take the full size of the buffer (not just the length) and guarantee to | ||
57 | NUL-terminate the result (as long as | ||
58 | .Fa size | ||
59 | is larger than 0 or, in the case of | ||
60 | .Fn strlcat , | ||
61 | as long as there is at least one byte free in | ||
62 | .Fa dst ) . | ||
63 | Note that you should include a byte for the NUL in | ||
64 | .Fa size . | ||
65 | Also note that | ||
66 | .Fn strlcpy | ||
67 | and | ||
68 | .Fn strlcat | ||
69 | only operate on true | ||
70 | .Dq C | ||
71 | strings. | ||
72 | This means that for | ||
73 | .Fn strlcpy | ||
74 | .Fa src | ||
75 | must be NUL-terminated and for | ||
76 | .Fn strlcat | ||
77 | both | ||
78 | .Fa src | ||
79 | and | ||
80 | .Fa dst | ||
81 | must be NUL-terminated. | ||
82 | .Pp | ||
83 | The | ||
84 | .Fn strlcpy | ||
85 | function copies up to | ||
86 | .Fa size | ||
87 | - 1 characters from the NUL-terminated string | ||
88 | .Fa src | ||
89 | to | ||
90 | .Fa dst , | ||
91 | NUL-terminating the result. | ||
92 | .Pp | ||
93 | The | ||
94 | .Fn strlcat | ||
95 | function appends the NUL-terminated string | ||
96 | .Fa src | ||
97 | to the end of | ||
98 | .Fa dst . | ||
99 | It will append at most | ||
100 | .Fa size | ||
101 | - strlen(dst) - 1 bytes, NUL-terminating the result. | ||
102 | .Sh RETURN VALUES | ||
103 | The | ||
104 | .Fn strlcpy | ||
105 | and | ||
106 | .Fn strlcat | ||
107 | functions return the total length of the string they tried to create. | ||
108 | For | ||
109 | .Fn strlcpy | ||
110 | that means the length of | ||
111 | .Fa src . | ||
112 | For | ||
113 | .Fn strlcat | ||
114 | that means the initial length of | ||
115 | .Fa dst | ||
116 | plus | ||
117 | the length of | ||
118 | .Fa src . | ||
119 | While this may seem somewhat confusing it was done to make | ||
120 | truncation detection simple. | ||
121 | .Pp | ||
122 | Note however, that if | ||
123 | .Fn strlcat | ||
124 | traverses | ||
125 | .Fa size | ||
126 | characters without finding a NUL, the length of the string is considered | ||
127 | to be | ||
128 | .Fa size | ||
129 | and the destination string will not be NUL-terminated (since there was | ||
130 | no space for the NUL). | ||
131 | This keeps | ||
132 | .Fn strlcat | ||
133 | from running off the end of a string. | ||
134 | In practice this should not happen (as it means that either | ||
135 | .Fa size | ||
136 | is incorrect or that | ||
137 | .Fa dst | ||
138 | is not a proper | ||
139 | .Dq C | ||
140 | string). | ||
141 | The check exists to prevent potential security problems in incorrect code. | ||
142 | .Sh EXAMPLES | ||
143 | The following code fragment illustrates the simple case: | ||
144 | .Bd -literal -offset indent | ||
145 | char *s, *p, buf[BUFSIZ]; | ||
146 | |||
147 | \&... | ||
148 | |||
149 | (void)strlcpy(buf, s, sizeof(buf)); | ||
150 | (void)strlcat(buf, p, sizeof(buf)); | ||
151 | .Ed | ||
152 | .Pp | ||
153 | To detect truncation, perhaps while building a pathname, something | ||
154 | like the following might be used: | ||
155 | .Bd -literal -offset indent | ||
156 | char *dir, *file, pname[MAXPATHLEN]; | ||
157 | |||
158 | \&... | ||
159 | |||
160 | if (strlcpy(pname, dir, sizeof(pname)) >= sizeof(pname)) | ||
161 | goto toolong; | ||
162 | if (strlcat(pname, file, sizeof(pname)) >= sizeof(pname)) | ||
163 | goto toolong; | ||
164 | .Ed | ||
165 | .Pp | ||
166 | Since we know how many characters we copied the first time, we can | ||
167 | speed things up a bit by using a copy instead of an append: | ||
168 | .Bd -literal -offset indent | ||
169 | char *dir, *file, pname[MAXPATHLEN]; | ||
170 | size_t n; | ||
171 | |||
172 | \&... | ||
173 | |||
174 | n = strlcpy(pname, dir, sizeof(pname)); | ||
175 | if (n >= sizeof(pname)) | ||
176 | goto toolong; | ||
177 | if (strlcpy(pname + n, file, sizeof(pname) - n) >= sizeof(pname) - n) | ||
178 | goto toolong; | ||
179 | .Ed | ||
180 | .Pp | ||
181 | However, one may question the validity of such optimizations, as they | ||
182 | defeat the whole purpose of | ||
183 | .Fn strlcpy | ||
184 | and | ||
185 | .Fn strlcat . | ||
186 | As a matter of fact, the first version of this manual page got it wrong. | ||
187 | .Sh SEE ALSO | ||
188 | .Xr snprintf 3 , | ||
189 | .Xr strncat 3 , | ||
190 | .Xr strncpy 3 | ||