-
Type: Bug
-
Resolution: Done
-
Priority: Blocker - P1
-
Affects Version/s: 1.8.3
-
Component/s: Serialization
-
Minor Change
We have been running into a pretty bad bug where we noticed some objects were failing to be inserted into mongo. We were getting the following stack trace:
System.IndexOutOfRangeException
Index was outside the bounds of the array.
at MongoDB.Bson.IO.BsonWriter.CheckElementName(String name)
at MongoDB.Bson.IO.BsonWriter.WriteName(String name)
at MongoDB.Bson.Serialization.Serializers.DictionarySerializer`2.Serialize(BsonWriter bsonWriter, Type nominalType, Object value, IBsonSerializationOptions options)
at MongoDB.Bson.Serialization.BsonClassMapSerializer.SerializeMember(BsonWriter bsonWriter, Object obj, BsonMemberMap memberMap)
at MongoDB.Bson.Serialization.BsonClassMapSerializer.Serialize(BsonWriter bsonWriter, Type nominalType, Object value, IBsonSerializationOptions options)
at MongoDB.Bson.Serialization.BsonClassMapSerializer.SerializeMember(BsonWriter bsonWriter, Object obj, BsonMemberMap memberMap)
at MongoDB.Bson.Serialization.BsonClassMapSerializer.Serialize(BsonWriter bsonWriter, Type nominalType, Object value, IBsonSerializationOptions options)
at MongoDB.Driver.Internal.MongoInsertMessage.AddDocument(BsonBuffer buffer, Type nominalType, Object document)
at MongoDB.Driver.Operations.InsertOperation.Execute(MongoConnection connection)
at MongoDB.Driver.MongoCollection.InsertBatch(Type nominalType, IEnumerable documents, MongoInsertOptions options)
at MongoDB.Driver.MongoCollection.Insert(Type nominalType, Object document, MongoInsertOptions options)
When researching this issue we came across this: https://jira.mongodb.org/browse/CSHARP-624, which seemed like it was the issue but we were still experiencing it...
Upon looking at the code... We tracked this down to the CheckElementName name is not checking for an empty string (just null), it's just trying to access the first character (which there are none). The following unit test reproduces this issue in the smallest amount of code possible.
[Test]
public void TestEmptyDictionaryKeySerialization() {
var dictionary = new Dictionary<string, string>() {
,
{ "", "emptyKey" }};
using (var buffer = new BsonBuffer()) {
using (var bsonWriter = BsonWriter.Create(buffer, new BsonBinaryWriterSettings()))
}
}
- is duplicated by
-
CSHARP-880 System.IndexOutOfRange Exception in Check
- Closed
- related to
-
SERVER-2051 Disallow empty string keys
- Open