summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorderaadt <>2014-04-19 11:30:40 +0000
committerderaadt <>2014-04-19 11:30:40 +0000
commit597610ccf04bbca4e88fac7988877d0a06e02211 (patch)
treefcfc398023e666979cbe5ad67cb1e398e980f095 /src
parent442335bf77f2f6653794378cc53adb59bba1ed12 (diff)
downloadopenbsd-597610ccf04bbca4e88fac7988877d0a06e02211.tar.gz
openbsd-597610ccf04bbca4e88fac7988877d0a06e02211.tar.bz2
openbsd-597610ccf04bbca4e88fac7988877d0a06e02211.zip
Use somewhat harsher language and better examples; demonstrate that
non-dangerous use functions is difficult. ok guenther
Diffstat (limited to 'src')
-rw-r--r--src/lib/libc/string/strcat.321
-rw-r--r--src/lib/libc/string/strcpy.334
-rw-r--r--src/lib/libc/string/strncat.385
-rw-r--r--src/lib/libc/string/strncpy.357
4 files changed, 88 insertions, 109 deletions
diff --git a/src/lib/libc/string/strcat.3 b/src/lib/libc/string/strcat.3
index fba992edd9..7368d08e4b 100644
--- a/src/lib/libc/string/strcat.3
+++ b/src/lib/libc/string/strcat.3
@@ -1,4 +1,4 @@
1.\" $OpenBSD: strcat.3,v 1.16 2013/12/19 20:52:37 millert Exp $ 1.\" $OpenBSD: strcat.3,v 1.17 2014/04/19 11:30:40 deraadt Exp $
2.\" 2.\"
3.\" Copyright (c) 1990, 1991 The Regents of the University of California. 3.\" Copyright (c) 1990, 1991 The Regents of the University of California.
4.\" All rights reserved. 4.\" All rights reserved.
@@ -31,12 +31,12 @@
31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32.\" SUCH DAMAGE. 32.\" SUCH DAMAGE.
33.\" 33.\"
34.Dd $Mdocdate: December 19 2013 $ 34.Dd $Mdocdate: April 19 2014 $
35.Dt STRCAT 3 35.Dt STRCAT 3
36.Os 36.Os
37.Sh NAME 37.Sh NAME
38.Nm strcat 38.Nm strcat
39.Nd concatenate two strings 39.Nd concatenate two strings without bounds checking
40.Sh SYNOPSIS 40.Sh SYNOPSIS
41.In string.h 41.In string.h
42.Ft char * 42.Ft char *
@@ -50,22 +50,19 @@ to the end of the NUL-terminated string
50.Fa s , 50.Fa s ,
51then adds a terminating 51then adds a terminating
52.Ql \e0 . 52.Ql \e0 .
53The string 53.Pp
54.Fa s 54No bounds checking is performed.
55must have sufficient space to hold the result. 55If the buffer
56.Fa dst
57is not large enough to hold the result,
58subsequent memory will be damaged.
56.Sh RETURN VALUES 59.Sh RETURN VALUES
57The 60The
58.Fn strcat 61.Fn strcat
59function return the pointer 62function return the pointer
60.Fa s . 63.Fa s .
61.Sh SEE ALSO 64.Sh SEE ALSO
62.Xr bcopy 3 ,
63.Xr memccpy 3 ,
64.Xr memcpy 3 ,
65.Xr memmove 3 ,
66.Xr strcpy 3 ,
67.Xr strlcpy 3 , 65.Xr strlcpy 3 ,
68.Xr strncat 3 ,
69.Xr wcscat 3 , 66.Xr wcscat 3 ,
70.Xr wcslcpy 3 67.Xr wcslcpy 3
71.Sh STANDARDS 68.Sh STANDARDS
diff --git a/src/lib/libc/string/strcpy.3 b/src/lib/libc/string/strcpy.3
index 849184d1f5..7174f7c963 100644
--- a/src/lib/libc/string/strcpy.3
+++ b/src/lib/libc/string/strcpy.3
@@ -1,4 +1,4 @@
1.\" $OpenBSD: strcpy.3,v 1.20 2013/12/19 20:52:37 millert Exp $ 1.\" $OpenBSD: strcpy.3,v 1.21 2014/04/19 11:30:40 deraadt Exp $
2.\" 2.\"
3.\" Copyright (c) 1990, 1991 The Regents of the University of California. 3.\" Copyright (c) 1990, 1991 The Regents of the University of California.
4.\" All rights reserved. 4.\" All rights reserved.
@@ -31,12 +31,12 @@
31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32.\" SUCH DAMAGE. 32.\" SUCH DAMAGE.
33.\" 33.\"
34.Dd $Mdocdate: December 19 2013 $ 34.Dd $Mdocdate: April 19 2014 $
35.Dt STRCPY 3 35.Dt STRCPY 3
36.Os 36.Os
37.Sh NAME 37.Sh NAME
38.Nm strcpy 38.Nm strcpy
39.Nd copy a string 39.Nd copy a string without bounds checking
40.Sh SYNOPSIS 40.Sh SYNOPSIS
41.In string.h 41.In string.h
42.Ft char * 42.Ft char *
@@ -46,43 +46,35 @@ The
46.Fn strcpy 46.Fn strcpy
47function copies the string 47function copies the string
48.Fa src 48.Fa src
49to
50.Fa dst
51(including the terminating 49(including the terminating
52.Ql \e0 50.Ql \e0
53character). 51character) to the buffer
54The string 52.Fa dst .
53.Pp
54No bounds checking is performed.
55If the buffer
55.Fa dst 56.Fa dst
56must be at least as large as 57is not large enough to hold the result,
57.Fa src 58subsequent memory will be damaged.
58to hold the result.
59.Pp 59.Pp
60If the 60If the
61.Fa src 61.Fa src
62and 62string is inside the
63.Fa dst 63.Fa dst
64strings overlap, the behavior is undefined. 64buffer, the behavior is undefined.
65.Sh RETURN VALUES 65.Sh RETURN VALUES
66The 66The
67.Fn strcpy 67.Fn strcpy
68function returns 68function returns
69.Fa dst . 69.Fa dst .
70.Sh SEE ALSO 70.Sh SEE ALSO
71.Xr bcopy 3 ,
72.Xr memccpy 3 ,
73.Xr memcpy 3 ,
74.Xr memmove 3 ,
75.Xr strcat 3 ,
76.Xr strlcpy 3 , 71.Xr strlcpy 3 ,
77.Xr strncpy 3 ,
78.Xr wcscpy 3 , 72.Xr wcscpy 3 ,
79.Xr wcslcpy 3 73.Xr wcslcpy 3
80.Sh STANDARDS 74.Sh STANDARDS
81The 75The
82.Fn strcpy 76.Fn strcpy
83and 77function conforms to
84.Fn strncpy
85functions conform to
86.St -ansiC . 78.St -ansiC .
87.Sh HISTORY 79.Sh HISTORY
88The 80The
diff --git a/src/lib/libc/string/strncat.3 b/src/lib/libc/string/strncat.3
index bd15ef10fa..c0a0da57c7 100644
--- a/src/lib/libc/string/strncat.3
+++ b/src/lib/libc/string/strncat.3
@@ -1,4 +1,4 @@
1.\" $OpenBSD: strncat.3,v 1.2 2013/12/19 22:00:58 jmc Exp $ 1.\" $OpenBSD: strncat.3,v 1.3 2014/04/19 11:30:40 deraadt Exp $
2.\" 2.\"
3.\" Copyright (c) 1990, 1991 The Regents of the University of California. 3.\" Copyright (c) 1990, 1991 The Regents of the University of California.
4.\" All rights reserved. 4.\" All rights reserved.
@@ -31,7 +31,7 @@
31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32.\" SUCH DAMAGE. 32.\" SUCH DAMAGE.
33.\" 33.\"
34.Dd $Mdocdate: December 19 2013 $ 34.Dd $Mdocdate: April 19 2014 $
35.Dt STRNCAT 3 35.Dt STRNCAT 3
36.Os 36.Os
37.Sh NAME 37.Sh NAME
@@ -40,86 +40,91 @@
40.Sh SYNOPSIS 40.Sh SYNOPSIS
41.In string.h 41.In string.h
42.Ft char * 42.Ft char *
43.Fn strncat "char *s" "const char *append" "size_t count" 43.Fn strncat "char *dst" "const char *append" "size_t count"
44.Sh DESCRIPTION 44.Sh DESCRIPTION
45The 45The
46.Fn strncat 46.Fn strncat
47function appends not more than 47function appends not more than
48.Fa count 48.Fa count
49characters of the NUL-terminated string 49characters of the string
50.Fa append 50.Fa append
51to the end of the NUL-terminated string 51to the end of the string found in the buffer
52.Fa s . 52.Fa dst .
53Space for the terminating 53Space for the terminating
54.Ql \e0 54.Ql \e0
55should not be included in 55should not be included in
56.Fa count . 56.Fa count .
57The string 57.Pp
58.Fa s 58Bounds checking must be performed manually with great care.
59must have sufficient space to hold the result. 59If the buffer
60.Fa dst
61is not large enough to hold the result,
62subsequent memory will be damaged.
60.Sh RETURN VALUES 63.Sh RETURN VALUES
61The 64The
62.Fn strncat 65.Fn strncat
63function returns the pointer 66function returns the pointer
64.Fa s . 67.Fa dst .
65.Sh EXAMPLES 68.Sh EXAMPLES
66The following appends
67.Dq Li abc
68to
69.Va chararray :
70.Bd -literal -offset indent
71char *letters = "abcdefghi";
72
73(void)strncat(chararray, letters, 3);
74.Ed
75.Pp
76The following example shows how to use 69The following example shows how to use
77.Fn strncat 70.Fn strncat
78safely in conjunction with 71in conjunction with
79.Xr strncpy 3 . 72.Xr strncpy 3 :
80.Bd -literal -offset indent 73.Bd -literal -offset indent
81char buf[BUFSIZ]; 74char buf[BUFSIZ];
82char *input, *suffix; 75char *base, *suffix;
83 76
84(void)strncpy(buf, input, sizeof(buf) - 1); 77(void)strncpy(buf, base, sizeof(buf) - 1);
85buf[sizeof(buf) - 1] = '\e0'; 78buf[sizeof(buf) - 1] = '\e0';
86(void)strncat(buf, suffix, sizeof(buf) - 1 - strlen(buf)); 79(void)strncat(buf, suffix, sizeof(buf) - 1 - strlen(buf));
87.Ed 80.Ed
88.Pp 81.Pp
89The above will copy as many characters from 82The above will copy as many characters from
90.Va input 83.Va base
91to 84to
92.Va buf 85.Va buf
93as will fit. 86as will fit.
94It then appends as many characters from 87It then appends as many characters from
95.Va suffix 88.Va suffix
96as will fit (or none 89as will fit.
97if there is no space). 90If either
98For operations like this, the 91.Va base
92or
93.Va suffix
94are too large, truncation will occur without detection.
95.Pp
96The above example shows dangerous coding patterns, including an
97inability to detect truncation.
98.Fn strncat
99and
100.Fn strncpy
101are dangerously easy to misuse.
102The
99.Xr strlcpy 3 103.Xr strlcpy 3
100and 104and
101.Xr strlcat 3 105.Xr strlcat 3
102functions are a better choice, as shown below. 106functions are safer for this kind of operation:
107.Bd -literal -offset indent
108if (strlcpy(buf, base, sizeof(buf)) >= sizeof(buf) ||
109 strlcat(buf, suffix, sizeof(buf)) >= sizeof(buf))
110 goto toolong;
111
112.Ed
113or for greatest portability,
103.Bd -literal -offset indent 114.Bd -literal -offset indent
104(void)strlcpy(buf, input, sizeof(buf)); 115if (snprintf(buf, sizeof(buf), "%s%s",
105(void)strlcat(buf, suffix, sizeof(buf)); 116 base, suffix) >= sizeof(buf))
117 goto toolong;
106.Ed 118.Ed
119
107.Sh SEE ALSO 120.Sh SEE ALSO
108.Xr bcopy 3 ,
109.Xr memccpy 3 ,
110.Xr memcpy 3 ,
111.Xr memmove 3 ,
112.Xr strcat 3 ,
113.Xr strcpy 3 ,
114.Xr strlcpy 3 , 121.Xr strlcpy 3 ,
115.Xr wcscat 3 , 122.Xr wcscat 3 ,
116.Xr wcslcpy 3 123.Xr wcslcpy 3
117.Sh STANDARDS 124.Sh STANDARDS
118The 125The
119.Fn strcat
120and
121.Fn strncat 126.Fn strncat
122functions conform to 127function conform to
123.St -ansiC . 128.St -ansiC .
124.Sh HISTORY 129.Sh HISTORY
125The 130The
diff --git a/src/lib/libc/string/strncpy.3 b/src/lib/libc/string/strncpy.3
index dd8ddb86fc..3a68a0bd5b 100644
--- a/src/lib/libc/string/strncpy.3
+++ b/src/lib/libc/string/strncpy.3
@@ -1,4 +1,4 @@
1.\" $OpenBSD: strncpy.3,v 1.1 2013/12/19 20:52:37 millert Exp $ 1.\" $OpenBSD: strncpy.3,v 1.2 2014/04/19 11:30:40 deraadt Exp $
2.\" 2.\"
3.\" Copyright (c) 1990, 1991 The Regents of the University of California. 3.\" Copyright (c) 1990, 1991 The Regents of the University of California.
4.\" All rights reserved. 4.\" All rights reserved.
@@ -31,7 +31,7 @@
31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32.\" SUCH DAMAGE. 32.\" SUCH DAMAGE.
33.\" 33.\"
34.Dd $Mdocdate: December 19 2013 $ 34.Dd $Mdocdate: April 19 2014 $
35.Dt STRNCPY 3 35.Dt STRNCPY 3
36.Os 36.Os
37.Sh NAME 37.Sh NAME
@@ -48,17 +48,16 @@ function copies not more than
48.Fa len 48.Fa len
49characters from the string 49characters from the string
50.Fa src 50.Fa src
51to 51to the buffer
52.Fa dst . 52.Fa dst .
53If 53If
54.Fa src 54.Fa src
55is less than 55is less than
56.Fa len 56.Fa len
57characters long, 57characters long,
58it appends 58it fills the remaining buffer with
59.Ql \e0 59.Ql \e0
60characters for the rest of 60characters.
61.Fa len .
62If the length of 61If the length of
63.Fa src 62.Fa src
64is greater than or equal to 63is greater than or equal to
@@ -68,6 +67,11 @@ will
68.Em not 67.Em not
69be NUL-terminated. 68be NUL-terminated.
70.Pp 69.Pp
70.Fn strncpy
71.Em only
72NUL terminates the destination string when the length of the source
73string is less than the length parameter.
74.Pp
71If the 75If the
72.Fa src 76.Fa src
73and 77and
@@ -90,31 +94,17 @@ to
90The following sets 94The following sets
91.Va chararray 95.Va chararray
92to 96to
93.Dq abcdef 97.Dq abcdef ,
94and does 98without a NUL-terminator:
95.Em not
96NUL terminate
97.Va chararray
98because the length of the source string is greater than or equal to the
99length parameter.
100.Fn strncpy
101.Em only
102NUL terminates the destination string when the length of the source
103string is less than the length parameter.
104.Bd -literal -offset indent 99.Bd -literal -offset indent
105(void)strncpy(chararray, "abcdefgh", 6); 100(void)strncpy(chararray, "abcdefgh", 6);
106.Ed 101.Ed
107.Pp 102.Pp
108The following copies as many characters from 103The following sequence copies as many characters from
109.Va input 104.Va input
110to 105to
111.Va buf 106.Va buf
112as will fit and NUL terminates the result. 107as will fit, and then NUL terminates the result by hand:
113Because
114.Fn strncpy
115does
116.Em not
117guarantee to NUL terminate the string itself, it must be done by hand.
118.Bd -literal -offset indent 108.Bd -literal -offset indent
119char buf[BUFSIZ]; 109char buf[BUFSIZ];
120 110
@@ -122,23 +112,18 @@ char buf[BUFSIZ];
122buf[sizeof(buf) - 1] = '\e0'; 112buf[sizeof(buf) - 1] = '\e0';
123.Ed 113.Ed
124.Pp 114.Pp
125Note that 115By now it is clear that
126.Xr strlcpy 3 116.Nm strncpy
127is a better choice for this kind of operation. 117is dangerously easy to misuse.
128The equivalent using 118The
129.Xr strlcpy 3 119.Xr strlcpy 3
130is simply: 120function is safer for this kind of operation:
131.Bd -literal -offset indent 121.Bd -literal -offset indent
132(void)strlcpy(buf, input, sizeof(buf)); 122if (strlcpy(buf, input, sizeof(buf)) >= sizeof(buf))
123 goto toolong;
133.Ed 124.Ed
134.Sh SEE ALSO 125.Sh SEE ALSO
135.Xr bcopy 3 ,
136.Xr memccpy 3 ,
137.Xr memcpy 3 ,
138.Xr memmove 3 ,
139.Xr strcat 3 ,
140.Xr strlcpy 3 , 126.Xr strlcpy 3 ,
141.Xr strncat 3 ,
142.Xr wcscpy 3 , 127.Xr wcscpy 3 ,
143.Xr wcslcpy 3 128.Xr wcslcpy 3
144.Sh STANDARDS 129.Sh STANDARDS