diff options
| author | deraadt <> | 2014-04-19 11:30:40 +0000 |
|---|---|---|
| committer | deraadt <> | 2014-04-19 11:30:40 +0000 |
| commit | ac3cb5c172d4edffffbf917c401f9b4bfecea55f (patch) | |
| tree | fcfc398023e666979cbe5ad67cb1e398e980f095 /src/lib/libc/string/strncat.3 | |
| parent | d43e1f468c006866ad7261a6e699a0fe621ad9ba (diff) | |
| download | openbsd-ac3cb5c172d4edffffbf917c401f9b4bfecea55f.tar.gz openbsd-ac3cb5c172d4edffffbf917c401f9b4bfecea55f.tar.bz2 openbsd-ac3cb5c172d4edffffbf917c401f9b4bfecea55f.zip | |
Use somewhat harsher language and better examples; demonstrate that
non-dangerous use functions is difficult.
ok guenther
Diffstat (limited to '')
| -rw-r--r-- | src/lib/libc/string/strncat.3 | 85 |
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 |
| 45 | The | 45 | The |
| 46 | .Fn strncat | 46 | .Fn strncat |
| 47 | function appends not more than | 47 | function appends not more than |
| 48 | .Fa count | 48 | .Fa count |
| 49 | characters of the NUL-terminated string | 49 | characters of the string |
| 50 | .Fa append | 50 | .Fa append |
| 51 | to the end of the NUL-terminated string | 51 | to the end of the string found in the buffer |
| 52 | .Fa s . | 52 | .Fa dst . |
| 53 | Space for the terminating | 53 | Space for the terminating |
| 54 | .Ql \e0 | 54 | .Ql \e0 |
| 55 | should not be included in | 55 | should not be included in |
| 56 | .Fa count . | 56 | .Fa count . |
| 57 | The string | 57 | .Pp |
| 58 | .Fa s | 58 | Bounds checking must be performed manually with great care. |
| 59 | must have sufficient space to hold the result. | 59 | If the buffer |
| 60 | .Fa dst | ||
| 61 | is not large enough to hold the result, | ||
| 62 | subsequent memory will be damaged. | ||
| 60 | .Sh RETURN VALUES | 63 | .Sh RETURN VALUES |
| 61 | The | 64 | The |
| 62 | .Fn strncat | 65 | .Fn strncat |
| 63 | function returns the pointer | 66 | function returns the pointer |
| 64 | .Fa s . | 67 | .Fa dst . |
| 65 | .Sh EXAMPLES | 68 | .Sh EXAMPLES |
| 66 | The following appends | ||
| 67 | .Dq Li abc | ||
| 68 | to | ||
| 69 | .Va chararray : | ||
| 70 | .Bd -literal -offset indent | ||
| 71 | char *letters = "abcdefghi"; | ||
| 72 | |||
| 73 | (void)strncat(chararray, letters, 3); | ||
| 74 | .Ed | ||
| 75 | .Pp | ||
| 76 | The following example shows how to use | 69 | The following example shows how to use |
| 77 | .Fn strncat | 70 | .Fn strncat |
| 78 | safely in conjunction with | 71 | in conjunction with |
| 79 | .Xr strncpy 3 . | 72 | .Xr strncpy 3 : |
| 80 | .Bd -literal -offset indent | 73 | .Bd -literal -offset indent |
| 81 | char buf[BUFSIZ]; | 74 | char buf[BUFSIZ]; |
| 82 | char *input, *suffix; | 75 | char *base, *suffix; |
| 83 | 76 | ||
| 84 | (void)strncpy(buf, input, sizeof(buf) - 1); | 77 | (void)strncpy(buf, base, sizeof(buf) - 1); |
| 85 | buf[sizeof(buf) - 1] = '\e0'; | 78 | buf[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 |
| 89 | The above will copy as many characters from | 82 | The above will copy as many characters from |
| 90 | .Va input | 83 | .Va base |
| 91 | to | 84 | to |
| 92 | .Va buf | 85 | .Va buf |
| 93 | as will fit. | 86 | as will fit. |
| 94 | It then appends as many characters from | 87 | It then appends as many characters from |
| 95 | .Va suffix | 88 | .Va suffix |
| 96 | as will fit (or none | 89 | as will fit. |
| 97 | if there is no space). | 90 | If either |
| 98 | For operations like this, the | 91 | .Va base |
| 92 | or | ||
| 93 | .Va suffix | ||
| 94 | are too large, truncation will occur without detection. | ||
| 95 | .Pp | ||
| 96 | The above example shows dangerous coding patterns, including an | ||
| 97 | inability to detect truncation. | ||
| 98 | .Fn strncat | ||
| 99 | and | ||
| 100 | .Fn strncpy | ||
| 101 | are dangerously easy to misuse. | ||
| 102 | The | ||
| 99 | .Xr strlcpy 3 | 103 | .Xr strlcpy 3 |
| 100 | and | 104 | and |
| 101 | .Xr strlcat 3 | 105 | .Xr strlcat 3 |
| 102 | functions are a better choice, as shown below. | 106 | functions are safer for this kind of operation: |
| 107 | .Bd -literal -offset indent | ||
| 108 | if (strlcpy(buf, base, sizeof(buf)) >= sizeof(buf) || | ||
| 109 | strlcat(buf, suffix, sizeof(buf)) >= sizeof(buf)) | ||
| 110 | goto toolong; | ||
| 111 | |||
| 112 | .Ed | ||
| 113 | or for greatest portability, | ||
| 103 | .Bd -literal -offset indent | 114 | .Bd -literal -offset indent |
| 104 | (void)strlcpy(buf, input, sizeof(buf)); | 115 | if (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 |
| 118 | The | 125 | The |
| 119 | .Fn strcat | ||
| 120 | and | ||
| 121 | .Fn strncat | 126 | .Fn strncat |
| 122 | functions conform to | 127 | function conform to |
| 123 | .St -ansiC . | 128 | .St -ansiC . |
| 124 | .Sh HISTORY | 129 | .Sh HISTORY |
| 125 | The | 130 | The |
