From dee59a306798aa740e94ccb51fdf744e6c41b92c Mon Sep 17 00:00:00 2001 From: jacobkeeler Date: Mon, 30 Oct 2017 15:57:53 -0400 Subject: [PATCH] Fixes segfault in `bson_object_iterator_next` Updating description according to new behavior. --- src/bson_object.c | 10 ++++++++-- src/bson_object.h | 3 ++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/bson_object.c b/src/bson_object.c index b517789..2bc6d27 100644 --- a/src/bson_object.c +++ b/src/bson_object.c @@ -398,7 +398,13 @@ MapIterator bson_object_iterator(BsonObject *obj) { BsonObjectEntry bson_object_iterator_next(MapIterator *iterator) { MapEntry *entry = emhashmap_iterator_next(iterator); BsonObjectEntry bsonEntry; - strncpy(bsonEntry.key, entry->key, 255); - bsonEntry.element = (BsonElement *)entry->value; + if(entry == NULL) { + bsonEntry.key[0] = 0x00; //Assign empty string + bsonEntry.element = NULL; + } + else { + strncpy(bsonEntry.key, entry->key, 255); + bsonEntry.element = (BsonElement *)entry->value; + } return bsonEntry; } \ No newline at end of file diff --git a/src/bson_object.h b/src/bson_object.h index 7717931..847abc8 100644 --- a/src/bson_object.h +++ b/src/bson_object.h @@ -279,7 +279,8 @@ MapIterator bson_object_iterator(BsonObject *obj); @param iterator - The iterator to be advanced @return - The next BSON object entry in the object if it exists, - NULL if the iterator has moved past the end of the entry list + an entry with a NULL element if the iterator has moved past the end + of the entry list */ BsonObjectEntry bson_object_iterator_next(MapIterator *iterator);