[CDRIVER-2382] Clarify calling codes cleanup responsibilities when mongoc_collection_find_and_modify returns false Created: 17/Nov/17  Updated: 28/Oct/23  Resolved: 19/Nov/17

Status: Closed
Project: C Driver
Component/s: None
Affects Version/s: 1.4.1
Fix Version/s: 1.9.0

Type: Improvement Priority: Major - P3
Reporter: Graham Whitted Assignee: A. Jesse Jiryu Davis
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

Prior to CDRIVER-1381 the reply bson_t was not initialized if mongoc_collection_find_and_modify() returned false. To prevent badness client code needed to effectively do something like:

old way

   bool ok;
   bson_t reply;
 
   ok = mongoc_collection_find_and_modify (collection, query, NULL, update, NULL, false, false, true, &reply, &error);
 
   if(ok) {
       bson_destroy(&reply);
   }

Now the reply bson_t is always initialized regardless of the return value of mongoc_collection_find_and_modify(). Based on the comment for the change it does not seem like this was intended. Is this new behavior a bug? Should client code like the code above? Or like the code below?

new way?

   bool ok;
   bson_t reply;
 
   ok = mongoc_collection_find_and_modify (collection, query, NULL, update, NULL, false, false, true, &reply, &error);
 
   bson_destroy(&reply);



 Comments   
Comment by Githook User [ 19/Nov/17 ]

Author:

{'name': 'A. Jesse Jiryu Davis', 'username': 'ajdavis', 'email': 'jesse@mongodb.com'}

Message: CDRIVER-2382 docs: findandmodify always inits reply
Branch: master
https://github.com/mongodb/mongo-c-driver/commit/7a3d44efa3e7cfc8c01cac93e7b68ce90cef244e

Comment by A. Jesse Jiryu Davis [ 17/Nov/17 ]

Thanks for pointing this out. The reply should always be initialized if you pass a non-null bson_t *. This is consistent with other functions like mongoc_client_command_simple that always initialize the reply, even if they fail.

I'll update the docs for mongoc_collection_find_and_modify and its successor, mongoc_collection_find_and_modify_with_opts, to say that reply is always initialized and must always be destroyed.

Generated at Wed Feb 07 21:15:03 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.