Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/Tomlyn.Tests/BasicTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,18 @@ public void TestEmptyComment()
Assert.AreEqual(input, docAsStr);
}

[Test]
public void TestIntegerOverflowIsRejected()
{
// TOML requires an error when an integer cannot be represented as a
// signed 64-bit value; a positive literal in [2^63, 2^64-1] must not
// silently wrap to a negative long.
Assert.IsFalse(SyntaxParser.Parse("a = 9223372036854775807").HasErrors); // long.MaxValue
Assert.IsFalse(SyntaxParser.Parse("a = -9223372036854775808").HasErrors); // long.MinValue
Assert.IsTrue(SyntaxParser.Parse("a = 9223372036854775808").HasErrors); // 2^63
Assert.IsTrue(SyntaxParser.Parse("a = 18446744073709551615").HasErrors); // 2^64-1
}

[Test]
public void SimpleTest()
{
Expand Down
9 changes: 9 additions & 0 deletions src/Tomlyn/Parsing/Lexer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1054,6 +1054,15 @@ private bool TryParseDecimalInt64(out long value)
return true;
}

// A positive magnitude above long.MaxValue does not fit a signed 64-bit
// integer; the negative branch already rejects its counterpart. Without
// this, values in [2^63, 2^64-1] wrap to a negative long (TOML requires
// an error when an integer cannot be represented losslessly).
if (accumulator > long.MaxValue)
{
return false;
}

value = unchecked((long)accumulator);
return true;
}
Expand Down