summaryrefslogtreecommitdiff
path: root/devdocs/c/string%2Fbyte%2Fstrcpy.html
blob: 723ef82b77ff02e22d4fefd7af992f58aa71e99a (plain)
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
    <h1 id="firstHeading" class="firstHeading">strcpy, strcpy_s</h1>            <table class="t-dcl-begin"> <tr class="t-dsc-header"> <th> Defined in header <code>&lt;string.h&gt;</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 *strcpy( char *dest, const char *src );</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 *strcpy( char *restrict dest, const char *restrict src );</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 strcpy_s( char *restrict dest, rsize_t destsz, const char *restrict src );</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 the null-terminated byte string pointed to by <code>src</code>, including the null terminator, to the character array whose first element is pointed to by <code>dest</code>.</div> <div class="t-li1">
 The behavior is undefined if the <code>dest</code> array is not large enough. The behavior is undefined if the strings overlap. The behavior is undefined if either <code>dest</code> is not a pointer to a character array or <code>src</code> is not a pointer to a null-terminated byte string.</div> <div class="t-li1">
<span class="t-li">2)</span> Same as <span class="t-v">(1)</span>, except that it may clobber the rest of the destination array with unspecified values 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> is zero or greater than <code>RSIZE_MAX</code> </li>
<li> <code>destsz</code> is less or equal <code>strnlen_s(src, destsz)</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> &lt;= <code>strnlen_s(src, destsz)</code> &lt; <code>destsz</code>; in other words, an erroneous value of <code>destsz</code> does not expose the impending buffer overflow. As with all bounds-checked functions, <code>strcpy_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>&lt;string.h&gt;</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 write to </td>
</tr> <tr class="t-par"> <td> src </td> <td> - </td> <td> pointer to the null-terminated byte string to copy from </td>
</tr> <tr class="t-par"> <td> destsz </td> <td> - </td> <td> maximum number of characters to write, typically 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><code>strcpy_s</code> is allowed to clobber the destination array from the last character written up to <code>destsz</code> in order to improve efficiency: it may copy in multibyte blocks and then check for null bytes.</p>
<p>The function <code>strcpy_s</code> is similar to the BSD function <code>strlcpy</code>, except that</p>
<ul>
<li> <code>strlcpy</code> truncates the source string to fit in the destination (which is a security risk) </li>
<li> <code>strlcpy</code> does not perform all the runtime checks that <code>strcpy_s</code> does </li>
<li> <code>strlcpy</code> does not make failures obvious by setting the destination to a null string or calling a handler if the call fails. </li>
</ul> <p>Although <code>strcpy_s</code> prohibits truncation due to potential security risks, it's possible to truncate a string using bounds-checked <code><a href="strncpy" title="c/string/byte/strncpy">strncpy_s</a></code> instead.</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 &lt;string.h&gt;
#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
 
int main(void)
{
    const char *src = "Take the test.";
//  src[0] = 'M' ; // this would be undefined behavior
    char dst[strlen(src) + 1]; // +1 to accommodate for the null terminator
    strcpy(dst, src);
    dst[0] = 'M'; // OK
    printf("src = %s\ndst = %s\n", src, dst);
 
#ifdef __STDC_LIB_EXT1__
    set_constraint_handler_s(ignore_handler_s);
    int r = strcpy_s(dst, sizeof dst, src);
    printf("dst = \"%s\", r = %d\n", dst, r);
    r = strcpy_s(dst, sizeof dst, "Take even more tests.");
    printf("dst = \"%s\", r = %d\n", dst, r);
#endif
}</pre></div> <p>Possible output:</p>
<div class="text source-text"><pre data-language="c">src = Take the test.
dst = Make the test.
dst = "Take the test.", r = 0
dst = "", r = 22</pre></div> </div> <h3 id="References"> References</h3>  <ul>
<li> C17 standard (ISO/IEC 9899:2018): </li>
<ul>
<li> 7.24.2.3 The strcpy function (p: 264-265) </li>
<li> K.3.7.1.3 The strcpy_s function (p: 447) </li>
</ul>
<li> C11 standard (ISO/IEC 9899:2011): </li>
<ul>
<li> 7.24.2.3 The strcpy function (p: 363) </li>
<li> K.3.7.1.3 The strcpy_s function (p: 615-616) </li>
</ul>
<li> C99 standard (ISO/IEC 9899:1999): </li>
<ul><li> 7.21.2.3 The strcpy function (p: 326) </li></ul>
<li> C89/C90 standard (ISO/IEC 9899:1990): </li>
<ul><li> 4.11.2.3 The strcpy function </li></ul>
</ul>                 <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td> <div><a href="strncpy" title="c/string/byte/strncpy"> <span class="t-lines"><span>strncpy</span><span>strncpy_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 a certain amount of characters from 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="../wide/wcscpy" title="c/string/wide/wcscpy"> <span class="t-lines"><span>wcscpy</span><span>wcscpy_s</span></span></a></div>
<div><span class="t-lines"><span><span class="t-mark-rev t-since-c95">(C95)</span></span><span><span class="t-mark-rev t-since-c11">(C11)</span></span></span></div> </td> <td> copies one wide string 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/strdup" title="c/experimental/dynamic/strdup"> <span class="t-lines"><span>strdup</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 <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/strcpy" title="cpp/string/byte/strcpy">C++ documentation</a></span> for <code>strcpy</code> </td>
</tr> </table>           <div class="_attribution">
  <p class="_attribution-p">
    &copy; 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/strcpy" class="_attribution-link">https://en.cppreference.com/w/c/string/byte/strcpy</a>
  </p>
</div>