Skip to content

Commit

Permalink
Improve properties set version error handling in ctx and dm
Browse files Browse the repository at this point in the history
  • Loading branch information
pnoltes committed Dec 22, 2023
1 parent caf9173 commit 9d37c55
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
10 changes: 8 additions & 2 deletions libs/framework/src/bundle_context.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
24 changes: 16 additions & 8 deletions libs/framework/src/dm_component_impl.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@
#include <stdlib.h>
#include <string.h>
#include <uuid/uuid.h>
#include <celix_utils.h>
#include <assert.h>
#include <celix_bundle.h>

#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"
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit 9d37c55

Please sign in to comment.