Uploaded image for project: 'C Driver'
  1. C Driver
  2. CDRIVER-1029

mongoc_collection_find_and_modify will cause memory leakage if reply is created by bson_new()

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 1.4.0
    • Affects Version/s: 1.2.0, 1.2.1
    • Component/s: Docs, libmongoc
    • Environment:
      windows 7 sp1
      vs2013 w update 4
      mongodb 3.0.3
      mongo c driver 1.2.1 / 1.2.0

      in function: mongoc_collection_find_and_modify
      if reply is created by bson_new(), will cause memory leakage.

      code:
      use example in
      http://api.mongodb.org/c/current/mongoc_collection_find_and_modify.html
      just replace

        bson_t reply;
      

      with

        bson_t* reply = bson_new();
      

      then will cause memory leakage:

        bson_t* reply = bson_new()
      ...
        if (!mongoc_collection_find_and_modify (collection, query, NULL, update, NULL, false, false, true, reply, &error)) 
      ...
        bson_destroy (reply);
      ...
      
      

      reason:
      mongoc_collection_find_and_modify use bson_copy_to() or bson_init() to set reply. These two function used with bson_t object created by bson_new() will cause memory leakage.

      eg:
      1.

      bson_t * doc = bson_new();
      bson_init(doc);
      bson_destroy(doc);
      

      2.

      bson_t * src= bson_new();
      bson_t * dst= bson_new();
      bson_copy_to(src, dst);
      bson_destroy(src);
      bson_destroy(dst);
      

      both code piece of code will cause memory leakage.

            Assignee:
            jesse@mongodb.com A. Jesse Jiryu Davis
            Reporter:
            jeson Jeson Chen
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: