]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man3/strcpy.3
strcpy.3: Add some text to emphasize possibility of buffer runs with strcpy()
[thirdparty/man-pages.git] / man3 / strcpy.3
CommitLineData
fea681da
MK
1.\" Copyright (C) 1993 David Metcalfe (david@prism.demon.co.uk)
2.\"
3.\" Permission is granted to make and distribute verbatim copies of this
4.\" manual provided the copyright notice and this permission notice are
5.\" preserved on all copies.
6.\"
7.\" Permission is granted to copy and distribute modified versions of this
8.\" manual under the conditions for verbatim copying, provided that the
9.\" entire resulting derived work is distributed under the terms of a
10.\" permission notice identical to this one.
c13182ef 11.\"
fea681da
MK
12.\" Since the Linux kernel and libraries are constantly changing, this
13.\" manual page may be incorrect or out-of-date. The author(s) assume no
14.\" responsibility for errors or omissions, or for damages resulting from
15.\" the use of the information contained herein. The author(s) may not
16.\" have taken the same level of care in the production of this manual,
17.\" which is licensed free of charge, as they might when working
18.\" professionally.
c13182ef 19.\"
fea681da
MK
20.\" Formatted or processed versions of this manual, if unaccompanied by
21.\" the source, must acknowledge the copyright and authors of this work.
22.\"
23.\" References consulted:
24.\" Linux libc source code
25.\" Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991)
26.\" 386BSD man pages
27.\" Modified Sat Jul 24 18:06:49 1993 by Rik Faith (faith@cs.unc.edu)
28.\" Modified Fri Aug 25 23:17:51 1995 by Andries Brouwer (aeb@cwi.nl)
29.\" Modified Wed Dec 18 00:47:18 1996 by Andries Brouwer (aeb@cwi.nl)
616a8140
MK
30.\" 2007-06-15, Marc Boyer <marc.boyer@enseeiht.fr> + mtk
31.\" Improve discussion of strncpy().
fea681da 32.\"
cd90222a 33.TH STRCPY 3 2012-07-18 "GNU" "Linux Programmer's Manual"
fea681da
MK
34.SH NAME
35strcpy, strncpy \- copy a string
36.SH SYNOPSIS
37.nf
38.B #include <string.h>
39.sp
40.BI "char *strcpy(char *" dest ", const char *" src );
41.sp
42.BI "char *strncpy(char *" dest ", const char *" src ", size_t " n );
43.fi
44.SH DESCRIPTION
60a90ecd
MK
45The
46.BR strcpy ()
616a8140 47function copies the string pointed to by \fIsrc\fP,
f81fb444 48including the terminating null byte (\(aq\\0\(aq),
616a8140 49to the buffer pointed to by \fIdest\fP.
1c44bd5b 50The strings may not overlap, and the destination string
fea681da 51\fIdest\fP must be large enough to receive the copy.
50a24bbc
MK
52.IR "Beware of buffer overruns!"
53(See BUGS.)
fea681da 54.PP
60a90ecd
MK
55The
56.BR strncpy ()
616a8140 57function is similar, except that at most
c13182ef 58\fIn\fP bytes of \fIsrc\fP are copied.
616a8140
MK
59.BR Warning :
60If there is no null byte
61among the first \fIn\fP bytes of \fIsrc\fP,
bd74a873 62the string placed in \fIdest\fP will not be null-terminated.
fea681da 63.PP
616a8140 64If the length of
fea681da 65.I src
616a8140 66is less than
fea681da 67.IR n ,
a638bc3b 68.BR strncpy ()
cd90222a 69writes additional null bytes to
fea681da 70.I dest
cd90222a
MK
71to ensure that a total of
72.I n
73bytes are written.
616a8140
MK
74.PP
75A simple implementation of
76.BR strncpy ()
77might be:
088a639b 78.in +4n
616a8140
MK
79.nf
80
83e5bed5
MK
81char *
82strncpy(char *dest, const char *src, size_t n)
83{
616a8140
MK
84 size_t i;
85
83e5bed5 86 for (i = 0; i < n && src[i] != \(aq\\0\(aq; i++)
616a8140 87 dest[i] = src[i];
83e5bed5 88 for ( ; i < n; i++)
f81fb444 89 dest[i] = \(aq\\0\(aq;
616a8140
MK
90
91 return dest;
92}
93.fi
94.in
fea681da 95.SH "RETURN VALUE"
60a90ecd
MK
96The
97.BR strcpy ()
98and
99.BR strncpy ()
100functions return a pointer to
fea681da 101the destination string \fIdest\fP.
2b2581ee
MK
102.SH "CONFORMING TO"
103SVr4, 4.3BSD, C89, C99.
616a8140
MK
104.SH NOTES
105Some programmers consider
106.BR strncpy ()
107to be inefficient and error prone.
108If the programmer knows (i.e., includes code to test!)
109that the size of \fIdest\fP is greater than
110the length of \fIsrc\fP, then
111.BR strcpy ()
112can be used.
113
114If there is no terminating null byte in the first \fIn\fP
a00b7454 115bytes of \fIsrc\fP,
616a8140
MK
116.BR strncpy ()
117produces an unterminated string in \fIdest\fP.
118Programmers often prevent this mistake by forcing termination
119as follows:
088a639b 120.in +4n
616a8140
MK
121.nf
122
7d557e75 123strncpy(buf, str, n);
988db661 124if (n > 0)
5b8dbfd4 125 buf[n \- 1]= \(aq\\0\(aq;
616a8140
MK
126.fi
127.in
fea681da 128.SH BUGS
60a90ecd
MK
129If the destination string of a
130.BR strcpy ()
9031fc7a
JS
131is not large enough, then anything might happen.
132Overflowing fixed-length string buffers is a favorite cracker technique
133for taking complete control of the machine.
134Any time a program reads or copies data into a buffer,
135the program first needs to check that there's enough space.
136This may be unnecessary if you can show that overflow is impossible,
137but be careful: programs can get changed over time,
138in ways that may make the impossible possible.
fea681da
MK
139.SH "SEE ALSO"
140.BR bcopy (3),
141.BR memccpy (3),
142.BR memcpy (3),
143.BR memmove (3),
2cd33eb5 144.BR stpcpy (3),
f4d305c9 145.BR stpncpy (3),
c7a20d46 146.BR strdup (3),
3e5c319e 147.BR string (3),
fea681da
MK
148.BR wcscpy (3),
149.BR wcsncpy (3)