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