From d274ac3e73dd30de260e1c36c4bee76b935f28d3 Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Sun, 4 Jun 2017 21:40:24 +0200 Subject: fix misc memory leaks --- src/backend/taler-merchant-httpd_exchanges.c | 10 ++ src/backend/taler-merchant-httpd_proposal.c | 34 ++++--- .../taler-merchant-httpd_track-transaction.c | 11 ++- src/backend/taler-merchant-httpd_track-transfer.c | 108 ++++++++++++--------- src/backenddb/plugin_merchantdb_postgres.c | 3 +- 5 files changed, 96 insertions(+), 70 deletions(-) diff --git a/src/backend/taler-merchant-httpd_exchanges.c b/src/backend/taler-merchant-httpd_exchanges.c index aab5b271..f8e67054 100644 --- a/src/backend/taler-merchant-httpd_exchanges.c +++ b/src/backend/taler-merchant-httpd_exchanges.c @@ -930,9 +930,19 @@ TMH_EXCHANGES_done () while (NULL != (exchange = exchange_head)) { + struct FeesByWireMethod *f; + GNUNET_CONTAINER_DLL_remove (exchange_head, exchange_tail, exchange); + while (NULL != (f = exchange->wire_fees_head)) + { + GNUNET_CONTAINER_DLL_remove (exchange->wire_fees_head, + exchange->wire_fees_tail, + f); + GNUNET_free (f->wire_method); + GNUNET_free (f); + } if (NULL != exchange->wire_request) { TALER_EXCHANGE_wire_cancel (exchange->wire_request); diff --git a/src/backend/taler-merchant-httpd_proposal.c b/src/backend/taler-merchant-httpd_proposal.c index fa8251c3..85a43c0a 100644 --- a/src/backend/taler-merchant-httpd_proposal.c +++ b/src/backend/taler-merchant-httpd_proposal.c @@ -178,9 +178,9 @@ proposal_put (struct MHD_Connection *connection, "-%llX", (long long unsigned) GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK, UINT64_MAX)); - json_object_set (order, - "order_id", - json_string (buf)); + json_object_set_new (order, + "order_id", + json_string (buf)); } if (NULL == json_object_get (order, "timestamp")) @@ -188,9 +188,9 @@ proposal_put (struct MHD_Connection *connection, struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get (); (void) GNUNET_TIME_round_abs (&now); - json_object_set (order, - "timestamp", - GNUNET_JSON_from_time_abs (now)); + json_object_set_new (order, + "timestamp", + GNUNET_JSON_from_time_abs (now)); } if (NULL == json_object_get (order, @@ -198,9 +198,9 @@ proposal_put (struct MHD_Connection *connection, { struct GNUNET_TIME_Absolute zero = { 0 }; - json_object_set (order, - "refund_deadline", - GNUNET_JSON_from_time_abs (zero)); + json_object_set_new (order, + "refund_deadline", + GNUNET_JSON_from_time_abs (zero)); } if (NULL == json_object_get (order, @@ -211,23 +211,25 @@ proposal_put (struct MHD_Connection *connection, /* FIXME: read the delay for pay_deadline from config */ t = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS); (void) GNUNET_TIME_round_abs (&t); - json_object_set (order, "pay_deadline", GNUNET_JSON_from_time_abs (t)); + json_object_set_new (order, + "pay_deadline", + GNUNET_JSON_from_time_abs (t)); } if (NULL == json_object_get (order, "max_wire_fee")) { - json_object_set (order, - "max_wire_fee", - TALER_JSON_from_amount (&default_max_wire_fee)); + json_object_set_new (order, + "max_wire_fee", + TALER_JSON_from_amount (&default_max_wire_fee)); } if (NULL == json_object_get (order, "wire_fee_amortization")) { - json_object_set (order, - "wire_fee_amortization", - json_integer ((json_int_t) default_wire_fee_amortization)); + json_object_set_new (order, + "wire_fee_amortization", + json_integer ((json_int_t) default_wire_fee_amortization)); } /* extract fields we need to sign separately */ diff --git a/src/backend/taler-merchant-httpd_track-transaction.c b/src/backend/taler-merchant-httpd_track-transaction.c index eb1ee164..e9610ae2 100644 --- a/src/backend/taler-merchant-httpd_track-transaction.c +++ b/src/backend/taler-merchant-httpd_track-transaction.c @@ -917,16 +917,17 @@ MH_handler_track_transaction (struct TMH_RequestHandler *rh, TALER_EC_TRACK_TRANSACTION_INSTANCE_UNKNOWN, "unknown instance"); - if (GNUNET_YES != db->find_contract_terms (db->cls, - &contract_terms, - order_id, - &tctx->mi->pubkey)) - + if (GNUNET_YES != + db->find_contract_terms (db->cls, + &contract_terms, + order_id, + &tctx->mi->pubkey)) return TMH_RESPONSE_reply_not_found (connection, TALER_EC_PROPOSAL_LOOKUP_NOT_FOUND, "Given order_id doesn't map to any proposal"); TALER_JSON_hash (contract_terms, &h_contract_terms); + json_decref (contract_terms); GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Trying to track h_contract_terms '%s'\n", diff --git a/src/backend/taler-merchant-httpd_track-transfer.c b/src/backend/taler-merchant-httpd_track-transfer.c index 4ca22aff..168fdcb0 100644 --- a/src/backend/taler-merchant-httpd_track-transfer.c +++ b/src/backend/taler-merchant-httpd_track-transfer.c @@ -215,8 +215,8 @@ build_deposits_response (void *cls, void *value) { struct TrackTransferContext *rctx = cls; - json_t *element; struct Entry *entry = value; + json_t *element; json_t *contract_terms; json_t *order_id; @@ -230,23 +230,26 @@ build_deposits_response (void *cls, } order_id = json_object_get (contract_terms, "order_id"); - + if (NULL == order_id) + { + GNUNET_break_op (0); + return GNUNET_NO; + } element = json_pack ("{s:s, s:o, s:o}", "order_id", json_string_value (order_id), "deposit_value", TALER_JSON_from_amount (&entry->deposit_value), "deposit_fee", TALER_JSON_from_amount (&entry->deposit_fee)); - - if (NULL == order_id || NULL == element) + if (NULL == element) { GNUNET_break_op (0); return GNUNET_NO; } - - json_array_append_new (deposits_response, element); - + json_array_append_new (deposits_response, + element); return GNUNET_YES; } + /** * Transform /track/transfer result as gotten from the exchange * and transforms it in a format liked by the backoffice Web interface. @@ -254,8 +257,9 @@ build_deposits_response (void *cls, * @param result response from exchange's /track/transfer * @result pointer to new JSON, or NULL upon errors. */ -json_t * -transform_response (const json_t *result, struct TrackTransferContext *rctx) +static json_t * +transform_response (const json_t *result, + struct TrackTransferContext *rctx) { json_t *deposits; json_t *value; @@ -267,80 +271,89 @@ transform_response (const json_t *result, struct TrackTransferContext *rctx) struct TALER_Amount iter_value; struct TALER_Amount iter_fee; struct Entry *current_entry; - - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Transforming /track/transfer response.\n"); struct GNUNET_JSON_Specification spec[] = { TALER_JSON_spec_amount ("deposit_value", &iter_value), TALER_JSON_spec_amount ("deposit_fee", &iter_fee), GNUNET_JSON_spec_string ("h_contract_terms", &key), GNUNET_JSON_spec_end () }; - + + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Transforming /track/transfer response.\n"); map = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_NO); - deposits = json_object_get (result, "deposits"); + deposits = json_object_get (result, + "deposits"); json_array_foreach (deposits, index, value) { - if (GNUNET_OK != GNUNET_JSON_parse (value, - spec, - NULL, - NULL)) + if (GNUNET_OK != + GNUNET_JSON_parse (value, + spec, + NULL, + NULL)) { - GNUNET_break_op (0); + GNUNET_break_op (0); return NULL; } + GNUNET_CRYPTO_hash_from_string (key, + &h_key); - GNUNET_CRYPTO_hash_from_string (key, &h_key); - - if (NULL != (current_entry = GNUNET_CONTAINER_multihashmap_get (map, (const struct GNUNET_HashCode *) &h_key))) + if (NULL != (current_entry = GNUNET_CONTAINER_multihashmap_get (map, + &h_key))) { - /*The map already knows this h_contract_terms*/ + /* The map already knows this h_contract_terms*/ if ((GNUNET_SYSERR == TALER_amount_add (¤t_entry->deposit_value, ¤t_entry->deposit_value, &iter_value)) || (GNUNET_SYSERR == TALER_amount_add (¤t_entry->deposit_fee, ¤t_entry->deposit_fee, &iter_fee))) + { + GNUNET_JSON_parse_free (spec); goto cleanup; + } } else { - /*First time in the map for this h_contract_terms*/ - current_entry = GNUNET_malloc (sizeof (struct Entry)); - memcpy (¤t_entry->deposit_value, &iter_value, sizeof (struct TALER_Amount)); - memcpy (¤t_entry->deposit_fee, &iter_fee, sizeof (struct TALER_Amount)); - - if (GNUNET_SYSERR == GNUNET_CONTAINER_multihashmap_put (map, - (const struct GNUNET_HashCode *) &h_key, - current_entry, - GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)) + /* First time in the map for this h_contract_terms*/ + current_entry = GNUNET_new (struct Entry); + current_entry->deposit_value = iter_value; + current_entry->deposit_fee = iter_fee; + + if (GNUNET_SYSERR == + GNUNET_CONTAINER_multihashmap_put (map, + &h_key, + current_entry, + GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)) + { + GNUNET_JSON_parse_free (spec); goto cleanup; + } } + GNUNET_JSON_parse_free (spec); } deposits_response = json_array (); - + if (GNUNET_SYSERR == GNUNET_CONTAINER_multihashmap_iterate (map, build_deposits_response, rctx)) goto cleanup; - result_mod = json_copy ((struct json_t *) result); - json_object_del (result_mod, "deposits"); - json_object_set (result_mod, "deposits_sums", deposits_response); - - goto cleanup; - - cleanup: - GNUNET_CONTAINER_multihashmap_iterate (map, - &hashmap_free, - NULL); - GNUNET_JSON_parse_free (spec); - GNUNET_CONTAINER_multihashmap_destroy (map); - return result_mod; + json_object_del (result_mod, + "deposits"); + json_object_set_new (result_mod, + "deposits_sums", + deposits_response); + cleanup: + GNUNET_CONTAINER_multihashmap_iterate (map, + &hashmap_free, + NULL); + GNUNET_CONTAINER_multihashmap_destroy (map); + return result_mod; } + /** * Resume the given /track/transfer operation and send the given response. * Stores the response in the @a rctx and signals MHD to resume @@ -685,7 +698,8 @@ proof_cb (void *cls, struct TrackTransferContext *rctx = cls; json_t *transformed_response; - if (NULL == (transformed_response = transform_response (proof, rctx))) + if (NULL == (transformed_response = transform_response (proof, + rctx))) { rctx->response_code = MHD_HTTP_INTERNAL_SERVER_ERROR; rctx->response = TMH_RESPONSE_make_internal_error (TALER_EC_TRACK_TRANSFER_JSON_RESPONSE_ERROR, diff --git a/src/backenddb/plugin_merchantdb_postgres.c b/src/backenddb/plugin_merchantdb_postgres.c index 3b3c0047..3903d172 100644 --- a/src/backenddb/plugin_merchantdb_postgres.c +++ b/src/backenddb/plugin_merchantdb_postgres.c @@ -1503,12 +1503,12 @@ postgres_find_proof_by_wtid (void *cls, { struct PostgresClosure *pg = cls; PGresult *result; - struct GNUNET_PQ_QueryParam params[] = { GNUNET_PQ_query_param_auto_from_type (wtid), GNUNET_PQ_query_param_string (exchange_uri), GNUNET_PQ_query_param_end }; + result = GNUNET_PQ_exec_prepared (pg->conn, "find_proof_by_wtid", params); @@ -1532,7 +1532,6 @@ postgres_find_proof_by_wtid (void *cls, { json_t *proof; - struct GNUNET_PQ_ResultSpec rs[] = { TALER_PQ_result_spec_json ("proof", &proof), -- cgit v1.2.3