summaryrefslogtreecommitdiff
path: root/lib/libc/string
diff options
context:
space:
mode:
Diffstat (limited to 'lib/libc/string')
-rw-r--r--lib/libc/string/memcpy.c39
-rw-r--r--lib/libc/string/strxfrm.c4
2 files changed, 41 insertions, 2 deletions
diff --git a/lib/libc/string/memcpy.c b/lib/libc/string/memcpy.c
index ecdbd602..05586b55 100644
--- a/lib/libc/string/memcpy.c
+++ b/lib/libc/string/memcpy.c
@@ -1,3 +1,4 @@
+#include <errno.h>
#include <string.h>
#include <features.h>
@@ -14,3 +15,41 @@ void *memcpy(void *restrict s1, const void *restrict s2, size_t n)
return s1;
}
+
+errno_t memcpy_s(void *restrict dest, rsize_t destsz, const void *restrict src,
+ rsize_t count)
+{
+ if (dest == NULL || src == NULL) {
+ if (dest != NULL && destsz > 0) {
+ unsigned char *d = dest;
+ for (rsize_t i = 0; i < destsz; i++) {
+ d[i] = 0;
+ }
+ }
+
+ return EINVAL;
+ }
+
+ const unsigned char *s = src;
+ unsigned char *d = dest;
+
+ if ((d > s && d < s + count) || (s > d && s < d + count)) {
+ for (rsize_t i = 0; i < destsz; i++) {
+ d[i] = 0;
+ }
+ return EINVAL;
+ }
+
+ if (count > destsz) {
+ for (rsize_t i = 0; i < destsz; i++) {
+ d[i] = 0;
+ }
+ return ERANGE;
+ }
+
+ for (rsize_t i = 0; i < count; i++) {
+ d[i] = s[i];
+ }
+
+ return 0;
+}
diff --git a/lib/libc/string/strxfrm.c b/lib/libc/string/strxfrm.c
index 123ebfe8..d216733b 100644
--- a/lib/libc/string/strxfrm.c
+++ b/lib/libc/string/strxfrm.c
@@ -6,13 +6,13 @@ size_t strxfrm(char *restrict s1, const char *restrict s2, size_t n)
size_t len = strlen(s2);
if (n > len)
- strcpy(s1, s2);
+ strlcpy(s1, s2, n);
return len;
}
__weak size_t strxfrm_l(char *restrict s1, const char *restrict s2, size_t n,
- locale_t __unused locale)
+ locale_t __unused locale)
{
return strxfrm(s1, s2, n);
}