Skip to content

CodeDom - method invocation on a string with length in a range of 80 to 256 chars or above 1500 chars #91060

Closed

Description

@eValker

Description

Hello,

I think I found a bug in CSharp's CodeDom Provider.
I created a CodeDom statement that tries to invoke a method on a long string.
For some strings that meet the criteria below, the generator fails to generate a valid code.

I am getting an invalid result for a string that has a length:

  • between 80 and 256 chars
  • above 1500 chars

I think the problem is how the Microsoft.CSharp.CSharpCodeProvider.QuoteSnippetStringCStyle() method works. It does not consider the context of the generation process and splits strings into multiple lines.

Reproduction Steps

I prepared a sample code that reproduces a problem:

using System.CodeDom;
using System.CodeDom.Compiler;

var statement = new CodeAssignStatement(
        new CodeFieldReferenceExpression
        {
            FieldName = "Value",
            TargetObject = new CodeTypeReferenceExpression("PF")
        },
        new CodeMethodInvokeExpression
        {
            Parameters =
            {
                new CodePrimitiveExpression('|')
            },
            Method = new CodeMethodReferenceExpression
            {
                MethodName = "MethodName",
                //TargetObject = new CodePrimitiveExpression(Sample_StringBelow80Chars()), // ok
                TargetObject = new CodePrimitiveExpression(Sample_StringAbove80Chars()), // invalid code
                //TargetObject = new CodePrimitiveExpression(Sample_StringBelow256Chars()), // invalid code
                //TargetObject = new CodePrimitiveExpression(Sample_StringAbove256Chars()), // ok
                //TargetObject = new CodePrimitiveExpression(Sample_StringAbove1500Chars()), // invalid code
            }
        }
);

var codeProvider = CodeDomProvider.CreateProvider("CSharp");

using TextWriter m = new StringWriter();
codeProvider.GenerateCodeFromStatement(statement, m, new CodeGeneratorOptions());

Console.WriteLine(m.ToString());


string Sample_StringBelow80Chars() => new string('*', 80);
// result:
// PF.Value = "********************************************************************************".MethodName('|');

string Sample_StringAbove80Chars() => new string('*', 82);
// result:
// PF.Value = "*********************************************************************************" +
//  "*".MethodName('|');

string Sample_StringBelow256Chars() => new string('*', 170);
// result:
//  PF.Value = "*********************************************************************************" +
//             "********************************************************************************" +
//             "*********".MethodName('|');

string Sample_StringAbove256Chars() => new string('*', 256);
// result:
// PF.Value = @"****************************************************************************************************************************************************************************************************************************************************************".MethodName('|');

string Sample_StringAbove1500Chars() => new string('*', 1501);
// result: invalid

Expected behavior

The method should be invoked on the whole string.

Actual behavior

For some cases (described above), the method is invoked only on the last part of the string.

Also, if the method happens to return something that cannot be converted to a string, it will fail the compilation process of the generated code.

Regression?

I tested it on CodeDom library versions:

  • 4.0.0 (.NET Framework)
  • 4.4.0 (.NET 7)
  • 7.0.0 (.NET 7)
  • 8.0.0-preview (.NET 7)

It fails to generate valid code for every version.

Known Workarounds

If you generate code that wraps a string, like Convert.ToString() method invocation, it works fine.

Configuration

  • NET 7
  • Windows OS, Visual Studio IDE
  • x64

Other information

No response

Activity

  1. ghost added
    needs-area-label
    on Aug 24, 2023
  2. ghost added
    untriaged
    on Aug 24, 2023
  3. added and removed
    needs-area-label
    on Aug 25, 2023
  4. steveharter commented on Aug 25, 2023

    @steveharter
    Contributor

    Verified the repro - string concat using + when it shouldn't be doing that.

  5. added this to the 9.0.0 milestone on Aug 25, 2023
  6. ghost removed
    untriaged
    on Aug 25, 2023
  7. added
    untriaged
    on Aug 25, 2023
  8. ghost removed
    untriaged
    on Aug 25, 2023
  9. added
    help wanted
    on Aug 25, 2023
  10. ghost added
    in-pr
    on Jan 13, 2024
  11. ghost removed
    in-pr
    on Feb 14, 2024
  12. locked and limited conversation to collaborators on Mar 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.CodeDombughelp wanted

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions