Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

apacheGH-491: Check return value of vasprintf and asprintf #781

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ celix_event_admin_remote_provider_mqtt_t* celix_earpm_create(celix_bundle_contex
}

if (asprintf(&earpm->syncEventAckTopic, CELIX_EARPM_SYNC_EVENT_ACK_TOPIC_PREFIX"%s", earpm->fwUUID) < 0) {
earpm->syncEventAckTopic = NULL;
celix_logHelper_error(logHelper, "Failed to create sync event response topic.");
return NULL;
}
Expand Down
8 changes: 7 additions & 1 deletion bundles/http_admin/http_admin/src/activator.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,12 @@ static int http_admin_start(http_admin_activator_t *act, celix_bundle_context_t

for(long port = prop_port_min; act->httpManager == NULL && port <= prop_port_max; port++) {
char *port_str;
asprintf(&port_str, "%li", port);
rc= asprintf(&port_str, "%li", port);
if(rc < 0 || port_str == NULL) {
celix_bundleContext_log(ctx, CELIX_LOG_LEVEL_ERROR, "Cannot allocate memory for port string.");
free(httpRoot);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it double-free?

return CELIX_ENOMEM;
}
svr_opts[3] = port_str;
act->httpManager = httpAdmin_create(ctx, httpRoot, svr_opts);
free(port_str);
Expand Down Expand Up @@ -136,6 +141,7 @@ static int http_admin_start(http_admin_activator_t *act, celix_bundle_context_t
}
}

free(httpRoot);
return CELIX_SUCCESS;
}

Expand Down
8 changes: 8 additions & 0 deletions bundles/http_admin/http_admin/src/http_admin.c
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,10 @@ static void createAliasesSymlink(const char *aliases, const char *admin_root, co
}

asprintf(&alias_path, "%s/%s%s", cwd, admin_root, sub_token);
if (alias_path == NULL) { // Check if asprintf failed.
continue;
}

