mintdb get_known_coin(): Do not allocate memory for return paramter.

Instead populate the fields of the placeholder return variable.
This commit is contained in:
Sree Harsha Totakura 2015-05-27 14:20:07 +02:00
parent 02c237d269
commit 1d551bf36b
3 changed files with 21 additions and 30 deletions

View File

@ -332,7 +332,7 @@ struct TALER_MINTDB_RefreshMelt
*/
struct TALER_Amount amount_with_fee;
/**
/** FIXME: This can be retrieved from the Denomination? Do we need this?
* Melting fee charged by the mint. This must match the Mint's
* denomination key's melting fee. If the client puts in an invalid
* melting fee (too high or too low) that does not match the Mint's
@ -848,7 +848,7 @@ struct TALER_MINTDB_Plugin
* @param cls the plugin closure
* @param session the database session handle
* @param coin_pub the public key of the coin to search for
* @param ret_coin_info place holder for the returned coin information object
* @param coin_info place holder for the returned coin information object
* @return #GNUNET_SYSERR upon error; #GNUNET_NO if no coin is found; #GNUNET_OK
* if upon succesfullying retrieving the record data info @a
* ret_coin_info
@ -857,7 +857,7 @@ struct TALER_MINTDB_Plugin
(*get_known_coin) (void *cls,
struct TALER_MINTDB_Session *session,
const struct TALER_CoinSpendPublicKeyP *coin_pub,
struct TALER_CoinPublicInfo **ret_coin_info);
struct TALER_CoinPublicInfo *coin_info);
/**

View File

@ -1641,23 +1641,24 @@ postgres_insert_known_coin (void *cls,
* @param cls the plugin closure
* @param session the database session handle
* @param coin_pub the public key of the coin to search for
* @param ret_coin_info place holder for the returned coin information object
* @param coin_info place holder for the returned coin information object
* @return #GNUNET_SYSERR upon error; #GNUNET_NO if no coin is found; #GNUNET_OK
* if upon succesfullying retrieving the record data info @a
* ret_coin_info
* coin_info
*/
static int
postgres_get_known_coin (void *cls,
struct TALER_MINTDB_Session *session,
const struct TALER_CoinSpendPublicKeyP *coin_pub,
struct TALER_CoinPublicInfo **ret_coin_info)
struct TALER_CoinPublicInfo *coin_info)
{
PGresult *result;
struct TALER_PQ_QueryParam params[] = {
TALER_PQ_query_param_auto_from_type (coin_pub),
TALER_PQ_query_param_end
};
struct TALER_CoinPublicInfo *coin_info;
int nrows;
result = TALER_PQ_exec_prepared (session->conn,
"get_known_coin",
params);
@ -1667,17 +1668,13 @@ postgres_get_known_coin (void *cls,
PQclear (result);
return GNUNET_SYSERR;
}
if (0 == PQntuples (result))
nrows = PQntuples (result);
if (0 == nrows)
{
PQclear (result);
return GNUNET_NO;
}
if ((NULL == ret_coin_info) && (1 == PQntuples (result)))
{
PQclear (result);
return GNUNET_OK;
}
coin_info = GNUNET_new (struct TALER_CoinPublicInfo);
GNUNET_assert (1 == nrows); /* due to primary key */
struct TALER_PQ_ResultSpec rs[] = {
TALER_PQ_result_spec_rsa_public_key ("denom_pub", &coin_info->denom_pub.rsa_public_key),
TALER_PQ_result_spec_rsa_signature ("denom_sig", &coin_info->denom_sig.rsa_signature),
@ -1691,10 +1688,11 @@ postgres_get_known_coin (void *cls,
return GNUNET_SYSERR;
}
PQclear (result);
/* no need to copy if the src and dest are same */
if (coin_pub != &coin_info->coin_pub)
(void) memcpy (&coin_info->coin_pub,
coin_pub,
sizeof (struct TALER_CoinSpendPublicKeyP));
*ret_coin_info = coin_info;
return GNUNET_OK;
}

View File

@ -180,7 +180,7 @@ static int
test_known_coins (struct TALER_MINTDB_Session *session)
{
struct TALER_CoinPublicInfo coin_info;
struct TALER_CoinPublicInfo *ret_coin_info;
struct TALER_CoinPublicInfo ret_coin_info;
struct DenomKeyPair *dkp;
int ret = GNUNET_SYSERR;
@ -190,13 +190,11 @@ test_known_coins (struct TALER_MINTDB_Session *session)
coin_info.denom_sig.rsa_signature =
GNUNET_CRYPTO_rsa_sign (dkp->priv.rsa_private_key,
"foobar", 6);
ret_coin_info = NULL;
FAILIF (GNUNET_NO !=
plugin->get_known_coin (plugin->cls,
session,
&coin_info.coin_pub,
&ret_coin_info));
FAILIF (NULL != ret_coin_info);
FAILIF (GNUNET_OK !=
plugin->insert_known_coin (plugin->cls,
session,
@ -206,23 +204,18 @@ test_known_coins (struct TALER_MINTDB_Session *session)
session,
&coin_info.coin_pub,
&ret_coin_info));
FAILIF (NULL == ret_coin_info);
FAILIF (0 != GNUNET_CRYPTO_rsa_public_key_cmp
(ret_coin_info->denom_pub.rsa_public_key,
(ret_coin_info.denom_pub.rsa_public_key,
coin_info.denom_pub.rsa_public_key));
FAILIF (0 != GNUNET_CRYPTO_rsa_signature_cmp
(ret_coin_info->denom_sig.rsa_signature,
(ret_coin_info.denom_sig.rsa_signature,
coin_info.denom_sig.rsa_signature));
GNUNET_CRYPTO_rsa_public_key_free (ret_coin_info.denom_pub.rsa_public_key);
GNUNET_CRYPTO_rsa_signature_free (ret_coin_info.denom_sig.rsa_signature);
ret = GNUNET_OK;
drop:
destroy_denom_key_pair (dkp);
GNUNET_CRYPTO_rsa_signature_free (coin_info.denom_sig.rsa_signature);
if (NULL != ret_coin_info)
{
GNUNET_CRYPTO_rsa_public_key_free (ret_coin_info->denom_pub.rsa_public_key);
GNUNET_CRYPTO_rsa_signature_free (ret_coin_info->denom_sig.rsa_signature);
GNUNET_free (ret_coin_info);
}
return ret;
}