summaryrefslogtreecommitdiff
path: root/src/lib/libc/string/strncat.3
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/lib/libc/string/strncat.3
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/lib/libc/string/strncat.3')
-rw-r--r--src/lib/libc/string/strncat.385
1 files changed, 45 insertions, 40 deletions
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