Description
StringBuilder and ValueStringBuilder impose limits on Index and Width, but CompositeFormat.Parse does not, resulting in inconsistent behavior. Since CompositeFormat.Parse also triggers a CA2241 warning, this creates a discrepancy with the analyzer as well.
|
// Undocumented exclusive limits on the range for Argument Hole Index and Argument Hole Alignment. |
|
const int IndexLimit = 1_000_000; // Note: 0 <= ArgIndex < IndexLimit |
|
const int WidthLimit = 1_000_000; // Note: -WidthLimit < ArgAlign < WidthLimit |
|
// Undocumented exclusive limits on the range for Argument Hole Index and Argument Hole Alignment. |
|
const int IndexLimit = 1_000_000; // Note: 0 <= ArgIndex < IndexLimit |
|
const int WidthLimit = 1_000_000; // Note: -WidthLimit < ArgAlign < WidthLimit |
|
private static bool TryParseLiterals(ReadOnlySpan<char> format, List<(string? Literal, int ArgIndex, int Alignment, string? Format)> segments, ref int failureOffset, ref ExceptionResource failureReason) |
Reproduction Steps
var numbers = Enumerable.Range(0, 100000000).Cast<object>().ToArray();
Run("Index= 9999999:CompositeFormat", () => string.Format(null, CompositeFormat.Parse("{9999999}"), numbers).Length);
Run("Index=10000000:CompositeFormat", () => string.Format(null, CompositeFormat.Parse("{10000000}"), numbers).Length);
Run("Index= 9999999:string ", () => string.Format(null, "{9999999}", numbers).Length);
Run("Index=10000000:string ", () => string.Format(null, "{10000000}", numbers).Length);
Console.WriteLine();
Run("Width= 9999999:CompositeFormat", () => string.Format(null, CompositeFormat.Parse("{0,9999999}"), "format").Length);
Run("Width=10000000:CompositeFormat", () => string.Format(null, CompositeFormat.Parse("{0,10000000}"), "format").Length);
Run("Width= 9999999:string ", () => string.Format(null, "{0,9999999}", "format").Length);
Run("Width=10000000:string ", () => string.Format(null, "{0,10000000}", "format").Length);
void Run(string name, Func<object> func)
{
try
{
Console.WriteLine($"{name}: {func()}");
}
catch (Exception e)
{
Console.WriteLine($"{name}: {e.GetType()}: {e.Message}");
}
}
Expected behavior
Index= 9999999:CompositeFormat: 7
Index=10000000:CompositeFormat: System.FormatException: Input string was not in a correct format. Failure to parse near offset 10. Format item ends prematurely.
Index= 9999999:string : 7
Index=10000000:string : System.FormatException: Input string was not in a correct format. Failure to parse near offset 8. Format item ends prematurely.
Width= 9999999:CompositeFormat: 9999999
Width=10000000:CompositeFormat: System.FormatException: Input string was not in a correct format. Failure to parse near offset 10. Format item ends prematurely.
Width= 9999999:string : 9999999
Width=10000000:string : System.FormatException: Input string was not in a correct format. Failure to parse near offset 10. Format item ends prematurely.
Actual behavior
Index= 9999999:CompositeFormat: 7
Index=10000000:CompositeFormat: 8
Index= 9999999:string : 7
Index=10000000:string : System.FormatException: Input string was not in a correct format. Failure to parse near offset 8. Format item ends prematurely.
Width= 9999999:CompositeFormat: 9999999
Width=10000000:CompositeFormat: 10000000
Width= 9999999:string : 9999999
Width=10000000:string : System.FormatException: Input string was not in a correct format. Failure to parse near offset 10. Format item ends prematurely.
Regression?
No response
Known Workarounds
No response
Configuration
.NET 8 or later
Other information
No response
Description
StringBuilderandValueStringBuilderimpose limits onIndexandWidth, butCompositeFormat.Parsedoes not, resulting in inconsistent behavior. SinceCompositeFormat.Parsealso triggers a CA2241 warning, this creates a discrepancy with the analyzer as well.runtime/src/libraries/System.Private.CoreLib/src/System/Text/StringBuilder.cs
Lines 1523 to 1525 in 54a230f
runtime/src/libraries/System.Private.CoreLib/src/System/Text/ValueStringBuilder.AppendFormat.cs
Lines 18 to 20 in 54a230f
runtime/src/libraries/System.Private.CoreLib/src/System/Text/CompositeFormat.cs
Line 112 in 54a230f
Reproduction Steps
Expected behavior
Actual behavior
Regression?
No response
Known Workarounds
No response
Configuration
.NET 8 or later
Other information
No response