Skip to content

Commit a8e01bf

Browse files
tylerzhao7684Tyler Zhao
authored andcommitted
Coverity Fix: Free allocated memory before pointer is lost
Using destructor to close MMD libraries Clang format Closing mmd_lib in destructor that is not opened globally
1 parent ad54f07 commit a8e01bf

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

src/acl_hal_mmd.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,24 @@ static void my_dlclose(void *library) {
464464
#endif
465465
}
466466

467+
static void my_dlclose_no_assert(void *library) {
468+
#ifdef _WIN32
469+
FreeLibrary((HMODULE)library);
470+
#else
471+
dlclose(library);
472+
#endif
473+
}
474+
475+
typedef struct my_dl_wrapper {
476+
void *handle;
477+
my_dl_wrapper(void *handle) { this->handle = handle; }
478+
~my_dl_wrapper() { my_dlclose_no_assert(this->handle); }
479+
// prohibit copying to avoid double-close of handle
480+
my_dl_wrapper(const my_dl_wrapper &) = delete;
481+
my_dl_wrapper &operator=(const my_dl_wrapper &) = delete;
482+
} my_dl_wrapper;
483+
std::vector<std::unique_ptr<my_dl_wrapper>> mmd_libs;
484+
467485
cl_bool l_load_board_functions(acl_mmd_dispatch_t *mmd_dispatch,
468486
const char *library_name, void *mmd_library,
469487
char *error_msg) {
@@ -632,12 +650,14 @@ cl_bool l_load_single_board_library(const char *library_name,
632650
if (result == CL_FALSE) {
633651
std::cout << "Error: Could not load board library " << library_name
634652
<< " due to failure to load symbols\n";
653+
my_dlclose(mmd_library);
635654
return result;
636655
}
637656
}
638657
++num_boards_found;
639658
}
640659

660+
mmd_libs.push_back(std::make_unique<my_dl_wrapper>(mmd_library));
641661
return CL_TRUE;
642662
}
643663

@@ -1193,6 +1213,7 @@ static acl_mmd_dispatch_t *get_msim_mmd_layer() {
11931213
return nullptr;
11941214
}
11951215
auto *sym = my_dlsym(mmd_lib, sym_name, &error_msg);
1216+
mmd_libs.push_back(std::make_unique<my_dl_wrapper>(mmd_lib));
11961217
if (!sym) {
11971218
std::cout << "Error: Symbol " << sym_name
11981219
<< " not found in simulation MMD library ";

0 commit comments

Comments
 (0)