Bug 24008 - Custom EncoderFallbackBuffer with multiple characters has output cut off
Summary: Custom EncoderFallbackBuffer with multiple characters has output cut off
Alias: None
Product: Class Libraries
Classification: Mono
Component: mscorlib ()
Version: unspecified
Hardware: PC Linux
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
Depends on:
Reported: 2014-10-22 20:17 UTC by Mathieu Fenniak
Modified: 2015-03-30 10:03 UTC (History)
2 users (show)

Is this bug a regression?: ---
Last known good build:

Demonstration console app (3.96 KB, text/x-csharp)
2014-10-22 20:17 UTC, Mathieu Fenniak

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and 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 Links.

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.

Related Links:

Description Mathieu Fenniak 2014-10-22 20:17:34 UTC
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.
Comment 1 Mathieu Fenniak 2014-10-22 20:21:16 UTC
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.
Comment 2 Marek Safar 2015-03-30 10:03:20 UTC
Fixed in Mono 4.0