Notice (2018-05-24): bugzilla.xamarin.com is now in
Please join us on
Visual Studio Developer Community and in the
Mono organizations on
GitHub to continue tracking issues. Bugzilla will remain
available for reference in read-only mode. We will continue to work
on open Bugzilla bugs, copy them to the new locations
as needed for follow-up, and add the new items under Related
Our sincere thanks to everyone who has contributed on this bug
tracker over the years. Thanks also for your understanding as we
make these adjustments and improvements for the future.
Please create a new report on
GitHub or Developer Community with
your current version information, steps to reproduce, and relevant error
messages or log files if you are hitting an issue that looks similar to
this resolved bug and you do not yet see a matching new report.
Created attachment 11854 [details]
patch special-casing UTF32Encoding as UTF8Encoding, UnicodeEncoding, etc. are
UnixMarshal.PtrToString(IntPtr, Encoding) does not work correctly if the Encoding argument is an instance of UTF32Encoding.
PtrToString calls GetStringByteLength, which doesn't special-case UTF32Encoding and thus calls GetRandomBufferLength. However, encoding.GetMaxByteCount(1) returns 8 for instances of UTF32Encoding (apparently because some specific input of one char can return two UTF-32 units, i.e. 8 bytes). Thus, GetRandomBufferLength reads until encountering 8 NUL bytes instead of the requisite 4.
Since GetStringByteLength contains special cases for UTF-8, UTF-7, UTF-16, ASCII and UnixEncoding, it probably makes sense to special-case UTF-32 too, as I have done in the attached patch.
The attached BrokenUtf32DecodingTestProgram.cs is a test application that marshals a byte array containing (among some garbage) a UTF-32 string, then tries to unmarshal it using PtrToString.
Created attachment 11855 [details]
test program for unmarshaling UTF-32 strings
Opened a pull request on Github: https://github.com/mono/mono/pull/1913
Merged in f7816d844aa482f3682efc3111f9001f0994b2d7.