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

Console can allocate too much on the stack with a custom encoding #68395

Closed
vcsjones opened this issue Apr 22, 2022 · 3 comments · Fixed by #68398
Closed

Console can allocate too much on the stack with a custom encoding #68395

vcsjones opened this issue Apr 22, 2022 · 3 comments · Fixed by #68398
Assignees
Milestone

Comments

@vcsjones
Copy link
Member

In the unlikely scenario where the console's input is using a custom encoding, and the GetMaxCharCount returns a large value, this can lead to a large stack allocation.

Program to reproduce (works on macOS):

using System.Text;

namespace Foo
{
    class Program
    {
        static void Main()
        {
            Console.InputEncoding = new CustomEncoding();
            Console.ReadKey(true);
        }
    }

    public class CustomEncoding : UTF8Encoding
    {
        public override int GetMaxCharCount(int byteCount) => int.MaxValue;
    }
}

The stackalloc is here:

Span<char> chars = stackalloc char[_encoding.GetMaxCharCount(buffer.Length)];

The value being fed in to the stackalloc's length should be guarded since it is dependent on external factors, in this case the encoding used by stdin.

@dotnet-issue-labeler dotnet-issue-labeler bot added the untriaged New issue has not been triaged by the area owner label Apr 22, 2022
@ghost
Copy link

ghost commented Apr 22, 2022

Tagging subscribers to this area: @dotnet/area-system-console
See info in area-owners.md if you want to be subscribed.

Issue Details

In the unlikely scenario where the console's input is using a custom encoding, and the GetMaxCharCount returns a large value, this can lead to a large stack allocation.

Program to reproduce (works on macOS):

using System.Text;

namespace Foo
{
    class Program
    {
        static void Main()
        {
            Console.InputEncoding = new CustomEncoding();
            Console.ReadKey(true);
        }
    }

    public class CustomEncoding : UTF8Encoding
    {
        public override int GetMaxCharCount(int byteCount) => int.MaxValue;
    }
}

The stackalloc is here:

Span<char> chars = stackalloc char[_encoding.GetMaxCharCount(buffer.Length)];

The value being fed in to the stackalloc's length should be guarded since it is dependent on external factors, in this case the encoding used by stdin.

Author: vcsjones
Assignees: -
Labels:

area-System.Console

Milestone: -

@stephentoub
Copy link
Member

Good catch. Are you planning to put up a PR?

@vcsjones vcsjones self-assigned this Apr 22, 2022
@vcsjones
Copy link
Member Author

@stephentoub Yep.

@ghost ghost added the in-pr There is an active PR which will close this issue when it is merged label Apr 22, 2022
@ghost ghost removed untriaged New issue has not been triaged by the area owner in-pr There is an active PR which will close this issue when it is merged labels Apr 23, 2022
@adamsitnik adamsitnik added this to the 7.0.0 milestone Apr 26, 2022
@ghost ghost locked as resolved and limited conversation to collaborators May 26, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants