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%2Fmemset.html | 73 +++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 devdocs/c/string%2Fbyte%2Fmemset.html (limited to 'devdocs/c/string%2Fbyte%2Fmemset.html') diff --git a/devdocs/c/string%2Fbyte%2Fmemset.html b/devdocs/c/string%2Fbyte%2Fmemset.html new file mode 100644 index 00000000..cf822c20 --- /dev/null +++ b/devdocs/c/string%2Fbyte%2Fmemset.html @@ -0,0 +1,73 @@ +

memset, memset_explicit, memset_s

Defined in header <string.h>
void *memset( void *dest, int ch, size_t count );
+
(1)
void *memset_explicit( void *dest, int ch, size_t count );
+
(2) (since C23)
errno_t memset_s( void *dest, rsize_t destsz, int ch, rsize_t count );
+
(3) (since C11)
+1) Copies the value (unsigned char)ch into each of the first count characters of the object pointed to by dest.
+ The behavior is undefined if access occurs beyond the end of the dest array. The behavior is undefined if dest is a null pointer.
+2) Same as (1), except that is safe for sensitive information.
+3) Same as (1), except that the following errors are detected at runtime and call the currently installed constraint handler function after storing ch in every location of the destination range [dest, dest+destsz) if dest and destsz are themselves valid: +
+ The behavior is undefined if the size of the character array pointed to by dest < count <= destsz; in other words, an erroneous value of destsz does not expose the impending buffer overflow. As with all bounds-checked functions, memset_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 object to fill
ch - fill byte
count - number of bytes to fill
destsz - size of the destination array

Return value

+1,2) A copy of dest +
+3) zero on success, non-zero on error. Also on error, if dest is not a null pointer and destsz is valid, writes destsz fill bytes ch to the destination array.

Notes

memset may be optimized away (under the as-if rules) if the object modified by this function is not accessed again for the rest of its lifetime (e.g., gcc bug 8537). For that reason, this function cannot be used to scrub memory (e.g., to fill an array that stored a password with zeroes).

+

This optimization is prohibited for memset_explicit and memset_s: they are guaranteed to perform the memory write.

+

Third-party solutions for that include FreeBSD explicit_bzero or Microsoft SecureZeroMemory.

+

Example

#define __STDC_WANT_LIB_EXT1__ 1
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+ 
+int main(void)
+{
+    char str[] = "ghghghghghghghghghghgh";
+    puts(str);
+    memset(str,'a',5);
+    puts(str);
+ 
+#ifdef __STDC_LIB_EXT1__
+    set_constraint_handler_s(ignore_handler_s);
+    int r = memset_s(str, sizeof str, 'b', 5);
+    printf("str = \"%s\", r = %d\n", str, r);
+    r = memset_s(str, 5, 'c', 10);   // count is greater than destsz  
+    printf("str = \"%s\", r = %d\n", str, r);
+#endif
+}

Possible output:

+
ghghghghghghghghghghgh
+aaaaahghghghghghghghgh
+str = "bbbbbhghghghghghghghgh", r = 0
+str = "ccccchghghghghghghghgh", r = 22

References

See also

+ + +
+
(C11)
copies one buffer to another
(function)
+
(C95)
copies the given wide character to every position in a wide character array
(function)
C++ documentation for memset
+

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

+
-- cgit v1.2.3