mongodb: fix iterator crashes
authorAlexandre Terrasa <alexandre@moz-code.org>
Sun, 29 Nov 2015 22:42:30 +0000 (17:42 -0500)
committerAlexandre Terrasa <alexandre@moz-code.org>
Sun, 29 Nov 2015 22:43:08 +0000 (17:43 -0500)
Before this PR, the iterator `is_ok` status relied on `mongoc_cursor_more` that can return
true even if the iterator is terminated.
This PR fixes the problem by using the result of `mongoc_cursor_next` instead.

Signed-off-by: Alexandre Terrasa <alexandre@moz-code.org>

lib/mongodb/mongodb.nit
lib/mongodb/native_mongodb.nit

index 889fc2e..fd753bd 100644 (file)
@@ -561,7 +561,10 @@ class MongoCollection
                var c = native.find(query.to_bson.native)
                if c == null then return res
                var cursor = new MongoCursor(c)
-               for item in cursor do res.add item
+               while cursor.is_ok do
+                       res.add cursor.item
+                       cursor.next
+               end
                return res
        end
 
@@ -618,9 +621,9 @@ class MongoCursor
 
        init do next
 
-       redef fun is_ok do return native.more
+       redef var is_ok = true
 
-       redef fun next do native.next
+       redef fun next do is_ok = native.next
 
        redef fun item do
                return new JsonObject.from_bson(new BSON(native.current))
index c28a218..e2e6213 100644 (file)
@@ -524,11 +524,6 @@ extern class NativeMongoCursor `{ mongoc_cursor_t* `}
                return mongoc_cursor_next(self, &doc);
        `}
 
-       # Wrapper for `mongoc_cursor_more()`.
-       #
-       # This function shall indicate if there is more data to be read from the cursor.
-       fun more: Bool `{ return mongoc_cursor_more(self); `}
-
        # Wrapper for `mongoc_cursor_destroy()`.
        #
        # This instance should not be used beyond this point!