diff options
author | Egor Bogatov <egorbo@gmail.com> | 2020-01-28 22:00:30 +0300 |
---|---|---|
committer | Bernhard Urban-Forster <lewurm@gmail.com> | 2020-01-28 22:00:30 +0300 |
commit | 883162e861c95b7be7c3e830f1d70ff1c8e74fa9 (patch) | |
tree | 595c2c2f82f2d73c0f96ed4769ae21c37254d314 | |
parent | 7d2924adcb0df8a6aef59f06f60ad55eabe1f715 (diff) |
Use memmove in Buffer.MemoryCopy (#18547)
* Use memmove in Buffer.MemoryCopy
* add test case
-rw-r--r-- | mcs/class/corlib/ReferenceSources/Buffer.cs | 24 | ||||
-rw-r--r-- | mcs/class/corlib/Test/System/BufferTest.cs | 22 |
2 files changed, 34 insertions, 12 deletions
diff --git a/mcs/class/corlib/ReferenceSources/Buffer.cs b/mcs/class/corlib/ReferenceSources/Buffer.cs index f89199219b5..5c83291cf5c 100644 --- a/mcs/class/corlib/ReferenceSources/Buffer.cs +++ b/mcs/class/corlib/ReferenceSources/Buffer.cs @@ -86,14 +86,14 @@ namespace System var src = (byte*)source; var dst = (byte*)destination; - while (sourceBytesToCopy > int.MaxValue) { - Memcpy (dst, src, int.MaxValue); - sourceBytesToCopy -= int.MaxValue; - src += int.MaxValue; - dst += int.MaxValue; + while (sourceBytesToCopy > uint.MaxValue) { + Memmove (dst, src, uint.MaxValue); + sourceBytesToCopy -= uint.MaxValue; + src += uint.MaxValue; + dst += uint.MaxValue; } - Memcpy (dst, src, (int) sourceBytesToCopy); + Memmove (dst, src, (uint) sourceBytesToCopy); } [CLSCompliantAttribute (false)] @@ -105,14 +105,14 @@ namespace System var src = (byte*)source; var dst = (byte*)destination; - while (sourceBytesToCopy > int.MaxValue) { - Memcpy (dst, src, int.MaxValue); - sourceBytesToCopy -= int.MaxValue; - src += int.MaxValue; - dst += int.MaxValue; + while (sourceBytesToCopy > uint.MaxValue) { + Memmove (dst, src, uint.MaxValue); + sourceBytesToCopy -= uint.MaxValue; + src += uint.MaxValue; + dst += uint.MaxValue; } - Memcpy (dst, src, (int) sourceBytesToCopy); + Memmove (dst, src, (uint) sourceBytesToCopy); } internal static unsafe void memcpy4 (byte *dest, byte *src, int size) { diff --git a/mcs/class/corlib/Test/System/BufferTest.cs b/mcs/class/corlib/Test/System/BufferTest.cs index 120f75bd435..06ad188180d 100644 --- a/mcs/class/corlib/Test/System/BufferTest.cs +++ b/mcs/class/corlib/Test/System/BufferTest.cs @@ -297,5 +297,27 @@ namespace MonoTests.System { Assert.AreEqual (0xAABB0000, b, "#2"); } } + + [Test] // https://github.com/mono/mono/issues/18516 + public unsafe void MemoryCopy_Overlapped () + { + byte [] buffer = new byte [5]; + for (int i = 0; i < buffer.Length; i++) + buffer [i] = (byte)i; + + int bytesToCopy = buffer.Length - 1; + fixed (byte* pBuffer = buffer) + Buffer.MemoryCopy (pBuffer, pBuffer + 1, buffer.Length - 1, bytesToCopy); + + bool failed = false; + for (int i = 0; i < buffer.Length; i++) + { + byte expectedByte = (byte)(i == 0 ? 0 : i - 1); + if (buffer [i] != expectedByte) + failed = true; + } + + Assert.IsFalse (failed); + } } } |