This is an automated email from the ASF dual-hosted git repository.

pnoltes pushed a commit to branch 
hotfix/do-not-use-dlsym-with-bundle-handle-null
in repository https://gitbox.apache.org/repos/asf/celix.git

commit 90ffe52551f3d4d04623564beed6086d43a73045
Author: Pepijn Noltes <[email protected]>
AuthorDate: Sun May 7 19:55:57 2023 +0200

    Fix issue that a NULL bundle handle still got an activator.
---
 libs/framework/include/celix_constants.h |  2 +-
 libs/framework/src/celix_libloader.c     | 44 ++++++++++++++++++++++++
 libs/framework/src/celix_libloader.h     | 40 ++++++++++++++++++++--
 libs/framework/src/framework.c           | 58 +++++++++++---------------------
 4 files changed, 101 insertions(+), 43 deletions(-)

diff --git a/libs/framework/include/celix_constants.h 
b/libs/framework/include/celix_constants.h
index 4da4f59e..14a0a265 100644
--- a/libs/framework/include/celix_constants.h
+++ b/libs/framework/include/celix_constants.h
@@ -188,7 +188,7 @@ extern "C" {
  * @brief Celix framework environment property (named 
"CELIX_FRAMEWORK_CLEAN_CACHE_DIR_ON_CREATE") specifying
  * whether to delete the cache dir on framework creation.
  *
- * If not specified "true" is used.
+ * The default value is false
  */
 #define CELIX_FRAMEWORK_CLEAN_CACHE_DIR_ON_CREATE 
"CELIX_FRAMEWORK_CLEAN_CACHE_DIR_ON_CREATE"
 
diff --git a/libs/framework/src/celix_libloader.c 
b/libs/framework/src/celix_libloader.c
index 94325aca..d49ab6c0 100644
--- a/libs/framework/src/celix_libloader.c
+++ b/libs/framework/src/celix_libloader.c
@@ -53,6 +53,50 @@ void* celix_libloader_getSymbol(celix_library_handle_t 
*handle, const char *name
     return dlsym(handle, name);
 }
 
+celix_bundle_activator_create_fp 
celix_libloader_findBundleActivatorCreate(celix_library_handle_t* 
bundleActivatorHandle) {
+    celix_bundle_activator_create_fp result = NULL;
+    if (bundleActivatorHandle != NULL) {
+        result = (celix_bundle_activator_create_fp) 
dlsym(bundleActivatorHandle, CELIX_FRAMEWORK_BUNDLE_ACTIVATOR_CREATE);
+        if (result == NULL) {
+            result = (celix_bundle_activator_create_fp) 
dlsym(bundleActivatorHandle, OSGI_FRAMEWORK_DEPRECATED_BUNDLE_ACTIVATOR_CREATE);
+        }
+    }
+    return result;
+}
+
+celix_bundle_activator_start_fp 
celix_libloader_findBundleActivatorStart(celix_library_handle_t* 
bundleActivatorHandle) {
+    celix_bundle_activator_start_fp result = NULL;
+    if (bundleActivatorHandle != NULL) {
+        result = (celix_bundle_activator_start_fp) 
dlsym(bundleActivatorHandle, CELIX_FRAMEWORK_BUNDLE_ACTIVATOR_START);
+        if (result == NULL) {
+            result = (celix_bundle_activator_start_fp) 
dlsym(bundleActivatorHandle, OSGI_FRAMEWORK_DEPRECATED_BUNDLE_ACTIVATOR_START);
+        }
+    }
+    return result;
+}
+
+celix_bundle_activator_stop_fp 
celix_libloader_findBundleActivatorStop(celix_library_handle_t* 
bundleActivatorHandle) {
+    celix_bundle_activator_stop_fp result = NULL;
+    if (bundleActivatorHandle != NULL) {
+        result = (celix_bundle_activator_stop_fp) dlsym(bundleActivatorHandle, 
CELIX_FRAMEWORK_BUNDLE_ACTIVATOR_STOP);
+        if (result == NULL) {
+            result = (celix_bundle_activator_stop_fp) 
dlsym(bundleActivatorHandle, OSGI_FRAMEWORK_DEPRECATED_BUNDLE_ACTIVATOR_STOP);
+        }
+    }
+    return result;
+}
+
+celix_bundle_activator_destroy_fp 
celix_libloader_findBundleActivatorDestroy(celix_library_handle_t* 
bundleActivatorHandle) {
+    celix_bundle_activator_destroy_fp result = NULL;
+    if (bundleActivatorHandle != NULL) {
+        result = (celix_bundle_activator_destroy_fp) 
dlsym(bundleActivatorHandle, CELIX_FRAMEWORK_BUNDLE_ACTIVATOR_DESTROY);
+        if (result == NULL) {
+            result = (celix_bundle_activator_destroy_fp) 
dlsym(bundleActivatorHandle, 
OSGI_FRAMEWORK_DEPRECATED_BUNDLE_ACTIVATOR_DESTROY);
+        }
+    }
+    return result;
+}
+
 const char* celix_libloader_getLastError() {
     return dlerror();
 }
\ No newline at end of file
diff --git a/libs/framework/src/celix_libloader.h 
b/libs/framework/src/celix_libloader.h
index c454c869..b6ec0ce9 100644
--- a/libs/framework/src/celix_libloader.h
+++ b/libs/framework/src/celix_libloader.h
@@ -24,22 +24,28 @@
 
 typedef void celix_library_handle_t;
 
+typedef celix_status_t (*celix_bundle_activator_create_fp)(bundle_context_t 
*context, void **userData);
+typedef celix_status_t (*celix_bundle_activator_start_fp)(void *userData, 
bundle_context_t *context);
+typedef celix_status_t (*celix_bundle_activator_stop_fp)(void *userData, 
bundle_context_t *context);
+typedef celix_status_t (*celix_bundle_activator_destroy_fp)(void *userData, 
bundle_context_t *context);
+
+
 /**
  * @brief Load library using the provided path.
  * @return a library handle or NULL if the library could not be loaded.
  */
-celix_library_handle_t* celix_libloader_open(celix_bundle_context_t *ctx, 
const char *libPath);
+celix_library_handle_t* celix_libloader_open(celix_bundle_context_t* ctx, 
const char* libPath);
 
 /**
  * @brief Close the library
  */
-void celix_libloader_close(celix_bundle_context_t *ctx, celix_library_handle_t 
*handle);
+void celix_libloader_close(celix_bundle_context_t* ctx, 
celix_library_handle_t* handle);
 
 /**
  * @brief Get the address of a symbol with the provided name.
  * @return The symbol address of NULL if the symbol could not be found.
  */
-void* celix_libloader_getSymbol(celix_library_handle_t *handle, const char 
*name);
+void* celix_libloader_getSymbol(celix_library_handle_t* handle, const char* 
name);
 
 /**
  * @brief Returns the last celix_libloader_open error
@@ -47,6 +53,34 @@ void* celix_libloader_getSymbol(celix_library_handle_t 
*handle, const char *name
  */
 const char* celix_libloader_getLastError();
 
+/**
+ * @brief Tries to find the bundle activator create function for the provide 
bundle activator library handle.
+ * @param bundleActivatorHandle The bundle activator library handle.
+ * @return The found bundle activator create function or NULL.
+ */
+celix_bundle_activator_create_fp 
celix_libloader_findBundleActivatorCreate(celix_library_handle_t* 
bundleActivatorHandle);
+
+/**
+ * @brief Tries to find the bundle activator start function for the provide 
bundle activator library handle.
+ * @param bundleActivatorHandle The bundle activator library handle.
+ * @return The found bundle activator start function or NULL.
+ */
+celix_bundle_activator_start_fp 
celix_libloader_findBundleActivatorStart(celix_library_handle_t* 
bundleActivatorHandle);
+
+/**
+ * @brief Tries to find the bundle activator stop function for the provide 
bundle activator library handle.
+ * @param bundleActivatorHandle The bundle activator library handle.
+ * @return The found bundle activator stop function or NULL.
+ */
+celix_bundle_activator_stop_fp 
celix_libloader_findBundleActivatorStop(celix_library_handle_t* 
bundleActivatorHandle);
+
+/**
+ * @brief Tries to find the bundle activator destroy function for the provide 
bundle activator library handle.
+ * @param bundleActivatorHandle The bundle activator library handle.
+ * @return The found bundle activator destroy function or NULL.
+ */
+celix_bundle_activator_destroy_fp 
celix_libloader_findBundleActivatorDestroy(celix_library_handle_t* 
bundleActivatorHandle);
+
 /**
  * @brief Tries to find the bundle activator library for the provided addr and 
if found tries to
  * find the symbol address for the provided symbol name in the bundle 
activator library.
diff --git a/libs/framework/src/framework.c b/libs/framework/src/framework.c
index 83a73da8..3bd9b5de 100644
--- a/libs/framework/src/framework.c
+++ b/libs/framework/src/framework.c
@@ -45,18 +45,13 @@
 #include "celix_convert_utils.h"
 #include "celix_build_assert.h"
 
-typedef celix_status_t (*create_function_fp)(bundle_context_t *context, void 
**userData);
-typedef celix_status_t (*start_function_fp)(void *userData, bundle_context_t 
*context);
-typedef celix_status_t (*stop_function_fp)(void *userData, bundle_context_t 
*context);
-typedef celix_status_t (*destroy_function_fp)(void *userData, bundle_context_t 
*context);
-
 struct celix_bundle_activator {
     void * userData;
 
-    create_function_fp create;
-    start_function_fp start;
-    stop_function_fp stop;
-    destroy_function_fp destroy;
+    celix_bundle_activator_create_fp create;
+    celix_bundle_activator_start_fp start;
+    celix_bundle_activator_stop_fp stop;
+    celix_bundle_activator_destroy_fp destroy;
 };
 
 static inline celix_framework_bundle_entry_t* 
fw_bundleEntry_create(celix_bundle_t *bnd) {
@@ -427,9 +422,9 @@ celix_status_t fw_init(framework_pt framework) {
             void * userData = NULL;
 
                        //create_function_pt create = NULL;
-                       start_function_fp start = (start_function_fp) 
frameworkActivator_start;
-                       stop_function_fp stop = (stop_function_fp) 
frameworkActivator_stop;
-                       destroy_function_fp destroy = (destroy_function_fp) 
frameworkActivator_destroy;
+            celix_bundle_activator_start_fp start = frameworkActivator_start;
+            celix_bundle_activator_stop_fp stop = frameworkActivator_stop;
+            celix_bundle_activator_destroy_fp destroy = 
frameworkActivator_destroy;
 
             activator->start = start;
             activator->stop = stop;
@@ -2196,28 +2191,13 @@ celix_status_t 
celix_framework_startBundleEntry(celix_framework_t* framework, ce
                 if (activator == NULL) {
                     status = CELIX_ENOMEM;
                 } else {
-                    void * userData = NULL;
-                    create_function_fp create = (create_function_fp) 
celix_libloader_getSymbol((celix_library_handle_t*) 
bundle_getHandle(bndEntry->bnd), OSGI_FRAMEWORK_BUNDLE_ACTIVATOR_CREATE);
-                    if (create == NULL) {
-                        create = 
celix_libloader_getSymbol(bundle_getHandle(bndEntry->bnd), 
OSGI_FRAMEWORK_DEPRECATED_BUNDLE_ACTIVATOR_CREATE);
-                    }
-                    start_function_fp start = (start_function_fp) 
celix_libloader_getSymbol((celix_library_handle_t*) 
bundle_getHandle(bndEntry->bnd), OSGI_FRAMEWORK_BUNDLE_ACTIVATOR_START);
-                    if (start == NULL) {
-                        start = (start_function_fp) 
celix_libloader_getSymbol((celix_library_handle_t*) 
bundle_getHandle(bndEntry->bnd), 
OSGI_FRAMEWORK_DEPRECATED_BUNDLE_ACTIVATOR_START);
-                    }
-                    stop_function_fp stop = (stop_function_fp) 
celix_libloader_getSymbol((celix_library_handle_t*) 
bundle_getHandle(bndEntry->bnd), OSGI_FRAMEWORK_BUNDLE_ACTIVATOR_STOP);
-                    if (stop == NULL) {
-                        stop = (stop_function_fp) 
celix_libloader_getSymbol((celix_library_handle_t*) 
bundle_getHandle(bndEntry->bnd), 
OSGI_FRAMEWORK_DEPRECATED_BUNDLE_ACTIVATOR_STOP);
-                    }
-                    destroy_function_fp destroy = (destroy_function_fp) 
celix_libloader_getSymbol((celix_library_handle_t*) 
bundle_getHandle(bndEntry->bnd), OSGI_FRAMEWORK_BUNDLE_ACTIVATOR_DESTROY);
-                    if (destroy == NULL) {
-                        destroy = (destroy_function_fp) 
celix_libloader_getSymbol((celix_library_handle_t*) 
bundle_getHandle(bndEntry->bnd), 
OSGI_FRAMEWORK_DEPRECATED_BUNDLE_ACTIVATOR_DESTROY);
-                    }
+                    void* userData = NULL;
+                    void* bundleHandle = bundle_getHandle(bndEntry->bnd);
 
-                    activator->create = create;
-                    activator->start = start;
-                    activator->stop = stop;
-                    activator->destroy = destroy;
+                    activator->create = 
celix_libloader_findBundleActivatorCreate(bundleHandle);
+                    activator->start = 
celix_libloader_findBundleActivatorStart(bundleHandle);
+                    activator->stop = 
celix_libloader_findBundleActivatorStop(bundleHandle);
+                    activator->destroy = 
celix_libloader_findBundleActivatorDestroy(bundleHandle);
 
                     status = CELIX_DO_IF(status, 
bundle_setActivator(bndEntry->bnd, activator));
 
@@ -2227,16 +2207,16 @@ celix_status_t 
celix_framework_startBundleEntry(celix_framework_t* framework, ce
                     status = CELIX_DO_IF(status, 
bundle_getContext(bndEntry->bnd, &context));
 
                     if (status == CELIX_SUCCESS) {
-                        if (create != NULL) {
-                            status = CELIX_DO_IF(status, create(context, 
&userData));
+                        if (activator->create != NULL) {
+                            status = CELIX_DO_IF(status, 
activator->create(context, &userData));
                             if (status == CELIX_SUCCESS) {
                                 activator->userData = userData;
                             }
                         }
                     }
                     if (status == CELIX_SUCCESS) {
-                        if (start != NULL) {
-                            status = CELIX_DO_IF(status, start(userData, 
context));
+                        if (activator->start != NULL) {
+                            status = CELIX_DO_IF(status, 
activator->start(userData, context));
                         }
                     }
 
@@ -2246,8 +2226,8 @@ celix_status_t 
celix_framework_startBundleEntry(celix_framework_t* framework, ce
                     if (status != CELIX_SUCCESS) {
                         //state is still STARTING, back to resolved
                         bool createCalled = activator != NULL && 
activator->userData != NULL;
-                        if (createCalled) {
-                            destroy(activator->userData, context);
+                        if (createCalled && activator->destroy) {
+                            activator->destroy(activator->userData, context);
                         }
                         bundle_setContext(bndEntry->bnd, NULL);
                         bundle_setActivator(bndEntry->bnd, NULL);

Reply via email to