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