Bug 45915 - I18N: ByteEncoding.GetMaxByteCount ignores EncoderFallback.MaxCharCount
Summary: I18N: ByteEncoding.GetMaxByteCount ignores EncoderFallback.MaxCharCount
Status: NEW
Alias: None
Product: Class Libraries
Classification: Mono
Component: General (show other bugs)
Version: master
Hardware: PC All
: --- normal
Target Milestone: Untriaged
Assignee: Alexander Köplinger [MSFT]
URL:
Depends on:
Blocks:
 
Reported: 2016-10-26 12:39 UTC by Oleg
Modified: 2017-02-06 10:40 UTC (History)
4 users (show)

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


Attachments

Description Oleg 2016-10-26 12:39:19 UTC
ByteEncoding.GetMaxByteCount ignores EncoderFallback.MaxCharCount.

Code to reproduce:

    private static void Reproduce()
    {
        var encoding = Encoding.GetEncoding(1251, new MyEncoderFallback(), DecoderFallback.ExceptionFallback);
        Console.WriteLine(encoding.GetMaxByteCount(1024).ToString());
    }

    private class MyEncoderFallback : EncoderFallback
    {
        public override EncoderFallbackBuffer CreateFallbackBuffer()
        {
            return new MyEncoderFallbackBuffer();
        }

        public override int MaxCharCount => 16;
    }

    private class MyEncoderFallbackBuffer : EncoderFallbackBuffer
    {
        public override int Remaining => 0;

        public override bool Fallback(char charUnknown, int index)
        {
            return true;
        }

        public override bool Fallback(char charUnknownHigh, char charUnknownLow, int index)
        {
            return true;
        }

        public override char GetNextChar()
        {
            return '\0';
        }

        public override bool MovePrevious()
        {
            return false;
        }
    }

It will print 1024 on Mono, 16400 on Windows .NET.

This can cause "Reading would overrun buffer" exception while using StreamWriter with this kind of encodings.

Note You need to log in before you can comment on or make changes to this bug.