Skip to content

Commit

Permalink
CSHARP-5150: Coverity analysis defect 102677: Copy-paste error.
Browse files Browse the repository at this point in the history
  • Loading branch information
rstam committed Jun 27, 2024
1 parent 4eaf203 commit d09a511
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/MongoDB.Bson/IO/JsonReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1961,13 +1961,13 @@ private BsonValue ParseTimestampConstructor()
VerifyToken(",");
int increment;
var incrementToken = PopToken();
if (secondsSinceEpochToken.IsNumber)
if (incrementToken.IsNumber)
{
increment = incrementToken.Int32Value;
}
else
{
var message = string.Format("JSON reader expected a number but found '{0}'.", secondsSinceEpochToken.Lexeme);
var message = string.Format("JSON reader expected a number but found '{0}'.", incrementToken.Lexeme);
throw new FormatException(message);
}
VerifyToken(")");
Expand Down
20 changes: 20 additions & 0 deletions tests/MongoDB.Bson.Tests/IO/JsonReaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1396,6 +1396,26 @@ public void TestTimestampConstructor()
Assert.Equal(json, BsonSerializer.Deserialize<BsonTimestamp>(new StringReader(json)).ToJson());
}

[Theory]
// truncated input
[InlineData("Timestamp(")]
[InlineData("Timestamp()")]
[InlineData("Timestamp(1")]
[InlineData("Timestamp(1,")]
[InlineData("Timestamp(1, 2")]
// valid JSON but not a valid extended JSON BsonTimestamp
[InlineData("Timestamp('abc', 2)")]
[InlineData("Timestamp(2, 'abc')")]
public void TestTimestampConstructorWhenInvalid(string json)
{
using (_bsonReader = new JsonReader(json))
{
var exception = Record.Exception(() => _bsonReader.ReadBsonType());

exception.Should().BeOfType<FormatException>();
}
}

[Theory]
[InlineData("{ \"$timestamp\" : { \"t\" : 1, \"i\" : 2 } }")]
[InlineData("{ \"$timestamp\" : { \"i\" : 2, \"t\" : 1 } }")]
Expand Down

0 comments on commit d09a511

Please sign in to comment.