summaryrefslogtreecommitdiff
path: root/devdocs/c/string%2Fbyte%2Fstrcpy.html
diff options
context:
space:
mode:
authorCraig Jennings <c@cjennings.net>2024-04-07 13:41:34 -0500
committerCraig Jennings <c@cjennings.net>2024-04-07 13:41:34 -0500
commit754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 (patch)
treef1190704f78f04a2b0b4c977d20fe96a828377f1 /devdocs/c/string%2Fbyte%2Fstrcpy.html
new repository
Diffstat (limited to 'devdocs/c/string%2Fbyte%2Fstrcpy.html')
-rw-r--r--devdocs/c/string%2Fbyte%2Fstrcpy.html82
1 files changed, 82 insertions, 0 deletions
diff --git a/devdocs/c/string%2Fbyte%2Fstrcpy.html b/devdocs/c/string%2Fbyte%2Fstrcpy.html
new file mode 100644
index 00000000..723ef82b
--- /dev/null
+++ b/devdocs/c/string%2Fbyte%2Fstrcpy.html
@@ -0,0 +1,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>