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 8478 [details]
Demonstration console app
In the attached demonstration console app, I've developed a custom implementation of an EncoderFallback and EncoderFallbackBuffer. The custom implementation is intended to take characters that fail to encode and convert them into JSON-escaped strings; for example, the character é cannot be encoded in an ASCII encoding, so the fallback would convert it to the string \u00e9 instead.
Running with Mono 3.2.8, the attached demonstration console app outputs small portions of the encoded string, truncated at the end. In the first test, a single character is encoded, and the observed output is "\u"; on Microsoft .NET, this code executes and outputs "\u00e9". In the second text, multiple characters are encoded, and the observed output is the truncated "\u00e9\u00e9\u00e9\u", appearing partially correct and partially missing.
I've inspected mcs/class/corlib/System.Text/ASCIIEncoding.cs to attempt to spot the problem to provide some more detailed help. My first thought is that I see "fallback_chars.Length" being used on line 163, but fallback_chars.Length may not match buffer.Remaining if fallback_chars wasn't allocated on line 159; although I don't see how this would cause the effect being seen.
My next thought is that as InternalGetBytes calls it's own GetBytes method, it passes "ref fallback_chars" as the fallback_chars parameter (line 164). However, fallback_chars will be both the "chars" and "fallback_chars" parameter in the resulting call to InternalGetBytes. If fallback_chars is then modified by line 161, it may actually be modifying the same array that it's using to copy characters out of. That seems like a potential problem, albeit again I can't see exactly how it'd cause the reported issue.
Fixed in Mono 4.0