Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Overlap Buffer.MemoryCopy causes unknown error #562

Closed
neuecc opened this issue Aug 20, 2019 · 4 comments
Closed

Overlap Buffer.MemoryCopy causes unknown error #562

neuecc opened this issue Aug 20, 2019 · 4 comments
Assignees
Labels
Milestone

Comments

@neuecc
Copy link
Member

neuecc commented Aug 20, 2019

@AArnott

Overlapped Buffer.MemoryCopy in WriteString_PostEncoding (or other places?) causes unknown error.
https://github.com/neuecc/MessagePack-CSharp/blob/15c5739/src/MessagePack.UnityClient/Assets/Scripts/MessagePack/MessagePackWriter.cs#L1251

For example, in Unity, this string serialization is broken.

// 'あ' is [227, 129, 130]
var bin = MessagePackSerializer.Serialize(new string('あ', 11));

// expected: 217, 33, 227, 129, 130, 227, 129, 130, ....
// actual:  217, 33, 227, 227, 227, 227, 227, 227, 227, 227, ...
UnityEngine.Debug.Log(string.Join(", ", bin));

This code works fine in .NET Core but maybe it is undefined in overlapped memcpy.

The parameter of MemoryCopy is

// pBuffer = address
// estimatedOffset = 1
// byteCount = 33
Buffer.MemoryCopy(pBuffer + estimatedOffset, pBuffer + 2, byteCount, byteCount);
@AArnott AArnott added this to the v2.0 milestone Aug 20, 2019
@AArnott AArnott self-assigned this Aug 20, 2019
@AArnott AArnott added the bug label Aug 20, 2019
@neuecc
Copy link
Member Author

neuecc commented Aug 20, 2019

In .NET Core, called memmove so it is safe.
https://source.dot.net/#System.Private.CoreLib/shared/System/Buffer.cs,146

But Unity's .NET (2018.3-mbe(MonoBleedingEdge)) maybe calls this code.
https://github.com/Unity-Technologies/mono/blob/unity-2018.3-mbe/mcs/class/corlib/ReferenceSources/Buffer.cs#L87

@AArnott
Copy link
Collaborator

AArnott commented Aug 20, 2019

Ya, Buffer.MemoryCopy is documented to work with overlapping buffers. We may want to avoid the bug in mono as well, but do you know where we can file a bug against mono?

@neuecc
Copy link
Member Author

neuecc commented Aug 20, 2019

@AArnott
Copy link
Collaborator

AArnott commented Jan 20, 2020

I filed mono/mono#18516

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants