From 754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 Mon Sep 17 00:00:00 2001 From: Craig Jennings Date: Sun, 7 Apr 2024 13:41:34 -0500 Subject: new repository --- devdocs/c/string%2Fbyte%2Fstrcpy.html | 82 +++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 devdocs/c/string%2Fbyte%2Fstrcpy.html (limited to 'devdocs/c/string%2Fbyte%2Fstrcpy.html') 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 @@ +

strcpy, strcpy_s

Defined in header <string.h>
(1)
char *strcpy( char *dest, const char *src );
+
(until C99)
char *strcpy( char *restrict dest, const char *restrict src );
+
(since C99)
errno_t strcpy_s( char *restrict dest, rsize_t destsz, const char *restrict src );
+
(2) (since C11)
+1) Copies the null-terminated byte string pointed to by src, including the null terminator, to the character array whose first element is pointed to by dest.
+ The behavior is undefined if the dest array is not large enough. The behavior is undefined if the strings overlap. The behavior is undefined if either dest is not a pointer to a character array or src is not a pointer to a null-terminated byte string.
+2) Same as (1), 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 constraint handler function: +
+ The behavior is undefined if the size of the character array pointed to by dest <= strnlen_s(src, destsz) < destsz; in other words, an erroneous value of destsz does not expose the impending buffer overflow. As with all bounds-checked functions, strcpy_s only guaranteed to be available if __STDC_LIB_EXT1__ is defined by the implementation and if the user defines __STDC_WANT_LIB_EXT1__ to the integer constant 1 before including <string.h>.

Parameters

+ + + +
dest - pointer to the character array to write to
src - pointer to the null-terminated byte string to copy from
destsz - maximum number of characters to write, typically the size of the destination buffer

Return value

+1) returns a copy of dest +
+2) returns zero on success, returns non-zero on error. Also, on error, writes zero to dest[0] (unless dest is a null pointer or destsz is zero or greater than RSIZE_MAX).

Notes

strcpy_s is allowed to clobber the destination array from the last character written up to destsz in order to improve efficiency: it may copy in multibyte blocks and then check for null bytes.

+

The function strcpy_s is similar to the BSD function strlcpy, except that

+

Although strcpy_s prohibits truncation due to potential security risks, it's possible to truncate a string using bounds-checked strncpy_s instead.

+

Example

#define __STDC_WANT_LIB_EXT1__ 1
+#include <string.h>
+#include <stdio.h>
+#include <stdlib.h>
+ 
+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
+}

Possible output:

+
src = Take the test.
+dst = Make the test.
+dst = "Take the test.", r = 0
+dst = "", r = 22

References

See also

+ + + + +
+
(C11)
copies a certain amount of characters from one string to another
(function)
+
(C11)
copies one buffer to another
(function)
+
(C95)(C11)
copies one wide string to another
(function)
+
(dynamic memory TR)
allocate a copy of a string
(function)
C++ documentation for strcpy
+

+ © cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
+ https://en.cppreference.com/w/c/string/byte/strcpy +

+
-- cgit v1.2.3