xref: /netbsd-src/lib/libc/string/strcpy.3 (revision 13b8c448a375a27446e2324a9d27b5ef0b51783a)
1.\" Copyright (c) 1990, 1991, 1993
2.\"	The Regents of the University of California.  All rights reserved.
3.\"
4.\" This code is derived from software contributed to Berkeley by
5.\" Chris Torek and the American National Standards Committee X3,
6.\" on Information Processing Systems.
7.\"
8.\" Redistribution and use in source and binary forms, with or without
9.\" modification, are permitted provided that the following conditions
10.\" are met:
11.\" 1. Redistributions of source code must retain the above copyright
12.\"    notice, this list of conditions and the following disclaimer.
13.\" 2. Redistributions in binary form must reproduce the above copyright
14.\"    notice, this list of conditions and the following disclaimer in the
15.\"    documentation and/or other materials provided with the distribution.
16.\" 3. Neither the name of the University nor the names of its contributors
17.\"    may be used to endorse or promote products derived from this software
18.\"    without specific prior written permission.
19.\"
20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE.
31.\"
32.\"     from: @(#)strcpy.3	8.1 (Berkeley) 6/4/93
33.\"	$NetBSD: strcpy.3,v 1.27 2023/08/11 21:17:16 riastradh Exp $
34.\"
35.Dd August 11, 2023
36.Dt STRCPY 3
37.Os
38.Sh NAME
39.Nm stpcpy ,
40.Nm strcpy
41.Nd copy strings
42.Sh LIBRARY
43.Lb libc
44.Sh SYNOPSIS
45.In string.h
46.Ft char *
47.Fn stpcpy "char * restrict dst" "const char * restrict src"
48.Ft char *
49.Fn strcpy "char * restrict dst" "const char * restrict src"
50.Sh DESCRIPTION
51The
52.Fn stpcpy
53and
54.Fn strcpy
55functions
56copy the string
57.Fa src
58to
59.Fa dst ,
60including the terminating
61.Tn NUL
62byte.
63.Pp
64The strings
65.Fa src
66and
67.Fa dst
68may not overlap.
69The string
70.Fa src
71must be terminated by a
72.Tn NUL
73byte.
74The memory for
75.Fa dst
76must have space for
77.Fn strlen src Li "+ 1"
78bytes.
79.Sh RETURN VALUES
80The
81.Fn strcpy
82function returns
83.Fa dst .
84.Pp
85The
86.Fn stpcpy
87function returns a pointer to the terminating
88.Tn NUL
89byte of
90.Fa dst .
91.Sh SEE ALSO
92.Xr bcopy 3 ,
93.Xr memccpy 3 ,
94.Xr memcpy 3 ,
95.Xr memmove 3 ,
96.Xr strlcpy 3 ,
97.Xr strncpy 3 ,
98.Xr wcscpy 3
99.Sh STANDARDS
100The
101.Fn strcpy
102function conforms to
103.St -isoC-99 .
104.Pp
105The
106.Fn stpcpy
107function conforms to
108.St -p1003.1-2008 .
109.Sh HISTORY
110The
111.Fn stpcpy
112function first appeared in
113.Nx 6.0 .
114.Sh SECURITY CONSIDERATIONS
115The
116.Fn strcpy
117and
118.Fn stpcpy
119functions copy until a
120.Tn NUL
121terminator without any bounds checks on the size of the input or output
122buffers.
123If the input buffer is missing a
124.Tn NUL
125terminator, or the input string is longer than the output buffer, this
126can lead to crashes or security vulnerabilities from buffer overruns,
127including disclosure of secrets in memory and arbitrary code
128execution.
129.Pp
130The
131.Xr strlcpy 3
132function is a safer replacement for
133.Fn strcpy
134which allows the caller to specify the space allocated for
135.Fa dst .
136.Xr strlcpy 3 ,
137or
138.Xr snprintf 3
139with a format string of
140.Li \*q%s\*q ,
141should be used instead of
142.Fn strcpy
143and
144.Fn stpcpy
145wherever possible to avoid buffer overruns in
146.Fa dst .
147.Po
148However, they still require
149.Fa src
150to be
151.Tn NUL Ns -terminated .
152.Pc
153