diff --git a/src/MongoDB.Bson/IO/JsonReader.cs b/src/MongoDB.Bson/IO/JsonReader.cs index 54b98d08eba..f6480420eb9 100644 --- a/src/MongoDB.Bson/IO/JsonReader.cs +++ b/src/MongoDB.Bson/IO/JsonReader.cs @@ -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(")"); diff --git a/tests/MongoDB.Bson.Tests/IO/JsonReaderTests.cs b/tests/MongoDB.Bson.Tests/IO/JsonReaderTests.cs index 76c31fa01cd..4cf30d92361 100644 --- a/tests/MongoDB.Bson.Tests/IO/JsonReaderTests.cs +++ b/tests/MongoDB.Bson.Tests/IO/JsonReaderTests.cs @@ -1396,6 +1396,26 @@ public void TestTimestampConstructor() Assert.Equal(json, BsonSerializer.Deserialize(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(); + } + } + [Theory] [InlineData("{ \"$timestamp\" : { \"t\" : 1, \"i\" : 2 } }")] [InlineData("{ \"$timestamp\" : { \"i\" : 2, \"t\" : 1 } }")]