fix: avoid OverflowException when alignment width is int.MinValue
Parsing a message template whose alignment is "-2147483648" (int.MinValue) threw System.OverflowException from Math.Abs, violating the documented contract that syntactically invalid templates yield text tokens rather than throwing. Treat that alignment as text, matching how other alignment values that cannot form a valid width are already handled.
This commit is contained in:
1 parent
ec7d201361
commit
2125154397
2 files changed
+12
No files matched your search
@@ -150,6 +150,11 @@ public class MessageTemplateParser : IMessageTemplateParser
|
||||
if (!int.TryParse(alignment, NumberStyles.AllowLeadingSign, CultureInfo.InvariantCulture, out var width))
|
||||
return new TextToken(rawText);
|
||||
|
||||
// A width of int.MinValue has no positive counterpart, so its magnitude cannot be
|
||||
// represented as an alignment width; treat it as text rather than overflowing below.
|
||||
if (width == int.MinValue)
|
||||
return new TextToken(rawText);
|
||||
|
||||
var hasDash = alignment[0] == '-';
|
||||
var direction = hasDash ? AlignmentDirection.Left : AlignmentDirection.Right;
|
||||
|
||||
|
||||
@@ -379,6 +379,13 @@ public class MessageTemplateParserTests
|
||||
new TextToken("{Hello,10-1}"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AlignmentOfIntMinValueIsParsedAsText()
|
||||
{
|
||||
AssertParsedAs("{Hello,-2147483648}",
|
||||
new TextToken("{Hello,-2147483648}"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void EmptyAlignmentIsParsedAsText()
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user