From 902f09a4f8d1ef99c570455c8f56635499b45fda Mon Sep 17 00:00:00 2001 From: Joseph Hickey Date: Fri, 30 Oct 2020 16:54:57 -0400 Subject: [PATCH] Fix #637, fix OS_ModuleUnload for static modules Ensure that the handle is not NULL before invoking dlclose(). In particular the handle will be NULL for static modules. --- src/os/portable/os-impl-posix-dl-loader.c | 21 ++++++++++++--------- src/os/rtems/src/os-impl-loader.c | 21 ++++++++++++--------- 2 files changed, 24 insertions(+), 18 deletions(-) diff --git a/src/os/portable/os-impl-posix-dl-loader.c b/src/os/portable/os-impl-posix-dl-loader.c index 55aa9ac3c..ff8c3e513 100644 --- a/src/os/portable/os-impl-posix-dl-loader.c +++ b/src/os/portable/os-impl-posix-dl-loader.c @@ -90,20 +90,23 @@ int32 OS_ModuleLoad_Impl(uint32 module_id, const char *translated_path) *-----------------------------------------------------------------*/ int32 OS_ModuleUnload_Impl(uint32 module_id) { - int32 status = OS_ERROR; + int32 status = OS_SUCCESS; /* ** Attempt to close/unload the module */ - dlerror(); - if (dlclose(OS_impl_module_table[module_id].dl_handle) == 0) - { - OS_impl_module_table[module_id].dl_handle = NULL; - status = OS_SUCCESS; - } - else + if (OS_impl_module_table[module_id].dl_handle != NULL) { - OS_DEBUG("Error unloading shared library: %s\n", dlerror()); + dlerror(); + if (dlclose(OS_impl_module_table[module_id].dl_handle) == 0) + { + OS_impl_module_table[module_id].dl_handle = NULL; + } + else + { + OS_DEBUG("Error unloading shared library: %s\n", dlerror()); + status = OS_ERROR; + } } return status; diff --git a/src/os/rtems/src/os-impl-loader.c b/src/os/rtems/src/os-impl-loader.c index 89fbbedce..2fd7412c6 100644 --- a/src/os/rtems/src/os-impl-loader.c +++ b/src/os/rtems/src/os-impl-loader.c @@ -179,20 +179,23 @@ int32 OS_ModuleLoad_Impl(uint32 module_id, const char *translated_path) *-----------------------------------------------------------------*/ int32 OS_ModuleUnload_Impl(uint32 module_id) { - int32 status = OS_ERROR; + int32 status = OS_SUCCESS; /* ** Attempt to close/unload the module */ - dlerror(); - if (dlclose(OS_impl_module_table[module_id].dl_handle) == 0) - { - OS_impl_module_table[module_id].dl_handle = NULL; - status = OS_SUCCESS; - } - else + if (OS_impl_module_table[module_id].dl_handle != NULL) { - OS_DEBUG("Error unloading shared library: %s\n", dlerror()); + dlerror(); + if (dlclose(OS_impl_module_table[module_id].dl_handle) == 0) + { + OS_impl_module_table[module_id].dl_handle = NULL; + } + else + { + OS_DEBUG("Error unloading shared library: %s\n", dlerror()); + status = OS_ERROR; + } } return status;