cleaning up some memory management issues in response generation

This commit is contained in:
Christian Grothoff 2015-01-28 19:58:20 +01:00
parent 2e0e30291c
commit 554ee3d4da
3 changed files with 64 additions and 68 deletions

View File

@ -1293,7 +1293,9 @@ TALER_MINT_db_execute_refresh_link (struct MHD_Connection *connection,
json_object_set_new (root, "secret_enc", json_object_set_new (root, "secret_enc",
TALER_JSON_from_data (&shared_secret_enc, TALER_JSON_from_data (&shared_secret_enc,
sizeof (struct GNUNET_HashCode))); sizeof (struct GNUNET_HashCode)));
return TALER_MINT_reply_json (connection, res = TALER_MINT_reply_json (connection,
root, root,
MHD_HTTP_OK); MHD_HTTP_OK);
json_decref (root);
return res;
} }

View File

@ -36,8 +36,7 @@
/** /**
* Send JSON object as response. Decreases the reference count of the * Send JSON object as response.
* JSON object.
* *
* @param connection the MHD connection * @param connection the MHD connection
* @param json the json object * @param json the json object
@ -46,7 +45,7 @@
*/ */
int int
TALER_MINT_reply_json (struct MHD_Connection *connection, TALER_MINT_reply_json (struct MHD_Connection *connection,
json_t *json, const json_t *json,
unsigned int response_code) unsigned int response_code)
{ {
struct MHD_Response *resp; struct MHD_Response *resp;
@ -54,7 +53,6 @@ TALER_MINT_reply_json (struct MHD_Connection *connection,
int ret; int ret;
json_str = json_dumps (json, JSON_INDENT(2)); json_str = json_dumps (json, JSON_INDENT(2));
json_decref (json);
resp = MHD_create_response_from_buffer (strlen (json_str), json_str, resp = MHD_create_response_from_buffer (strlen (json_str), json_str,
MHD_RESPMEM_MUST_FREE); MHD_RESPMEM_MUST_FREE);
if (NULL == resp) if (NULL == resp)
@ -86,15 +84,18 @@ TALER_MINT_reply_json_pack (struct MHD_Connection *connection,
{ {
json_t *json; json_t *json;
va_list argp; va_list argp;
int ret;
va_start (argp, fmt); va_start (argp, fmt);
json = json_vpack_ex (NULL, 0, fmt, argp); json = json_vpack_ex (NULL, 0, fmt, argp);
va_end (argp); va_end (argp);
if (NULL == json) if (NULL == json)
return MHD_NO; return MHD_NO;
return TALER_MINT_reply_json (connection, ret = TALER_MINT_reply_json (connection,
json, json,
response_code); response_code);
json_decref (json);
return ret;
} }
@ -109,16 +110,11 @@ int
TALER_MINT_reply_arg_invalid (struct MHD_Connection *connection, TALER_MINT_reply_arg_invalid (struct MHD_Connection *connection,
const char *param_name) const char *param_name)
{ {
json_t *json; return TALER_MINT_reply_json_pack (connection,
MHD_HTTP_BAD_REQUEST,
json = json_pack ("{ s:s, s:s }", "{s:s, s:s}",
"error", "error", "invalid parameter",
"invalid parameter", "parameter", param_name);
"parameter",
param_name);
return TALER_MINT_reply_json (connection,
json,
MHD_HTTP_BAD_REQUEST);
} }
@ -153,16 +149,11 @@ int
TALER_MINT_reply_arg_missing (struct MHD_Connection *connection, TALER_MINT_reply_arg_missing (struct MHD_Connection *connection,
const char *param_name) const char *param_name)
{ {
json_t *json; return TALER_MINT_reply_json_pack (connection,
MHD_HTTP_BAD_REQUEST,
json = json_pack ("{ s:s, s:s }", "{ s:s, s:s}",
"error", "error", "missing parameter",
"missing parameter", "parameter", param_name);
"parameter",
param_name);
return TALER_MINT_reply_json (connection,
json,
MHD_HTTP_BAD_REQUEST);
} }
@ -177,16 +168,11 @@ int
TALER_MINT_reply_internal_error (struct MHD_Connection *connection, TALER_MINT_reply_internal_error (struct MHD_Connection *connection,
const char *hint) const char *hint)
{ {
json_t *json; return TALER_MINT_reply_json_pack (connection,
MHD_HTTP_BAD_REQUEST,
json = json_pack ("{ s:s, s:s }", "{s:s, s:s}",
"error", "error", "internal error",
"internal error", "hint", hint);
"hint",
hint);
return TALER_MINT_reply_json (connection,
json,
MHD_HTTP_BAD_REQUEST);
} }
@ -319,20 +305,20 @@ TALER_MINT_reply_withdraw_status_success (struct MHD_Connection *connection,
const struct TALER_Amount balance, const struct TALER_Amount balance,
struct GNUNET_TIME_Absolute expiration) struct GNUNET_TIME_Absolute expiration)
{ {
json_t *json; json_t *json_balance;
json_t *json_expiration;
int ret;
/* Convert the public information of a reserve (i.e. json_balance = TALER_JSON_from_amount (balance);
excluding private key) to a JSON object. */ json_expiration = TALER_JSON_from_abs (expiration);
json = json_object (); ret = TALER_MINT_reply_json_pack (connection,
json_object_set_new (json, MHD_HTTP_OK,
"balance", "{s:o, s:o}",
TALER_JSON_from_amount (balance)); "balance", json_balance,
json_object_set_new (json, "expiration", json_expiration);
"expiration", json_decref (json_balance);
TALER_JSON_from_abs (expiration)); json_decref (json_expiration);
return TALER_MINT_reply_json (connection, return ret;
json,
MHD_HTTP_OK);
} }
@ -347,19 +333,23 @@ int
TALER_MINT_reply_withdraw_sign_success (struct MHD_Connection *connection, TALER_MINT_reply_withdraw_sign_success (struct MHD_Connection *connection,
const struct CollectableBlindcoin *collectable) const struct CollectableBlindcoin *collectable)
{ {
json_t *root = json_object (); json_t *sig_json;
size_t sig_buf_size; size_t sig_buf_size;
char *sig_buf; char *sig_buf;
int ret;
/* FIXME: use TALER_JSON_from_sig here instead! */
sig_buf_size = GNUNET_CRYPTO_rsa_signature_encode (collectable->sig, sig_buf_size = GNUNET_CRYPTO_rsa_signature_encode (collectable->sig,
&sig_buf); &sig_buf);
json_object_set_new (root, "ev_sig", sig_json = TALER_JSON_from_data (sig_buf,
TALER_JSON_from_data (sig_buf, sig_buf_size);
sig_buf_size));
GNUNET_free (sig_buf); GNUNET_free (sig_buf);
return TALER_MINT_reply_json (connection, ret = TALER_MINT_reply_json_pack (connection,
root, MHD_HTTP_OK,
MHD_HTTP_OK); "{s:o}",
"ev_sig", sig_json);
json_decref (sig_json);
return ret;
} }
@ -421,6 +411,7 @@ TALER_MINT_reply_refresh_commit_success (struct MHD_Connection *connection,
struct RefreshCommitResponseSignatureBody body; struct RefreshCommitResponseSignatureBody body;
struct GNUNET_CRYPTO_EddsaSignature sig; struct GNUNET_CRYPTO_EddsaSignature sig;
json_t *sig_json; json_t *sig_json;
int ret;
body.purpose.size = htonl (sizeof (struct RefreshCommitResponseSignatureBody)); body.purpose.size = htonl (sizeof (struct RefreshCommitResponseSignatureBody));
body.purpose.purpose = htonl (TALER_SIGNATURE_REFRESH_COMMIT_RESPONSE); body.purpose.purpose = htonl (TALER_SIGNATURE_REFRESH_COMMIT_RESPONSE);
@ -429,11 +420,13 @@ TALER_MINT_reply_refresh_commit_success (struct MHD_Connection *connection,
&sig); &sig);
sig_json = TALER_JSON_from_sig (&body.purpose, &sig); sig_json = TALER_JSON_from_sig (&body.purpose, &sig);
GNUNET_assert (NULL != sig_json); GNUNET_assert (NULL != sig_json);
return TALER_MINT_reply_json_pack (connection, ret = TALER_MINT_reply_json_pack (connection,
MHD_HTTP_OK, MHD_HTTP_OK,
"{s:i, s:o}", "{s:i, s:o}",
"noreveal_index", (int) refresh_session->noreveal_index, "noreveal_index", (int) refresh_session->noreveal_index,
"signature", sig_json); "signature", sig_json);
json_decref (sig_json);
return ret;
} }
@ -455,6 +448,7 @@ TALER_MINT_reply_refresh_reveal_success (struct MHD_Connection *connection,
json_t *list; json_t *list;
char *buf; char *buf;
size_t buf_size; size_t buf_size;
int ret;
root = json_object (); root = json_object ();
list = json_array (); list = json_array ();
@ -468,10 +462,11 @@ TALER_MINT_reply_refresh_reveal_success (struct MHD_Connection *connection,
buf_size)); buf_size));
GNUNET_free (buf); GNUNET_free (buf);
} }
return TALER_MINT_reply_json (connection, ret = TALER_MINT_reply_json (connection,
root, root,
MHD_HTTP_OK); MHD_HTTP_OK);
json_decref (root);
return ret;
} }

View File

@ -34,8 +34,7 @@
/** /**
* Send JSON object as response. Decreases the reference count of the * Send JSON object as response.
* JSON object.
* *
* @param connection the MHD connection * @param connection the MHD connection
* @param json the json object * @param json the json object
@ -44,7 +43,7 @@
*/ */
int int
TALER_MINT_reply_json (struct MHD_Connection *connection, TALER_MINT_reply_json (struct MHD_Connection *connection,
json_t *json, const json_t *json,
unsigned int response_code); unsigned int response_code);