Skip to content

Commit 792beee

Browse files
authored
MONGOCRYPT-576 Include the error from mcr_dll_open in hard error (#638)
* add a failing test * add optional status to _try_load_csfle
1 parent 1525897 commit 792beee

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

src/mongocrypt.c

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -362,8 +362,10 @@ typedef struct {
362362
/**
363363
* @brief Attempt to open the CSFLE dynamic library and initialize a vtable for
364364
* it.
365+
*
366+
* @param status is an optional status to set an error message if `mcr_dll_open` fails.
365367
*/
366-
static _loaded_csfle _try_load_csfle(const char *filepath, _mongocrypt_log_t *log) {
368+
static _loaded_csfle _try_load_csfle(const char *filepath, _mongocrypt_log_t *log, mongocrypt_status_t *status) {
367369
// Try to open the dynamic lib
368370
mcr_dll lib = mcr_dll_open(filepath);
369371
// Check for errors, which are represented by strings
@@ -374,6 +376,7 @@ static _loaded_csfle _try_load_csfle(const char *filepath, _mongocrypt_log_t *lo
374376
"Error while opening candidate for CSFLE dynamic library [%s]: %s",
375377
filepath,
376378
lib.error_string.data);
379+
CLIENT_ERR("Error while opening candidate for CSFLE dynamic library [%s]: %s", filepath, lib.error_string.data);
377380
// Free resources, which will include the error string
378381
mcr_dll_close(lib);
379382
// Bad:
@@ -476,7 +479,7 @@ static _loaded_csfle _try_find_csfle(mongocrypt_t *crypt) {
476479
// Do not allow a plain filename to go through, as that will cause the
477480
// DLL load to search the system.
478481
mstr_assign(&csfle_cand_filepath, mpath_absolute(csfle_cand_filepath.view, MPATH_NATIVE));
479-
candidate_csfle = _try_load_csfle(csfle_cand_filepath.data, &crypt->log);
482+
candidate_csfle = _try_load_csfle(csfle_cand_filepath.data, &crypt->log, crypt->status);
480483
}
481484
} else {
482485
// No override path was specified, so try to find it on the provided
@@ -498,7 +501,7 @@ static _loaded_csfle _try_find_csfle(mongocrypt_t *crypt) {
498501
}
499502
}
500503
// Try to load the file:
501-
candidate_csfle = _try_load_csfle(csfle_cand_filepath.data, &crypt->log);
504+
candidate_csfle = _try_load_csfle(csfle_cand_filepath.data, &crypt->log, NULL /* status */);
502505
if (candidate_csfle.okay) {
503506
// Stop searching:
504507
break;
@@ -817,9 +820,11 @@ static bool _try_enable_csfle(mongocrypt_t *crypt) {
817820
// If a crypt_shared override path was specified, but we did not succeed in
818821
// loading crypt_shared, that is a hard-error.
819822
if (crypt->opts.crypt_shared_lib_override_path.data && !found.okay) {
820-
CLIENT_ERR("A crypt_shared override path was specified [%s], but we failed to "
821-
"open a dynamic library at that location",
822-
crypt->opts.crypt_shared_lib_override_path.data);
823+
// Wrap error with additional information.
824+
CLIENT_ERR("A crypt_shared override path was specified [%s], but we failed to open a dynamic "
825+
"library at that location. Load error: [%s]",
826+
crypt->opts.crypt_shared_lib_override_path.data,
827+
mongocrypt_status_message(crypt->status, NULL /* len */));
823828
return false;
824829
}
825830

test/test-mongocrypt-csfle-lib.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,18 @@ static void _test_csfle_not_loaded_with_bypassqueryanalysis(_mongocrypt_tester_t
158158
mongocrypt_destroy(crypt);
159159
}
160160

161+
// _test_override_error_includes_reason test changes of MONGOCRYPT-576: the error message from mcr_dll_open is
162+
// propagated.
163+
static void _test_override_error_includes_reason(_mongocrypt_tester_t *tester) {
164+
mongocrypt_t *crypt = get_test_mongocrypt(tester);
165+
// Set an incorrect override path.
166+
mongocrypt_setopt_set_crypt_shared_lib_path_override(crypt, "invalid_path_to_crypt_shared.so");
167+
ASSERT_FAILS(mongocrypt_init(crypt), crypt, "Error while opening candidate");
168+
BSON_ASSERT(mongocrypt_crypt_shared_lib_version_string(crypt, NULL) == NULL);
169+
BSON_ASSERT(mongocrypt_crypt_shared_lib_version(crypt) == 0);
170+
mongocrypt_destroy(crypt);
171+
}
172+
161173
void _mongocrypt_tester_install_csfle_lib(_mongocrypt_tester_t *tester) {
162174
INSTALL_TEST(_test_csfle_no_paths);
163175
INSTALL_TEST(_test_csfle_not_found);
@@ -168,4 +180,5 @@ void _mongocrypt_tester_install_csfle_lib(_mongocrypt_tester_t *tester) {
168180
INSTALL_TEST(_test_csfle_path_override_fail);
169181
INSTALL_TEST(_test_cur_exe_path);
170182
INSTALL_TEST(_test_csfle_not_loaded_with_bypassqueryanalysis);
183+
INSTALL_TEST(_test_override_error_includes_reason);
171184
}

0 commit comments

Comments
 (0)