From 9d37c5548de987e6bc41fa62aef04d6c0e1753c4 Mon Sep 17 00:00:00 2001 From: Pepijn Noltes Date: Fri, 22 Dec 2023 17:15:39 +0100 Subject: [PATCH] Improve properties set version error handling in ctx and dm --- libs/framework/src/bundle_context.c | 10 ++++++++-- libs/framework/src/dm_component_impl.c | 24 ++++++++++++++++-------- 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/libs/framework/src/bundle_context.c b/libs/framework/src/bundle_context.c index f45d82221..7985fcf3a 100644 --- a/libs/framework/src/bundle_context.c +++ b/libs/framework/src/bundle_context.c @@ -398,14 +398,20 @@ static long celix_bundleContext_registerServiceWithOptionsInternal(bundle_contex } if (opts->serviceVersion != NULL && strncmp("", opts->serviceVersion, 1) != 0) { - celix_version_t* version = celix_version_createVersionFromString(opts->serviceVersion); + celix_autoptr(celix_version_t) version = celix_version_createVersionFromString(opts->serviceVersion); if (!version) { celix_framework_logTssErrors(ctx->framework->logger, CELIX_LOG_LEVEL_ERROR); fw_log( ctx->framework->logger, CELIX_LOG_LEVEL_ERROR, "Cannot parse service version %s", opts->serviceVersion); return -1; } - celix_properties_setVersionWithoutCopy(props, CELIX_FRAMEWORK_SERVICE_VERSION, version); + celix_status_t rc = + celix_properties_setVersionWithoutCopy(props, CELIX_FRAMEWORK_SERVICE_VERSION, celix_steal_ptr(version)); + if (rc != CELIX_SUCCESS) { + celix_framework_logTssErrors(ctx->framework->logger, CELIX_LOG_LEVEL_ERROR); + fw_log(ctx->framework->logger, CELIX_LOG_LEVEL_ERROR, "Cannot set service version %s", opts->serviceVersion); + return -1; + } } long svcId; diff --git a/libs/framework/src/dm_component_impl.c b/libs/framework/src/dm_component_impl.c index 1286a1efa..d4c3ea60e 100644 --- a/libs/framework/src/dm_component_impl.c +++ b/libs/framework/src/dm_component_impl.c @@ -20,10 +20,11 @@ #include #include #include -#include #include -#include +#include "celix_utils.h" +#include "celix_bundle.h" +#include "celix_stdlib_cleanup.h" #include "celix_constants.h" #include "celix_filter.h" #include "dm_component_impl.h" @@ -456,32 +457,39 @@ celix_status_t celix_dmComponent_addInterface(celix_dm_component_t* component, return CELIX_ILLEGAL_ARGUMENT; } - dm_interface_t* interface = calloc(1, sizeof(*interface)); - char* name = celix_utils_strdup(serviceName); + celix_autofree dm_interface_t* interface = calloc(1, sizeof(*interface)); + celix_autofree char* name = celix_utils_strdup(serviceName); if (properties == NULL) { properties = celix_properties_create(); } if (serviceVersion != NULL) { - celix_version_t* version = celix_version_createVersionFromString(serviceVersion); + celix_autoptr(celix_version_t) version = celix_version_createVersionFromString(serviceVersion); if (!version) { celix_bundleContext_log( component->context, CELIX_LOG_LEVEL_ERROR, "Cannot add interface with an invalid serviceVersion"); celix_properties_destroy(properties); return CELIX_ILLEGAL_ARGUMENT; } - celix_properties_setVersionWithoutCopy(properties, CELIX_FRAMEWORK_SERVICE_VERSION, version); + celix_status_t rc = celix_properties_setVersionWithoutCopy( + properties, CELIX_FRAMEWORK_SERVICE_VERSION, celix_steal_ptr(version)); + if (rc != CELIX_SUCCESS) { + celix_bundleContext_log( + component->context, CELIX_LOG_LEVEL_ERROR, "Cannot add interface with an invalid serviceVersion"); + celix_properties_destroy(properties); + return CELIX_ILLEGAL_ARGUMENT; + } } celix_properties_set(properties, CELIX_DM_COMPONENT_UUID, (char*)component->uuid); celixThreadMutex_lock(&component->mutex); - interface->serviceName = name; + interface->serviceName = celix_steal_ptr(name); interface->service = service; interface->properties = properties; interface->svcId = -1L; - celix_arrayList_add(component->providedInterfaces, interface); + celix_arrayList_add(component->providedInterfaces, celix_steal_ptr(interface)); if (celix_dmComponent_currentState(component) == CELIX_DM_CMP_STATE_TRACKING_OPTIONAL) { celix_dmComponent_registerServices(component, false); }