1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
<h1 id="firstHeading" class="firstHeading">strncpy, strncpy_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 *strncpy( 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 *strncpy( 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 strncpy_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> Copies at most <code>count</code> characters of the character array pointed to by <code>src</code> (including the terminating null character, but not any of the characters that follow the null character) to character array pointed to by <code>dest</code>. </div> <div class="t-li1">
If <code>count</code> is reached before the entire array <code>src</code> was copied, the resulting character array is not null-terminated.</div> <div class="t-li1">
If, after copying the terminating null character from <code>src</code>, <code>count</code> is not reached, additional null characters are written to <code>dest</code> until the total of <code>count</code> characters have been written.</div> <div class="t-li1">
The behavior is undefined if the character arrays overlap, if either <code>dest</code> or <code>src</code> is not a pointer to a character array (including if <code>dest</code> or <code>src</code> is a null pointer), if the size of the array pointed to by <code>dest</code> is less than <code>count</code>, or if the size of the array pointed to by <code>src</code> is less than <code>count</code> and it does not contain a null character.</div> <div class="t-li1">
<span class="t-li">2)</span> Same as <span class="t-v">(1)</span>, except that the function does not continue writing zeroes into the destination array to pad up to <code>count</code>, it stops after writing the terminating null character (if there was no null in the source, it writes one at <code>dest[count]</code> and then stops). Also, 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> is zero or greater than <code>RSIZE_MAX</code> </li>
<li> <code>count</code> is greater than <code>RSIZE_MAX</code> </li>
<li> <code>count</code> is greater or equal <code>destsz</code>, but <code>destsz</code> is less or equal <code>strnlen_s(src, count)</code>, in other words, truncation would occur </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_s(src, destsz)</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_s(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>strncpy_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 character array to copy 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>) and may clobber the rest of the destination array with unspecified values.</div> <h3 id="Notes"> Notes</h3> <p>As corrected by the post-C11 DR 468, <code>strncpy_s</code>, unlike <code><a href="strcpy" title="c/string/byte/strcpy">strcpy_s</a></code>, is only allowed to clobber the remainder of the destination array if an error occurs.</p>
<p>Unlike <code>strncpy</code>, <code>strncpy_s</code> does not pad the destination array with zeroes, This is a common source of errors when converting existing code to the bounds-checked version.</p>
<p>Although truncation to fit the destination buffer is a security risk and therefore a runtime constraints violation for <code>strncpy_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>strncpy_s(dst, sizeof dst, src, (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>
#include <errno.h>
int main(void)
{
char src[] = "hi";
char dest[6] = "abcdef"; // no null terminator
strncpy(dest, src, 5); // writes five characters 'h', 'i', '\0', '\0', '\0' to dest
printf("strncpy(dest, src, 5) to a 6-byte dest gives : ");
for (size_t n = 0; n < sizeof dest; ++n) {
char c = dest[n];
c ? printf("'%c' ", c) : printf("'\\0' ");
}
printf("\nstrncpy(dest2, src, 2) to a 2-byte dst gives : ");
char dest2[2];
strncpy(dest2, src, 2); // truncation: writes two characters 'h', 'i', to dest2
for (size_t n = 0; n < sizeof dest2; ++n) {
char c = dest2[n];
c ? printf("'%c' ", c) : printf("'\\0' ");
}
printf("\n");
#ifdef __STDC_LIB_EXT1__
set_constraint_handler_s(ignore_handler_s);
char dst1[6], src1[100] = "hello";
errno_t r1 = strncpy_s(dst1, 6, src1, 100); // writes 0 to r1, 6 characters to dst1
printf("dst1 = \"%s\", r1 = %d\n", dst1,r1); // 'h','e','l','l','o','\0' to dst1
char dst2[5], src2[7] = {'g','o','o','d','b','y','e'};
errno_t r2 = strncpy_s(dst2, 5, src2, 7); // copy overflows the destination array
printf("dst2 = \"%s\", r2 = %d\n", dst2,r2); // writes nonzero to r2,'\0' to dst2[0]
char dst3[5];
errno_t r3 = strncpy_s(dst3, 5, src2, 4); // writes 0 to r3, 5 characters to dst3
printf("dst3 = \"%s\", r3 = %d\n", dst3,r3); // 'g', 'o', 'o', 'd', '\0' to dst3
#endif
}</pre></div> <p>Possible output:</p>
<div class="text source-text"><pre data-language="c">strncpy(dest, src, 5) to a 6-byte dst gives : 'h' 'i' '\0' '\0' '\0' 'f'
strncpy(dest2, src, 2) to a 2-byte dst gives : 'h' 'i'
dst1 = "hello", r1 = 0
dst2 = "", r2 = 22
dst3 = "good", r3 = 0</pre></div> </div> <h3 id="References"> References</h3> <ul>
<li> C17 standard (ISO/IEC 9899:2018): </li>
<ul>
<li> 7.24.2.4 The strncpy function (p: 265) </li>
<li> K.3.7.1.4 The strncpy_s function (p: 447-448) </li>
</ul>
<li> C11 standard (ISO/IEC 9899:2011): </li>
<ul>
<li> 7.24.2.4 The strncpy function (p: 363-364) </li>
<li> K.3.7.1.4 The strncpy_s function (p: 616-617) </li>
</ul>
<li> C99 standard (ISO/IEC 9899:1999): </li>
<ul><li> 7.21.2.4 The strncpy function (p: 326-327) </li></ul>
<li> C89/C90 standard (ISO/IEC 9899:1990): </li>
<ul><li> 4.11.2.4 The strncpy function </li></ul>
</ul> <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <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="memcpy" title="c/string/byte/memcpy"> <span class="t-lines"><span>memcpy</span><span>memcpy_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 buffer to another <br> <span class="t-mark">(function)</span> </td>
</tr> <tr class="t-dsc"> <td> <div><a href="https://en.cppreference.com/w/c/experimental/dynamic/strndup" title="c/experimental/dynamic/strndup"> <span class="t-lines"><span>strndup</span></span></a></div>
<div><span class="t-lines"><span><span class="t-mark-rev t-since-dynamic-tr t-mark-ts">(dynamic memory TR)</span></span></span></div> </td> <td> allocate a copy of a string up to specified size <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/strncpy" title="cpp/string/byte/strncpy">C++ documentation</a></span> for <code>strncpy</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/strncpy" class="_attribution-link">https://en.cppreference.com/w/c/string/byte/strncpy</a>
</p>
</div>
|