Move from memcpy to memcpy_s.
Change-Id: If7d53c6e54428f01f14528c3f281331d308af56a
diff --git a/src/std.c b/src/std.c
index 00d4704..b7ea2ce 100644
--- a/src/std.c
+++ b/src/std.c
@@ -20,6 +20,7 @@
/* Declare unsafe functions locally so they are not available globally. */
void *memset(void *s, int c, size_t n);
+void *memcpy(void *dst, const void *src, size_t n);
void memset_s(void *dest, rsize_t destsz, int ch, rsize_t count)
{
@@ -41,3 +42,39 @@
fail:
panic("memset_s failure");
}
+
+void memcpy_s(void *dest, rsize_t destsz, const void *src, rsize_t count)
+{
+ uintptr_t d = (uintptr_t)dest;
+ uintptr_t s = (uintptr_t)src;
+
+ if (dest == NULL || src == NULL) {
+ goto fail;
+ }
+
+ if (destsz > RSIZE_MAX || count > RSIZE_MAX) {
+ goto fail;
+ }
+
+ if (count > destsz) {
+ goto fail;
+ }
+
+ /* Destination overlaps the end of source. */
+ if (d > s && d < (s + count)) {
+ goto fail;
+ }
+
+ /* Source overlaps the end of destination. */
+ if (s > d && s < (d + destsz)) {
+ goto fail;
+ }
+
+ /* TODO: consider wrapping? */
+
+ memcpy(dest, src, count);
+ return;
+
+fail:
+ panic("memcpy_s failure");
+}