From 726b28a61c858b4b251e4a0a006e4021b01886c0 Mon Sep 17 00:00:00 2001 From: Martin Schanzenbach Date: Thu, 26 Mar 2015 15:51:56 +0000 Subject: [PATCH] -fix mem corruption --- src/identity/plugin_rest_identity.c | 44 ++++++++++++++--------------- src/rest/gnunet-rest-server.c | 11 +------- src/rest/rest.c | 32 ++++++++++++++------- 3 files changed, 44 insertions(+), 43 deletions(-) diff --git a/src/identity/plugin_rest_identity.c b/src/identity/plugin_rest_identity.c index d62e56db2..2fb29d87d 100644 --- a/src/identity/plugin_rest_identity.c +++ b/src/identity/plugin_rest_identity.c @@ -31,9 +31,7 @@ #include "microhttpd.h" #include -#define API_NAMESPACE "/identity" - -#define EGO_NAMESPACE "/identity/egos" +#define GNUNET_REST_API_NS_IDENTITY "/identity" #define ID_REST_STATE_INIT 0 @@ -183,8 +181,6 @@ cleanup_handle (struct RequestHandle *handle) struct EgoEntry *ego_tmp; GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Cleaning up\n"); - if (NULL != handle->json_root) - json_decref (handle->json_root); if (NULL != handle->name) GNUNET_free (handle->name); if (NULL != handle->timeout_task) @@ -301,16 +297,14 @@ ego_info_response (struct RestConnectionDataHandle *con, struct JsonApiObject *json_object; struct JsonApiResource *json_resource; json_t *key_str; - - if (GNUNET_NO == GNUNET_REST_namespace_match (handle->url, EGO_NAMESPACE)) + if (GNUNET_NO == GNUNET_REST_namespace_match (handle->url, GNUNET_REST_API_NS_IDENTITY)) { resp = GNUNET_REST_create_json_response (NULL); handle->proc (handle->proc_cls, resp, MHD_HTTP_BAD_REQUEST); cleanup_handle (handle); - GNUNET_break (0); return; } - if ( (strlen (EGO_NAMESPACE) == strlen (handle->url) )) { + if ( (strlen (GNUNET_REST_API_NS_IDENTITY) == strlen (handle->url) )) { GNUNET_CRYPTO_hash (URL_PARAM_SUBSYS, strlen (URL_PARAM_SUBSYS), &key); if ( GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (handle->conndata_handle->url_param_map, @@ -331,13 +325,12 @@ ego_info_response (struct RestConnectionDataHandle *con, } } json_object = GNUNET_REST_jsonapi_object_new (); - egoname = &handle->url[strlen (EGO_NAMESPACE)+1]; + egoname = &handle->url[strlen (GNUNET_REST_API_NS_IDENTITY)+1]; - if (strlen (EGO_NAMESPACE) == strlen (handle->url)) + if (strlen (GNUNET_REST_API_NS_IDENTITY) == strlen (handle->url)) { egoname = NULL; } - //Return all egos for (ego_entry = handle->ego_head; NULL != ego_entry; @@ -355,7 +348,12 @@ ego_info_response (struct RestConnectionDataHandle *con, GNUNET_free (keystring); GNUNET_REST_jsonapi_object_resource_add (json_object, json_resource); } - + if (0 == GNUNET_REST_jsonapi_object_resource_count (json_object)) + { + GNUNET_REST_jsonapi_object_delete (json_object); + GNUNET_SCHEDULER_add_now (&do_error, handle); + return; + } GNUNET_REST_jsonapi_data_serialize (json_object, &result_str); GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Result %s\n", result_str); resp = GNUNET_REST_create_json_response (result_str); @@ -430,7 +428,7 @@ ego_create_cont (struct RestConnectionDataHandle *con, char term_data[handle->data_size+1]; const char* egoname; - if (strlen (API_NAMESPACE) != strlen (handle->url)) + if (strlen (GNUNET_REST_API_NS_IDENTITY) != strlen (handle->url)) { GNUNET_SCHEDULER_add_now (&do_error, handle); return; @@ -513,13 +511,13 @@ subsys_set_cont (struct RestConnectionDataHandle *con, int ego_exists = GNUNET_NO; json_t *subsys_json; - if (strlen (API_NAMESPACE) > strlen (handle->url)) + if (strlen (GNUNET_REST_API_NS_IDENTITY) > strlen (handle->url)) { GNUNET_SCHEDULER_add_now (&do_error, handle); return; } - egoname = &handle->url[strlen(EGO_NAMESPACE)+1]; + egoname = &handle->url[strlen(GNUNET_REST_API_NS_IDENTITY)+1]; for (ego_entry = handle->ego_head; NULL != ego_entry; ego_entry = ego_entry->next) @@ -605,13 +603,13 @@ ego_delete_cont (struct RestConnectionDataHandle *con_handle, struct RequestHandle *handle = cls; int ego_exists = GNUNET_NO; - if (strlen (API_NAMESPACE) >= strlen (handle->url)) + if (strlen (GNUNET_REST_API_NS_IDENTITY) >= strlen (handle->url)) { GNUNET_SCHEDULER_add_now (&do_error, handle); return; } - egoname = &handle->url[strlen(API_NAMESPACE)+1]; + egoname = &handle->url[strlen(GNUNET_REST_API_NS_IDENTITY)+1]; for (ego_entry = handle->ego_head; NULL != ego_entry; ego_entry = ego_entry->next) @@ -640,10 +638,10 @@ void init_cont (struct RequestHandle *handle) { static const struct GNUNET_REST_RestConnectionHandler handlers[] = { - {MHD_HTTP_METHOD_GET, API_NAMESPACE, &ego_info_response}, - {MHD_HTTP_METHOD_POST, API_NAMESPACE, &ego_create_cont}, - {MHD_HTTP_METHOD_PUT, API_NAMESPACE, &subsys_set_cont}, - {MHD_HTTP_METHOD_DELETE, API_NAMESPACE, &ego_delete_cont}, + {MHD_HTTP_METHOD_GET, GNUNET_REST_API_NS_IDENTITY, &ego_info_response}, + {MHD_HTTP_METHOD_POST, GNUNET_REST_API_NS_IDENTITY, &ego_create_cont}, + {MHD_HTTP_METHOD_PUT, GNUNET_REST_API_NS_IDENTITY, &subsys_set_cont}, + {MHD_HTTP_METHOD_DELETE, GNUNET_REST_API_NS_IDENTITY, &ego_delete_cont}, GNUNET_REST_HANDLER_END }; @@ -775,7 +773,7 @@ libgnunet_plugin_rest_identity_init (void *cls) plugin.cfg = cfg; api = GNUNET_new (struct GNUNET_REST_Plugin); api->cls = &plugin; - api->name = API_NAMESPACE; + api->name = GNUNET_REST_API_NS_IDENTITY; api->process_request = &rest_identity_process_request; GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Identity REST API initialized\n")); diff --git a/src/rest/gnunet-rest-server.c b/src/rest/gnunet-rest-server.c index 84fc69d02..aa95e48b3 100644 --- a/src/rest/gnunet-rest-server.c +++ b/src/rest/gnunet-rest-server.c @@ -292,7 +292,7 @@ create_response (void *cls, } if (NULL == con_handle->plugin) { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Queueing response with MHD\n"); GNUNET_free (con_handle); return MHD_queue_response (con, @@ -356,15 +356,6 @@ mhd_completed_cb (void *cls, void **con_cls, enum MHD_RequestTerminationCode toe) { - struct MhdConnectionHandle *con_handle; - - con_handle = *con_cls; - if (NULL != con_handle) - { - MHD_destroy_response (con_handle->response); - GNUNET_free (con_handle); - *con_cls = NULL; - } if (MHD_REQUEST_TERMINATED_COMPLETED_OK != toe) GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "MHD encountered error handling request: %d\n", diff --git a/src/rest/rest.c b/src/rest/rest.c index d6fb8a2f3..f3251db1a 100644 --- a/src/rest/rest.c +++ b/src/rest/rest.c @@ -91,6 +91,8 @@ GNUNET_REST_jsonapi_resource_new (const char *type, const char *id) return NULL; res = GNUNET_new (struct JsonApiResource); + res->prev = NULL; + res->next = NULL; res->res_obj = json_object (); @@ -236,6 +238,8 @@ add_json_resource (struct JsonApiObject *obj, return; res = GNUNET_new (struct JsonApiResource); + res->next = NULL; + res->prev = NULL; res->res_obj = json_deep_copy (res_json); GNUNET_REST_jsonapi_object_resource_add (obj, res); } @@ -303,11 +307,18 @@ void GNUNET_REST_jsonapi_object_delete (struct JsonApiObject *resp) { struct JsonApiResource *res; + struct JsonApiResource *res_next; for (res = resp->res_list_head; - res != NULL; - res = res->next) + res != NULL;) + { + GNUNET_CONTAINER_DLL_remove (resp->res_list_head, + resp->res_list_tail, + res); + res_next = res->next; GNUNET_REST_jsonapi_resource_delete (res); + res = res_next; + } GNUNET_free (resp); } @@ -420,7 +431,7 @@ GNUNET_REST_jsonapi_data_serialize (const struct JsonApiObject *resp, } json_object_set (root_json, GNUNET_REST_JSONAPI_KEY_DATA, res_arr); } - *result = json_dumps (root_json, JSON_COMPACT); + *result = json_dumps (root_json, JSON_INDENT(2)); return GNUNET_OK; } @@ -454,14 +465,17 @@ GNUNET_REST_namespace_match (const char *url, const char *namespace) * @param data JSON result * @retun MHD response */ - struct MHD_Response* +struct MHD_Response* GNUNET_REST_create_json_response (const char *data) { struct MHD_Response *resp; size_t len; if (NULL == data) + { len = 0; + data = ""; + } else len = strlen (data); resp = MHD_create_response_from_buffer (len, @@ -482,24 +496,22 @@ GNUNET_REST_handle_request (struct RestConnectionDataHandle *conn, char *url; count = 0; - while (NULL != handlers[count].method) count++; GNUNET_asprintf (&url, "%s", conn->url); if (url[strlen (url)-1] == '/') url[strlen (url)-1] = '\0'; - for (i = 0; i < count; i++) { - if (0 != strcasecmp (conn->method, handlers[count].method)) + if (0 != strcasecmp (conn->method, handlers[i].method)) break; - if (strlen (url) < strlen (handlers[count].namespace)) + if (strlen (url) < strlen (handlers[i].namespace)) break; - if (GNUNET_NO == GNUNET_REST_namespace_match (url, handlers[count].namespace)) + if (GNUNET_NO == GNUNET_REST_namespace_match (url, handlers[i].namespace)) break; //Match - handlers[count].proc (conn, (const char*)url, cls); + handlers[i].proc (conn, (const char*)url, cls); GNUNET_free (url); return GNUNET_YES; } -- 2.25.1