diff --git a/include/cpp/marshal/View.hpp b/include/cpp/marshal/View.hpp index 394837f72..d60fd587a 100644 --- a/include/cpp/marshal/View.hpp +++ b/include/cpp/marshal/View.hpp @@ -15,7 +15,10 @@ inline bool cpp::marshal::View::tryCopyTo(const View& destination) const return false; } - std::memcpy(destination.ptr.ptr, ptr.ptr, sizeof(T) * length); + if (ptr.ptr + length < destination.ptr.ptr || destination.ptr.ptr + length < ptr.ptr) + std::memcpy(destination.ptr.ptr, ptr.ptr, sizeof(T) * length); + else + std::memmove(destination.ptr.ptr, ptr.ptr, sizeof(T) * length); return true; } diff --git a/test/native/tests/marshalling/view/TestView.hx b/test/native/tests/marshalling/view/TestView.hx index 104a56e7a..22ad70a05 100644 --- a/test/native/tests/marshalling/view/TestView.hx +++ b/test/native/tests/marshalling/view/TestView.hx @@ -291,5 +291,13 @@ class TestView extends Test { Assert.same(buffer, biggerDst.slice(0, 10)); Assert.same(buffer, matchingDst); Assert.same(buffer.slice(0, smallerDst.length), smallerDst); + + final overlapped = buffer.copy(); + + final regionA = overlapped.asView().slice(2, 5); + final regionB = overlapped.asView().slice(4, 5); + + Assert.isTrue(regionB.tryCopyTo(regionA)); + Assert.same([0, 1, 4, 5, 6, 7, 8, 7, 8, 9], overlapped); } }