[MONGOCRYPT-558] FLE2RangeFindDriverSpec_to_ciphertexts leads to double-initializing bson_t Created: 10/Mar/23  Updated: 27/Apr/23  Resolved: 27/Apr/23

Status: Closed
Project: Libmongocrypt
Component/s: None
Affects Version/s: None
Fix Version/s: 1.8.0

Type: Bug Priority: Unknown
Reporter: Zachary Espiritu Assignee: Kyle Kloberdanz
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Binding Changes: Not Needed

 Description   

In FLE2RangeFindDriverSpec_to_ciphertexts in mongocrypt-ctx-encrypt.c, a Valgrind memory leak can occur when the libbson BSON_MEMCHECK compile flag is set because:

  • bson_t with_placholders is initialized on line 1730 (as of commit 2ec9c3) with BSON_INITIALIZER
  • The function calls mc_FLE2RangeFindDriverSpec_to_placeholders on line 1768 and passes with_placholders as the out argument.
  • mc_FLE2RangeFindDriverSpec_to_placeholders calls bson_init on out on line 508

One possible solution is to remove the extra call to bson_init within the mc_FLE2RangeFindDriverSpec_to_placeholders function. Note that there is a test case in test-mc-fle2-rfds.c that uses mc_FLE2RangeFindDriverSpec_to_placeholders and the out argument passed to that test function will need to be initialized if this change is made.

To reproduce the memory leak, use

mkdir cmake-build && cd cmake-build
cmake ../
make
valgrind --leak-check=full ./test-mongocrypt 



 Comments   
Comment by Githook User [ 27/Apr/23 ]

Author:

{'name': 'Kyle Kloberdanz', 'email': 'kyle.kloberdanz@mongodb.com', 'username': 'kkloberdanz'}

Message: Improve bson_t initialization behavior (#629)

MONGOCRYPT-558
MONGOCRYPT-559
MONGOCRYPT-560

This addresses the issues brought up in the above tickets. We will not be removing calls to `BSON_INITIALIZER`, but we are addressing the other issues uncovered in these tickets.

Co-authored-by: Kevin Albertson <kevin.albertson@10gen.com>
Branch: master
https://github.com/mongodb/libmongocrypt/commit/ec15461d69cd740d9b8b5f195fc920a2347005dd

Generated at Thu Feb 08 09:08:57 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.