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

Incorrect NativeString<T> operator + implementation for 2-byte type characters #22

Open
3F opened this issue Oct 29, 2024 · 1 comment
Labels

Comments

@3F
Copy link
Owner

3F commented Oct 29, 2024

public static NativeString<T> operator +(NativeString<T> input, string newstr)
{
NativeString<T> ret = new(input, newstr.Length);
byte[] data = GetBytes(newstr);
WriteTo(((IntPtr)ret) + GetLengthFromPtr(input), data, ret.allocated);

Yep, WriteTo() actually writes beyond allocated region if WCharPtr or TCharPtr (when Unicode=true)
this ultimately leads to such consequences:

either ok, or:

The active test run was aborted. Reason: Test host process crashed

or:

Error Message:
Assert.Equal() Failure: Strings differ
Expected: "Рад знакомству 🤝 どうぞよろしく"
Actual: null

https://ci.appveyor.com/project/3Fs/dllexport-ix27o/builds/50882366#L297

and so on.

Test can be found here:
https://github.com/3F/DllExport/blob/9db4bf0ba6793523d12eb0a1680c0cd962283f19/src/DllExport/UnitedTest/NetfxAssetBasicTest.cs#L36

But I also forgot about the new disposing (that's another, actually even more interesting story*)

l.callme<TCharPtr>(ns + " and You 👋", nstruct);

*because this part must be fully reviewed since this creates non-obvious allocation which will lead to memory leaks

@3F 3F added the bug label Oct 29, 2024
@3F
Copy link
Owner Author

3F commented Oct 29, 2024

Note, you can avoid the error in Conari 1.5 easily with modern C# string interpolation, for example:

using NativeString<TCharPtr> nw = new($"{ns} and You 👋");
l.callme<TCharPtr>(nw, nstruct);

3F added a commit to 3F/DllExport that referenced this issue Nov 2, 2024
+Fixed possible 'Test host process crashed' due to bug in Conari 1.5
3F/Conari#22

Added new around strings, arrays, and structures
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

1 participant