if(aliasList_containsAlias(alias_list, alias_path)) {
free(alias_path);
continue; //Alias already existing, Continue to next entry
Expand All @@ -431,6 +435,10 @@ static void createAliasesSymlink(const char *aliases, const char *admin_root, co
}

asprintf(&bnd_resource_path, "%s/%s/%s", cwd, bundle_root, sub_token);
if (bnd_resource_path == NULL) { // Check if asprintf failed.
free(alias_path);
continue;
}

if(symlink(bnd_resource_path, alias_path) == 0) {
http_alias_t *alias = calloc(1, sizeof(*alias));
Expand Down
18 changes: 16 additions & 2 deletions bundles/http_admin/http_admin/src/service_tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,23 @@ bool addServiceNode(service_tree_t *svc_tree, const char *uri, void *svc) {
//Else root already exists
} else if(svc_tree->root_node == NULL) { //No service yet added
uri_cpy = strdup(uri);
if (uri_cpy == NULL) { // Check for memory allocation failure
return false;
}
req_uri = strtok_r(uri_cpy, "/", &save_ptr);
svc_tree->root_node = createServiceNode(NULL, NULL, NULL, NULL, req_uri, (tokenCount == 1 ? svc : NULL));
svc_tree->tree_node_count = 1;
uri_exists = false;
} else if(strcmp(svc_tree->root_node->svc_data->sub_uri, "root") == 0){
asprintf(&uri_cpy, "%s%s", "root", uri);
if (asprintf(&uri_cpy, "%s%s", "root", uri)<0) {
return false;
}
req_uri = strtok_r(uri_cpy, "/", &save_ptr);
} else {
uri_cpy = strdup(uri);
if (uri_cpy == NULL) { // Check for memory allocation failure
return false;
}
req_uri = strtok_r(uri_cpy, "/", &save_ptr);
}

Expand Down Expand Up @@ -293,9 +301,15 @@ service_tree_node_t *findServiceNodeInTree(service_tree_t *svc_tree, const char
if (tree_not_empty) {
if(strcmp(current->svc_data->sub_uri, "root") == 0)
{
asprintf(&uri_cpy, "%s%s", "root", uri);
if (asprintf(&uri_cpy, "%s%s", "root", uri)<0)
{
return NULL;
}
} else {
uri_cpy = strdup(uri);
if (uri_cpy == NULL) { // Check for strdup failure
return NULL;
}
}

char *uri_token = strtok_r(uri_cpy, "/", &save_ptr);
Expand Down
13 changes: 12 additions & 1 deletion bundles/logging/log_helper/src/celix_log_helper.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,15 @@ static void celix_logHelper_setLogSvc(void *handle, void *svc) {

celix_log_helper_t* celix_logHelper_create(celix_bundle_context_t* ctx, const char* logServiceName) {
celix_log_helper_t* logHelper = calloc(1, sizeof(*logHelper));
if (logHelper == NULL) {
return NULL;
}
logHelper->ctx = ctx;
logHelper->logServiceName = celix_utils_strdup(logServiceName);
if (logHelper->logServiceName == NULL) {
free(logHelper);
return NULL;
}
celixThreadMutex_create(&logHelper->mutex, NULL);

const char *actLogLevelStr = celix_bundleContext_getProperty(ctx, CELIX_LOGGING_DEFAULT_ACTIVE_LOG_LEVEL_CONFIG_NAME, CELIX_LOGGING_DEFAULT_ACTIVE_LOG_LEVEL_DEFAULT_VALUE);
Expand All @@ -58,7 +65,11 @@ celix_log_helper_t* celix_logHelper_create(celix_bundle_context_t* ctx, const ch

char *filter = NULL;
if (logServiceName != NULL) {
asprintf(&filter, "(%s=%s)", CELIX_LOG_SERVICE_PROPERTY_NAME, logServiceName);
if (asprintf(&filter, "(%s=%s)", CELIX_LOG_SERVICE_PROPERTY_NAME, logServiceName) < 0) {
free(logHelper->logServiceName);
free(logHelper);
return NULL;
}
}

celix_service_tracking_options_t opts = CELIX_EMPTY_SERVICE_TRACKING_OPTIONS;
Expand Down
33 changes: 22 additions & 11 deletions bundles/remote_services/discovery_common/src/discovery_activator.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,21 @@ celix_status_t celix_bundleActivator_start(void * userData, celix_bundle_context

char* scope = NULL;
int rc = asprintf(&scope, "(&(%s=*)(%s=%s))", CELIX_FRAMEWORK_SERVICE_NAME, CELIX_RSA_ENDPOINT_FRAMEWORK_UUID, uuid);
status = rc < 0 ? CELIX_ENOMEM : CELIX_SUCCESS;
if (rc < 0) {
return CELIX_ENOMEM;
}
status = rc < 0 ? CELIX_ENOMEM : CELIX_SUCCESS;

celix_autoptr(celix_properties_t) props = NULL;
if (status == CELIX_SUCCESS) {
celix_logHelper_debug(activator->loghelper, "using scope %s.", scope);

props = celix_properties_create();
celix_properties_set(props, "DISCOVERY", "true");
if (props==NULL) {
free(scope);
return CELIX_ENOMEM;
}
celix_properties_set(props, "DISCOVERY", "true");
celix_properties_set(props, (char *) CELIX_RSA_ENDPOINT_LISTENER_SCOPE, scope);
}

Expand All @@ -126,17 +133,21 @@ celix_status_t celix_bundleActivator_start(void * userData, celix_bundle_context
if (status == CELIX_SUCCESS) {
endpoint_listener_t *endpointListener = calloc(1, sizeof(struct endpoint_listener));

if (endpointListener) {
endpointListener->handle = activator->discovery;
endpointListener->endpointAdded = discovery_endpointAdded;
endpointListener->endpointRemoved = discovery_endpointRemoved;
if (endpointListener==NULL) {
free(scope);
status = CELIX_ENOMEM;
}
endpointListener->handle = activator->discovery;
endpointListener->endpointAdded = discovery_endpointAdded;
endpointListener->endpointRemoved = discovery_endpointRemoved;

status = bundleContext_registerService(context, (char *) CELIX_RSA_ENDPOINT_LISTENER_SERVICE_NAME, endpointListener,
celix_steal_ptr(props), &activator->endpointListenerService);
status = bundleContext_registerService(context, (char *) CELIX_RSA_ENDPOINT_LISTENER_SERVICE_NAME, endpointListener,
celix_steal_ptr(props), &activator->endpointListenerService);

if (status == CELIX_SUCCESS) {
activator->endpointListener = endpointListener;
}
if (status == CELIX_SUCCESS) {
activator->endpointListener = endpointListener;
} else{
free(endpointListener);
}
}
// We can release the scope, as celix_properties_set makes a copy of the key & value...
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,13 @@ celix_status_t endpointDiscoveryServer_create(discovery_t *discovery,
(*server)->loghelper = &discovery->loghelper;
(*server)->entries = hashMap_create(&utils_stringHash, NULL, &utils_stringEquals, NULL);
if (!(*server)->entries) {
free(*server);
return CELIX_ENOMEM;
}

status = celixThreadMutex_create(&(*server)->serverLock, NULL);
if (status != CELIX_SUCCESS) {
free(*server);
return CELIX_BUNDLE_EXCEPTION;
}

Expand Down Expand Up @@ -128,6 +130,11 @@ celix_status_t endpointDiscoveryServer_create(discovery_t *discovery,
free(detectedIp);
}

if (!(*server)->ip) { // Check if strdup failed
free(*server);
return CELIX_ENOMEM;
}

bundleContext_getProperty(context, DISCOVERY_SERVER_PORT, &port);
if (port == NULL) {
port = defaultServerPort;
Expand All @@ -148,6 +155,11 @@ celix_status_t endpointDiscoveryServer_create(discovery_t *discovery,
}

(*server)->path = format_path(path);
if (!(*server)->path) {
free((*server)->ip);
free(*server);
return CELIX_ENOMEM;
}

const struct mg_callbacks callbacks = {
.begin_request = endpointDiscoveryServer_callback,
Expand All @@ -165,6 +177,13 @@ celix_status_t endpointDiscoveryServer_create(discovery_t *discovery,
asprintf(&listeningPorts,"%s:%s", (*server)->ip, port);
}

if (!listeningPorts) { // Check if asprintf failed
free((*server)->path);
free((*server)->ip);
free(*server);
return CELIX_ENOMEM;
}

const char *options[] = {
"listening_ports", listeningPorts,
"num_threads", DEFAULT_SERVER_THREADS,
Expand Down Expand Up @@ -199,6 +218,12 @@ celix_status_t endpointDiscoveryServer_create(discovery_t *discovery,
} while(((*server)->ctx == NULL) && (port_counter < max_ep_num));

(*server)->port = strdup(port);
if (!(*server)->port) {
free((*server)->path);
free((*server)->ip);
free(*server);
return CELIX_ENOMEM;
}

return status;
}
Expand Down