From: Christophe Genevey Metat Date: Tue, 7 Jun 2016 13:50:08 +0000 (+0000) Subject: written function cleanup X-Git-Tag: initial-import-from-subversion-38251~807 X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=f12ac33d46fad89af94831f16dcdebd436a851da;p=oweals%2Fgnunet.git written function cleanup --- diff --git a/src/include/gnunet_my_lib.h b/src/include/gnunet_my_lib.h index f42276554..534a4db40 100644 --- a/src/include/gnunet_my_lib.h +++ b/src/include/gnunet_my_lib.h @@ -65,10 +65,21 @@ typedef int MYSQL_BIND *qbind); +/** + * Function called to cleanup result data. + * + * @param cls closure + * @param rs spec to clean up + */ +typedef void +(*GNUNET_MY_QueryCleanup)(void *cls, + struct GNUNET_MY_QueryParam *qp); /** * Information we pass to #GNUNET_MY_exec_prepared() to * initialize the arguments of the prepared statement. */ + + struct GNUNET_MY_QueryParam { @@ -77,6 +88,11 @@ struct GNUNET_MY_QueryParam */ GNUNET_MY_QueryConverter conv; + /** + * Function to call for cleaning up the query. Can be NULL. + */ + GNUNET_MY_QueryCleanup cleaner; + /** * Closure for @e conv. */ @@ -104,7 +120,7 @@ struct GNUNET_MY_QueryParam * * @return array last entry for the result specification to use */ -#define GNUNET_MY_query_param_end { NULL, NULL, 0, NULL, 0 } +#define GNUNET_MY_query_param_end { NULL, NULL, NULL, 0, NULL, 0 } @@ -447,6 +463,15 @@ GNUNET_MY_extract_result (struct GNUNET_MYSQL_StatementHandle *sh, struct GNUNET_MY_ResultSpec *specs); +/** + * Free all memory that was allocated in @a qp during + * #GNUNET_MY_exect_prepared(). + * + * @param qp query specification to clean up + */ +void +GNUNET_MY_cleanup_query (struct GNUNET_MY_QueryParam *qp); + /** * Free all memory that was allocated in @a rs during diff --git a/src/my/my.c b/src/my/my.c index 5409166fb..cec22716f 100644 --- a/src/my/my.c +++ b/src/my/my.c @@ -65,6 +65,10 @@ GNUNET_MY_exec_prepared (struct GNUNET_MYSQL_Context *mc, p, &qbind[off])) { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "Conversion for MySQL query failed at offset %u\n", + i); + GNUNET_MY_cleanup_query (params); return GNUNET_SYSERR; } off += p->num_params; @@ -88,6 +92,7 @@ GNUNET_MY_exec_prepared (struct GNUNET_MYSQL_Context *mc, "mysql_stmt_execute", __FILE__, __LINE__, mysql_stmt_error (stmt)); GNUNET_MYSQL_statements_invalidate (mc); + GNUNET_MY_cleanup_query (params); return GNUNET_SYSERR; } } @@ -96,6 +101,23 @@ GNUNET_MY_exec_prepared (struct GNUNET_MYSQL_Context *mc, } +/** + * Free all memory that was allocated in @a qp during + * #GNUNET_MY_exect_prepared(). + * + * @param qp query specification to clean up + */ +void +GNUNET_MY_cleanup_query (struct GNUNET_MY_QueryParam *qp) +{ + unsigned int i; + + for (i=0;NULL != qp[i].cleaner;i++) + qp[i].cleaner (qp[i].conv_cls, + &qp[i]); +} + + /** * Extract results from a query result according to the given * specification. Always fetches the next row. diff --git a/src/my/my_query_helper.c b/src/my/my_query_helper.c index 99bb4a5e4..cfa52b43a 100644 --- a/src/my/my_query_helper.c +++ b/src/my/my_query_helper.c @@ -62,6 +62,7 @@ GNUNET_MY_query_param_fixed_size (const void *ptr, { struct GNUNET_MY_QueryParam qp = { .conv = &my_conv_fixed_size, + .cleaner = NULL, .conv_cls = NULL, .num_params = 1, .data = ptr, @@ -106,7 +107,6 @@ my_conv_uint16 (void *cls, if (NULL == u_nbo) return -1; -// *u_nbo = htons (*u_hbo); *u_nbo = *u_hbo; qbind->buffer = (void *) u_nbo; @@ -126,6 +126,7 @@ GNUNET_MY_query_param_uint16 (const uint16_t *x) { struct GNUNET_MY_QueryParam res = { .conv = &my_conv_uint16, + .cleaner = NULL, .conv_cls = NULL, .num_params = 1, .data = x, @@ -174,6 +175,7 @@ GNUNET_MY_query_param_uint32 (const uint32_t *x) { struct GNUNET_MY_QueryParam res = { .conv = &my_conv_uint32, + .cleaner = NULL, .conv_cls = NULL, .num_params = 1, .data = x, @@ -222,6 +224,7 @@ GNUNET_MY_query_param_uint64 (const uint64_t *x) { struct GNUNET_MY_QueryParam res = { .conv = &my_conv_uint64, + .cleaner = NULL, .conv_cls = NULL, .num_params = 1, .data = x, @@ -260,6 +263,27 @@ my_conv_rsa_public_key (void *cls, } +/** + * Function called to clean up memory allocated + * by a #GNUNET_MY_ResultConverter. + * + * @param cls closure + * @param rd result data to clean up + */ +static void +my_clean_rsa_public_key (void *cls, + struct GNUNET_MY_QueryParam *qp) +{ + struct GNUNET_CRYPTO_RsaPublicKey **pk = qp->data; + + if (NULL != *pk) + { + GNUNET_CRYPTO_rsa_public_key_free (*pk); + *pk = NULL; + } +} + + /** * Generate query parameter for an RSA public key. The * database must contain a BLOB type in the respective position. @@ -272,6 +296,7 @@ GNUNET_MY_query_param_rsa_public_key (const struct GNUNET_CRYPTO_RsaPublicKey *x { struct GNUNET_MY_QueryParam res = { .conv = &my_conv_rsa_public_key, + .cleaner = &my_clean_rsa_public_key, .conv_cls = NULL, .num_params = 1, .data = x, @@ -311,6 +336,27 @@ my_conv_rsa_signature (void *cls, } +/** + * Function called to clean up memory allocated + * by a #GNUNET_MY_QueryConverter. + * + * @param cls closure + * @param rd result data to clean up + */ +static void +my_clean_rsa_signature (void *cls, + struct GNUNET_MY_QueryParam *qp) +{ + struct GNUNET_CRYPTO_RsaSignature **sig = qp->data; + + if (NULL != *sig) + { + GNUNET_CRYPTO_rsa_signature_free (*sig); + *sig = NULL; + } +} + + /** * Generate query parameter for an RSA signature. The * database must contain a BLOB type in the respective position @@ -323,6 +369,7 @@ GNUNET_MY_query_param_rsa_signature (const struct GNUNET_CRYPTO_RsaSignature *x) { struct GNUNET_MY_QueryParam res = { .conv = &my_conv_rsa_signature, + .cleaner = &my_clean_rsa_signature, .conv_cls = NULL, .num_params = 1, .data = (x), diff --git a/src/my/my_result_helper.c b/src/my/my_result_helper.c index 9cfd373c7..6f1b27065 100644 --- a/src/my/my_result_helper.c +++ b/src/my/my_result_helper.c @@ -43,7 +43,6 @@ pre_extract_varsize_blob (void *cls, results[0].buffer = NULL; results[0].buffer_length = 0; results[0].length = &rs->mysql_bind_output_length; - // results[0].buffer_type = MYSQL_TYPE_BLOB; return GNUNET_OK; } diff --git a/src/my/test_my.c b/src/my/test_my.c index f0766ac2c..f7e6ae68a 100644 --- a/src/my/test_my.c +++ b/src/my/test_my.c @@ -38,44 +38,44 @@ static int run_queries (struct GNUNET_MYSQL_Context *context) { struct GNUNET_CRYPTO_RsaPublicKey *pub; - struct GNUNET_CRYPTO_RsaPublicKey *pub2 = NULL; - struct GNUNET_CRYPTO_RsaSignature *sig; - struct GNUNET_CRYPTO_RsaSignature *sig2 = NULL; - struct GNUNET_TIME_Absolute abs_time = GNUNET_TIME_absolute_get (); - struct GNUNET_TIME_Absolute abs_time2; - struct GNUNET_TIME_Absolute forever = GNUNET_TIME_UNIT_FOREVER_ABS; - struct GNUNET_TIME_Absolute forever2; - struct GNUNET_HashCode hc; - struct GNUNET_HashCode hc2; - const char msg[] = "hello"; - void *msg2; - size_t msg2_len; - - uint16_t u16; - uint16_t u162; - uint32_t u32; - uint32_t u322; - uint64_t u64; - uint64_t u642; - - int ret; - - struct GNUNET_MYSQL_StatementHandle *statements_handle_insert; - struct GNUNET_MYSQL_StatementHandle *statements_handle_select; - - struct GNUNET_CRYPTO_RsaPrivateKey *priv; - struct GNUNET_HashCode hmsg; - - priv = GNUNET_CRYPTO_rsa_private_key_create (1024); - pub = GNUNET_CRYPTO_rsa_private_key_get_public (priv); - memset (&hmsg, 42, sizeof(hmsg)); - sig = GNUNET_CRYPTO_rsa_sign_fdh (priv, + struct GNUNET_CRYPTO_RsaPublicKey *pub2 = NULL; + struct GNUNET_CRYPTO_RsaSignature *sig; + struct GNUNET_CRYPTO_RsaSignature *sig2 = NULL; + struct GNUNET_TIME_Absolute abs_time = GNUNET_TIME_absolute_get (); + struct GNUNET_TIME_Absolute abs_time2; + struct GNUNET_TIME_Absolute forever = GNUNET_TIME_UNIT_FOREVER_ABS; + struct GNUNET_TIME_Absolute forever2; + struct GNUNET_HashCode hc; + struct GNUNET_HashCode hc2; + const char msg[] = "hello"; + void *msg2; + size_t msg2_len; + + uint16_t u16; + uint16_t u162; + uint32_t u32; + uint32_t u322; + uint64_t u64; + uint64_t u642; + + int ret; + + struct GNUNET_MYSQL_StatementHandle *statements_handle_insert; + struct GNUNET_MYSQL_StatementHandle *statements_handle_select; + + struct GNUNET_CRYPTO_RsaPrivateKey *priv; + struct GNUNET_HashCode hmsg; + + priv = GNUNET_CRYPTO_rsa_private_key_create (1024); + pub = GNUNET_CRYPTO_rsa_private_key_get_public (priv); + memset (&hmsg, 42, sizeof(hmsg)); + sig = GNUNET_CRYPTO_rsa_sign_fdh (priv, &hmsg); - u16 = 16; - u32 = 32; - u64 = 64; + u16 = 16; + u32 = 32; + u64 = 64; - statements_handle_insert = GNUNET_MYSQL_statement_prepare (context, + statements_handle_insert = GNUNET_MYSQL_statement_prepare (context, "INSERT INTO test_my2 (" " pub" ",sig" @@ -89,190 +89,146 @@ run_queries (struct GNUNET_MYSQL_Context *context) ") VALUES " "( ?, ?, ?, ?, ?, ?, ?, ?, ?)"); - if (NULL == statements_handle_insert) - { - fprintf (stderr, "Failed to prepared statement INSERT\n"); - return 1; - } - - struct GNUNET_MY_QueryParam params_insert[] = { - GNUNET_MY_query_param_rsa_public_key (pub), - GNUNET_MY_query_param_rsa_signature (sig), - GNUNET_MY_query_param_absolute_time (&abs_time), - GNUNET_MY_query_param_absolute_time (&forever), - GNUNET_MY_query_param_auto_from_type (&hc), - GNUNET_MY_query_param_fixed_size (msg, strlen (msg)), - GNUNET_MY_query_param_uint16 (&u16), - GNUNET_MY_query_param_uint32 (&u32), - GNUNET_MY_query_param_uint64 (&u64), - GNUNET_MY_query_param_end - }; - - /* statements_handle_insert = GNUNET_MYSQL_statement_prepare (context, - "INSERT INTO test_my2 (" - " abs_time" - ",forever" - ",hash" - ",u16" - ",u32" - ",u64" - ") VALUES " - "( ?, ?, ?, ?, ?, ?)"); - - struct GNUNET_MY_QueryParam params_insert[] = { - GNUNET_MY_query_param_absolute_time (&abs_time), - GNUNET_MY_query_param_absolute_time (&forever), - GNUNET_MY_query_param_auto_from_type (&hc), - GNUNET_MY_query_param_uint16 (&u16), - GNUNET_MY_query_param_uint32 (&u32), - GNUNET_MY_query_param_uint64 (&u64), - GNUNET_MY_query_param_end - }; -*/ - if (GNUNET_OK != GNUNET_MY_exec_prepared(context, - statements_handle_insert, - params_insert)) - { - fprintf (stderr, "Failed to execute prepared statement INSERT\n"); - return 1; - } - - - -/* statements_handle_select = GNUNET_MYSQL_statement_prepare (context, - "SELECT" - " pub" - ",sig" - ",abs_time" - ",forever" - ",hash" - ",vsize" - ",u16" - ",u32" - ",u64" - " FROM test_my" - " ORDER BY abs_time DESC " - " LIMIT 1;"); - -*/ - statements_handle_select = GNUNET_MYSQL_statement_prepare (context, - "SELECT" - " pub" - ",sig" - ",abs_time" - ",forever" - ",hash" - ",vsize" - ",u16" - ",u32" - ",u64" - " FROM test_my2"); - - if (NULL == statements_handle_select) - { - fprintf(stderr, "Failed to prepared statement SELECT\n"); - return 1; - } - - struct GNUNET_MY_QueryParam params_select[] = { - GNUNET_MY_query_param_end - }; - - if (GNUNET_OK != GNUNET_MY_exec_prepared (context, - statements_handle_select, - params_select)) - { + if (NULL == statements_handle_insert) + { + fprintf (stderr, "Failed to prepared statement INSERT\n"); + return 1; + } + + struct GNUNET_MY_QueryParam params_insert[] = { + GNUNET_MY_query_param_rsa_public_key (pub), + GNUNET_MY_query_param_rsa_signature (sig), + GNUNET_MY_query_param_absolute_time (&abs_time), + GNUNET_MY_query_param_absolute_time (&forever), + GNUNET_MY_query_param_auto_from_type (&hc), + GNUNET_MY_query_param_fixed_size (msg, strlen (msg)), + GNUNET_MY_query_param_uint16 (&u16), + GNUNET_MY_query_param_uint32 (&u32), + GNUNET_MY_query_param_uint64 (&u64), + GNUNET_MY_query_param_end + }; + + if (GNUNET_OK != GNUNET_MY_exec_prepared(context, + statements_handle_insert, + params_insert)) + { + fprintf (stderr, "Failed to execute prepared statement INSERT\n"); + return 1; + } + + statements_handle_select = GNUNET_MYSQL_statement_prepare (context, + "SELECT" + " pub" + ",sig" + ",abs_time" + ",forever" + ",hash" + ",vsize" + ",u16" + ",u32" + ",u64" + " FROM test_my2"); + + if (NULL == statements_handle_select) + { + fprintf(stderr, "Failed to prepared statement SELECT\n"); + return 1; + } + + struct GNUNET_MY_QueryParam params_select[] = { + GNUNET_MY_query_param_end + }; + + if (GNUNET_OK != GNUNET_MY_exec_prepared (context, + statements_handle_select, + params_select)) + { fprintf (stderr, "Failed to execute prepared statement SELECT\n"); return 1; - } - -/* - struct GNUNET_MY_ResultSpec results_select[] = { - GNUNET_MY_result_spec_rsa_public_key (&pub2), - GNUNET_MY_result_spec_rsa_signature (&sig2), - GNUNET_MY_result_spec_absolute_time (&abs_time2), - GNUNET_MY_result_spec_absolute_time (&forever2), - GNUNET_MY_result_spec_auto_from_type (&hc2), - GNUNET_MY_result_spec_variable_size (&msg2, &msg2_len), - GNUNET_MY_result_spec_uint16 (&u162), - GNUNET_MY_result_spec_uint32 (&u322), - GNUNET_MY_result_spec_uint64 (&u642), - GNUNET_MY_result_spec_end - }; -*/ - struct GNUNET_MY_ResultSpec results_select[] = { - GNUNET_MY_result_spec_rsa_public_key (&pub2), - GNUNET_MY_result_spec_rsa_signature (&sig2), - GNUNET_MY_result_spec_absolute_time (&abs_time2), - GNUNET_MY_result_spec_absolute_time (&forever2), - GNUNET_MY_result_spec_auto_from_type (&hc2), - GNUNET_MY_result_spec_variable_size (&msg2, &msg2_len), - GNUNET_MY_result_spec_uint16 (&u162), - GNUNET_MY_result_spec_uint32 (&u322), - GNUNET_MY_result_spec_uint64 (&u642), - GNUNET_MY_result_spec_end - }; - - ret = GNUNET_MY_extract_result (statements_handle_select, - results_select); - - GNUNET_break (GNUNET_YES == ret); - GNUNET_break (abs_time.abs_value_us == abs_time2.abs_value_us); - GNUNET_break (forever.abs_value_us == forever2.abs_value_us); - GNUNET_break (0 == - memcmp (&hc, + } + + struct GNUNET_MY_ResultSpec results_select[] = { + GNUNET_MY_result_spec_rsa_public_key (&pub2), + GNUNET_MY_result_spec_rsa_signature (&sig2), + GNUNET_MY_result_spec_absolute_time (&abs_time2), + GNUNET_MY_result_spec_absolute_time (&forever2), + GNUNET_MY_result_spec_auto_from_type (&hc2), + GNUNET_MY_result_spec_variable_size (&msg2, &msg2_len), + GNUNET_MY_result_spec_uint16 (&u162), + GNUNET_MY_result_spec_uint32 (&u322), + GNUNET_MY_result_spec_uint64 (&u642), + GNUNET_MY_result_spec_end + }; + + ret = GNUNET_MY_extract_result (statements_handle_select, + results_select); + + GNUNET_break (GNUNET_YES == ret); + GNUNET_break (abs_time.abs_value_us == abs_time2.abs_value_us); + GNUNET_break (forever.abs_value_us == forever2.abs_value_us); + GNUNET_break (0 == + memcmp (&hc, &hc2, sizeof (struct GNUNET_HashCode))); - GNUNET_break (strlen (msg) == msg2_len); - GNUNET_break (0 == + GNUNET_break (0 == + GNUNET_CRYPTO_rsa_signature_cmp (sig, + sig2)); + GNUNET_break (0 == + GNUNET_CRYPTO_rsa_public_key_cmp (pub, + pub2)); + + GNUNET_break (strlen (msg) == msg2_len); + GNUNET_break (0 == strncmp (msg, msg2, msg2_len)); - fprintf(stderr, "msg2 : %d\n", strlen(msg2)); - GNUNET_break (16 == u162); - GNUNET_break (32 == u322); - GNUNET_break (64 == u642); - - if (GNUNET_OK != ret) - { - fprintf(stderr, "Failed to extract result\n"); - return 1; - } + GNUNET_break (16 == u162); + GNUNET_break (32 == u322); + GNUNET_break (64 == u642); + + GNUNET_MY_cleanup_result (results_select); - return 0; + GNUNET_CRYPTO_rsa_signature_free (sig); + GNUNET_CRYPTO_rsa_private_key_free (priv); + GNUNET_CRYPTO_rsa_public_key_free (pub); + + if (GNUNET_OK != ret) + return 1; + + return 0; } int main (int argc, const char * const argv[]) { - struct GNUNET_CONFIGURATION_Handle *config; - struct GNUNET_MYSQL_Context *context; - int ret; + struct GNUNET_CONFIGURATION_Handle *config; + struct GNUNET_MYSQL_Context *context; + int ret; - GNUNET_log_setup ( "test-my", + GNUNET_log_setup ( "test-my", "WARNING", NULL); - config = GNUNET_CONFIGURATION_create (); - if (GNUNET_OK != GNUNET_CONFIGURATION_parse (config, "test_my.conf")) - { - fprintf (stderr, "Failed to parse configuaration\n"); - return 1; - } + config = GNUNET_CONFIGURATION_create (); + if (GNUNET_OK != GNUNET_CONFIGURATION_parse (config, "test_my.conf")) + { + fprintf (stderr, "Failed to parse configuaration\n"); + return 1; + } - context = GNUNET_MYSQL_context_create (config, + context = GNUNET_MYSQL_context_create (config, "datastore-mysql"); - if (NULL == context) - { - fprintf(stderr, "Failed to connect to database\n"); - return 77; - } + if (NULL == context) + { + fprintf(stderr, "Failed to connect to database\n"); + return 77; + } - (void) GNUNET_MYSQL_statement_run (context, + (void) GNUNET_MYSQL_statement_run (context, "DROP TABLE test_my2;"); - if (GNUNET_OK != GNUNET_MYSQL_statement_run (context, + if (GNUNET_OK != GNUNET_MYSQL_statement_run (context, "CREATE TABLE IF NOT EXISTS test_my2(" " pub BLOB NOT NULL" ",sig BLOB NOT NULL" @@ -284,43 +240,16 @@ main (int argc, const char * const argv[]) ",u32 INT NOT NULL" ",u64 BIGINT NOT NULL" ")")) - { - fprintf (stderr, - "Failed to create table \n"); - GNUNET_MYSQL_statements_invalidate (context); - GNUNET_MYSQL_context_destroy (context); - - return 1; - } - -/* if (GNUNET_OK != GNUNET_MYSQL_statement_run (context, - "CREATE TABLE test_my2(" - " abs_time BIGINT NOT NULL" - ", forever BIGINT NOT NULL" - ", hash VARCHAR(32) NOT NULL CHECK(LENGTH(hash)=64)" - ", u16 SMALLINT NOT NULL" - ", u32 INT NOT NULL" - ", u64 BIGINT NOT NULL" - ")")) - { - fprintf (stderr, - "Failed to create table \n"); - GNUNET_MYSQL_statements_invalidate (context); - GNUNET_MYSQL_context_destroy (context); + { + fprintf (stderr, + "Failed to create table \n"); + GNUNET_MYSQL_statements_invalidate (context); + GNUNET_MYSQL_context_destroy (context); - return 1; - } -*/ - ret = run_queries (context); + return 1; + } -/* if(GNUNET_OK != GNUNET_MYSQL_statement_run (context, - "DROP TABLE test_my2")) - { - fprintf (stderr, "Failed to drop table test_my\n"); - GNUNET_MYSQL_statements_invalidate (context); - } + ret = run_queries (context); - GNUNET_MYSQL_context_destroy (context); -*/ - return ret; + return ret; }