diff options
Diffstat (limited to 'devdocs/c/string%2Fbyte%2Fstrncat.html')
| -rw-r--r-- | devdocs/c/string%2Fbyte%2Fstrncat.html | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/devdocs/c/string%2Fbyte%2Fstrncat.html b/devdocs/c/string%2Fbyte%2Fstrncat.html new file mode 100644 index 00000000..282db51e --- /dev/null +++ b/devdocs/c/string%2Fbyte%2Fstrncat.html @@ -0,0 +1,93 @@ + <h1 id="firstHeading" class="firstHeading">strncat, strncat_s</h1> <table class="t-dcl-begin"> <tr class="t-dsc-header"> <th> Defined in header <code><string.h></code> </th> <th> </th> <th> </th> </tr> <tr class="t-dcl-rev-aux"> <td></td> <td rowspan="3">(1)</td> <td></td> </tr> <tr class="t-dcl t-until-c99"> <td> <pre data-language="c">char *strncat( char *dest, const char *src, size_t count );</pre> +</td> <td> <span class="t-mark-rev t-until-c99">(until C99)</span> </td> </tr> <tr class="t-dcl t-since-c99"> <td> <pre data-language="c">char *strncat( char *restrict dest, const char *restrict src, size_t count );</pre> +</td> <td> <span class="t-mark-rev t-since-c99">(since C99)</span> </td> </tr> <tr class="t-dcl t-since-c11"> <td> <pre data-language="c">errno_t strncat_s( char *restrict dest, rsize_t destsz, + const char *restrict src, rsize_t count );</pre> +</td> <td> (2) </td> <td> <span class="t-mark-rev t-since-c11">(since C11)</span> </td> </tr> </table> <div class="t-li1"> +<span class="t-li">1)</span> Appends at most <code>count</code> characters from the character array pointed to by <code>src</code>, stopping if the null character is found, to the end of the null-terminated byte string pointed to by <code>dest</code>. The character <code>src[0]</code> replaces the null terminator at the end of <code>dest</code>. The terminating null character is always appended in the end (so the maximum number of bytes the function may write is <code>count+1</code>).</div> <div class="t-li1"> + The behavior is undefined if the destination array does not have enough space for the contents of both <code>dest</code> and the first <code>count</code> characters of <code>src</code>, plus the terminating null character. The behavior is undefined if the source and destination objects overlap. The behavior is undefined if either <code>dest</code> is not a pointer to a null-terminated byte string or <code>src</code> is not a pointer to a character array,</div> <div class="t-li1"> +<span class="t-li">2)</span> Same as <span class="t-v">(1)</span>, except that this function may clobber the remainder of the destination array (from the last byte written to <code>destsz</code>) and that the following errors are detected at runtime and call the currently installed <a href="../../error/set_constraint_handler_s" title="c/error/set constraint handler s">constraint handler</a> function: <ul> +<li> <code>src</code> or <code>dest</code> is a null pointer </li> +<li> <code>destsz</code> or <code>count</code> is zero or greater than <code>RSIZE_MAX</code> </li> +<li> there is no null character in the first <code>destsz</code> bytes of <code>dest</code> </li> +<li> truncation would occur: <code>count</code> or the length of <code>src</code>, whichever is less, exceeds the space available between the null terminator of <code>dest</code> and <code>destsz</code>. </li> +<li> overlap would occur between the source and the destination strings</li> +</ul> +</div> <div class="t-li1"> + The behavior is undefined if the size of the character array pointed to by <code>dest</code> < <code>strnlen(dest,destsz)+strnlen(src,count)+1</code> < <code>destsz</code>; in other words, an erroneous value of <code>destsz</code> does not expose the impending buffer overflow. The behavior is undefined if the size of the character array pointed to by <code>src</code> < <code>strnlen(src,count)</code> < <code>destsz</code>; in other words, an erroneous value of <code>count</code> does not expose the impending buffer overflow. As with all bounds-checked functions, <code>strncat_s</code> only guaranteed to be available if <code>__STDC_LIB_EXT1__</code> is defined by the implementation and if the user defines <code>__STDC_WANT_LIB_EXT1__</code> to the integer constant <code>1</code> before including <a href="../byte" title="c/string/byte"><code><string.h></code></a>.</div> <h3 id="Parameters"> Parameters</h3> <table class="t-par-begin"> <tr class="t-par"> <td> dest </td> <td> - </td> <td> pointer to the null-terminated byte string to append to </td> +</tr> <tr class="t-par"> <td> src </td> <td> - </td> <td> pointer to the character array to copy from </td> +</tr> <tr class="t-par"> <td> count </td> <td> - </td> <td> maximum number of characters to copy </td> +</tr> <tr class="t-par"> <td> destsz </td> <td> - </td> <td> the size of the destination buffer </td> +</tr> +</table> <h3 id="Return_value"> Return value</h3> <div class="t-li1"> +<span class="t-li">1)</span> returns a copy of <code>dest</code> +</div> <div class="t-li1"> +<span class="t-li">2)</span> returns zero on success, returns non-zero on error. Also, on error, writes zero to <code>dest[0]</code> (unless <code>dest</code> is a null pointer or <code>destsz</code> is zero or greater than <code>RSIZE_MAX</code>).</div> <h3 id="Notes"> Notes</h3> <p>Because <code>strncat</code> needs to seek to the end of <code>dest</code> on each call, it is inefficient to concatenate many strings into one using <code>strncat</code>.</p> +<p>Although truncation to fit the destination buffer is a security risk and therefore a runtime constraints violation for <code>strncat_s</code>, it is possible to get the truncating behavior by specifying <code>count</code> equal to the size of the destination array minus one: it will copy the first <code>count</code> bytes and append the null terminator as always: <code>strncat_s(dst, sizeof dst, src, (sizeof dst)-strnlen_s(dst, sizeof dst)-1);</code></p> +<h3 id="Example"> Example</h3> <div class="t-example"> <div class="c source-c"><pre data-language="c">#define __STDC_WANT_LIB_EXT1__ 1 +#include <string.h> +#include <stdio.h> +#include <stdlib.h> + +int main(void) +{ + char str[50] = "Hello "; + char str2[50] = "World!"; + strcat(str, str2); + strncat(str, " Goodbye World!", 3); + puts(str); + +#ifdef __STDC_LIB_EXT1__ + set_constraint_handler_s(ignore_handler_s); + char s1[100] = "good"; + char s5[1000] = "bye"; + int r1 = strncat_s(s1, 100, s5, 1000); // r1 is 0, s1 holds "goodbye\0" + printf("s1 = %s, r1 = %d\n", s1, r1); + char s2[6] = "hello"; + int r2 = strncat_s(s2, 6, "", 1); // r2 is 0, s2 holds "hello\0" + printf("s2 = %s, r2 = %d\n", s2, r2); + char s3[6] = "hello"; + int r3 = strncat_s(s3, 6, "X", 2); // r3 is non-zero, s3 holds "\0" + printf("s3 = %s, r3 = %d\n", s3, r3); + // the strncat_s truncation idiom: + char s4[7] = "abc"; + int r4 = strncat_s(s4, 7, "defghijklmn", 3); // r4 is 0, s4 holds "abcdef\0" + printf("s4 = %s, r4 = %d\n", s4, r4); +#endif +}</pre></div> <p>Possible output:</p> +<div class="text source-text"><pre data-language="c">Hello World! Go +s1 = goodbye, r1 = 0 +s2 = hello, r2 = 0 +s3 = , r3 = 22 +s4 = abcdef, r4 = 0</pre></div> </div> <h3 id="References"> References</h3> <ul> +<li> C23 standard (ISO/IEC 9899:2023): </li> +<ul> +<li> 7.26.3.2 The strncat function (p: 379) </li> +<li> K.3.7.2.2 The strncat_s function (p: TBD) </li> +</ul> +<li> C17 standard (ISO/IEC 9899:2018): </li> +<ul> +<li> 7.24.3.2 The strncat function (p: 265-266) </li> +<li> K.3.7.2.2 The strncat_s function (p: 449-450) </li> +</ul> +<li> C11 standard (ISO/IEC 9899:2011): </li> +<ul> +<li> 7.24.3.2 The strncat function (p: 364-365) </li> +<li> K.3.7.2.2 The strncat_s function (p: 618-620) </li> +</ul> +<li> C99 standard (ISO/IEC 9899:1999): </li> +<ul><li> 7.21.3.2 The strncat function (p: 327-328) </li></ul> +<li> C89/C90 standard (ISO/IEC 9899:1990): </li> +<ul><li> 4.11.3.2 The strncat function </li></ul> +</ul> <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td> <div><a href="strcat" title="c/string/byte/strcat"> <span class="t-lines"><span>strcat</span><span>strcat_s</span></span></a></div> +<div><span class="t-lines"><span><span class="t-mark-rev t-since-c11">(C11)</span></span></span></div> </td> <td> concatenates two strings <br> <span class="t-mark">(function)</span> </td> +</tr> <tr class="t-dsc"> <td> <div><a href="strcpy" title="c/string/byte/strcpy"> <span class="t-lines"><span>strcpy</span><span>strcpy_s</span></span></a></div> +<div><span class="t-lines"><span><span class="t-mark-rev t-since-c11">(C11)</span></span></span></div> </td> <td> copies one string to another <br> <span class="t-mark">(function)</span> </td> +</tr> <tr class="t-dsc"> <td> <div><a href="memccpy" title="c/string/byte/memccpy"> <span class="t-lines"><span>memccpy</span></span></a></div> +<div><span class="t-lines"><span><span class="t-mark-rev t-since-c23">(C23)</span></span></span></div> </td> <td> copies one buffer to another, stopping after the specified delimiter <br> <span class="t-mark">(function)</span> </td> +</tr> <tr class="t-dsc"> <td colspan="2"> <span><a href="https://en.cppreference.com/w/cpp/string/byte/strncat" title="cpp/string/byte/strncat">C++ documentation</a></span> for <code>strncat</code> </td> +</tr> </table> <div class="_attribution"> + <p class="_attribution-p"> + © cppreference.com<br>Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.<br> + <a href="https://en.cppreference.com/w/c/string/byte/strncat" class="_attribution-link">https://en.cppreference.com/w/c/string/byte/strncat</a> + </p> +</div> |
