From d09a511a668c24008f8a55de0089739b2249a412 Mon Sep 17 00:00:00 2001 From: rstam Date: Mon, 17 Jun 2024 14:48:37 -0700 Subject: [PATCH] CSHARP-5150: Coverity analysis defect 102677: Copy-paste error. --- src/MongoDB.Bson/IO/JsonReader.cs | 4 ++-- .../MongoDB.Bson.Tests/IO/JsonReaderTests.cs | 20 +++++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) 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 } }")]