Skip to content
This repository was archived by the owner on Dec 19, 2018. It is now read-only.
This repository was archived by the owner on Dec 19, 2018. It is now read-only.

CSharpCodeWriter's write string literal can fail with Mono and Roslyn. #56

@NTaylorMullen

Description

@NTaylorMullen

Original Issue: https://aspnetwebstack.codeplex.com/workitem/1885

We are using razor to generate C# code for web pages, and on some pages there is very much content.

The method that chooses how to write out the text chooses to write out the string using WriteVerbatimStringLiteral only if the length is equal to or less than 1500 characters. The comment says this is because it may be too long to fit in one line, but it does not check the actual length of each line.
What should have ended up as a nicely formatted @"" block across multiple lines got turned into "~80chars" + repeated many many times.

        public override void WriteStringLiteral(string literal)
        {
            if (literal == null)
            {
                throw new ArgumentNullException("literal");
            }

            // From CSharpCodeProvider in CodeDOM
            //  If the string is short, use C style quoting (e.g "\r\n")
            //  Also do it if it is too long to fit in one line
            //  If the string contains '\0', verbatim style won't work.
            if (literal.Length >= 256 && literal.Length <= 1500 && literal.IndexOf('\0') == -1)
            {
                WriteVerbatimStringLiteral(literal);
            }
            else
            {
                WriteCStyleStringLiteral(literal);
            }
        }

This causes a problem when we try to compile the code using Mono.CSharp.Evaluator (see the issue I submitted about the StackOverflowException the generated code causes: https://bugzilla.xamarin.com/show_bug.cgi?id=19332)

Currently I either have to use a modified razor dll which only has the literal.IndexOf('\0') == -1 check, or increase the stack size on the thread that compiles the code and hope the content never reaches the new limit.

Maybe this check could be updated to prefer WriteVerbatimStringLiteral if there are line breaks in the string, but check that none of the lines exceed 1500 characters?

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions