[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[GNUnet-SVN] r37259 - in gnunet/src: include my
From: |
gnunet |
Subject: |
[GNUnet-SVN] r37259 - in gnunet/src: include my |
Date: |
Thu, 9 Jun 2016 17:39:57 +0200 |
Author: christophe.genevey
Date: 2016-06-09 17:39:57 +0200 (Thu, 09 Jun 2016)
New Revision: 37259
Modified:
gnunet/src/include/gnunet_my_lib.h
gnunet/src/my/my.c
gnunet/src/my/my_query_helper.c
gnunet/src/my/my_result_helper.c
gnunet/src/my/test_my.c
Log:
fix memory leak
Modified: gnunet/src/include/gnunet_my_lib.h
===================================================================
--- gnunet/src/include/gnunet_my_lib.h 2016-06-09 14:36:14 UTC (rev 37258)
+++ gnunet/src/include/gnunet_my_lib.h 2016-06-09 15:39:57 UTC (rev 37259)
@@ -73,7 +73,7 @@
*/
typedef void
(*GNUNET_MY_QueryCleanup)(void *cls,
- struct GNUNET_MY_QueryParam *qp);
+ MYSQL_BIND *qbind);
/**
* Information we pass to #GNUNET_MY_exec_prepared() to
* initialize the arguments of the prepared statement.
@@ -146,7 +146,7 @@
int
GNUNET_MY_exec_prepared (struct GNUNET_MYSQL_Context *mc,
struct GNUNET_MYSQL_StatementHandle *sh,
- const struct GNUNET_MY_QueryParam *params);
+ struct GNUNET_MY_QueryParam *params);
/**
@@ -470,7 +470,8 @@
* @param qp query specification to clean up
*/
void
-GNUNET_MY_cleanup_query (struct GNUNET_MY_QueryParam *qp);
+GNUNET_MY_cleanup_query (struct GNUNET_MY_QueryParam *qp,
+ MYSQL_BIND *qbind);
/**
Modified: gnunet/src/my/my.c
===================================================================
--- gnunet/src/my/my.c 2016-06-09 14:36:14 UTC (rev 37258)
+++ gnunet/src/my/my.c 2016-06-09 15:39:57 UTC (rev 37259)
@@ -42,7 +42,7 @@
int
GNUNET_MY_exec_prepared (struct GNUNET_MYSQL_Context *mc,
struct GNUNET_MYSQL_StatementHandle *sh,
- const struct GNUNET_MY_QueryParam *params)
+ struct GNUNET_MY_QueryParam *params)
{
const struct GNUNET_MY_QueryParam *p;
unsigned int num;
@@ -68,7 +68,6 @@
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;
@@ -92,9 +91,11 @@
"mysql_stmt_execute", __FILE__, __LINE__,
mysql_stmt_error (stmt));
GNUNET_MYSQL_statements_invalidate (mc);
- GNUNET_MY_cleanup_query (params);
return GNUNET_SYSERR;
}
+
+ GNUNET_MY_cleanup_query (params,
+ qbind);
}
return GNUNET_OK;
@@ -108,13 +109,15 @@
* @param qp query specification to clean up
*/
void
-GNUNET_MY_cleanup_query (struct GNUNET_MY_QueryParam *qp)
+GNUNET_MY_cleanup_query (struct GNUNET_MY_QueryParam *qp,
+ MYSQL_BIND * qbind)
{
unsigned int i;
- for (i=0;NULL != qp[i].cleaner;i++)
- qp[i].cleaner (qp[i].conv_cls,
- &qp[i]);
+ for (i=0; NULL != qp[i].conv ;i++)
+ if(NULL != qp[i].cleaner)
+ qp[i].cleaner (qp[i].conv_cls,
+ &qbind[i]);
}
@@ -245,9 +248,10 @@
{
unsigned int i;
- for (i=0;NULL != rs[i].cleaner;i++)
- rs[i].cleaner (rs[i].conv_cls,
- &rs[i]);
+ for (i=0;NULL != rs[i].post_conv;i++)
+ if (NULL != rs[i].cleaner)
+ rs[i].cleaner (rs[i].conv_cls,
+ &rs[i]);
}
Modified: gnunet/src/my/my_query_helper.c
===================================================================
--- gnunet/src/my/my_query_helper.c 2016-06-09 14:36:14 UTC (rev 37258)
+++ gnunet/src/my/my_query_helper.c 2016-06-09 15:39:57 UTC (rev 37259)
@@ -26,7 +26,23 @@
#include <mysql/mysql.h>
#include "gnunet_my_lib.h"
+
/**
+ * 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_query (void *cls,
+ MYSQL_BIND *qbind)
+{
+ GNUNET_free (qbind[0].buffer);
+}
+
+
+/**
* Function called to convert input argument into SQL parameters.
*
* @param cls closure
@@ -84,6 +100,7 @@
strlen(ptr));
}
+
/**
* Function called to convert input argument into SQL parameters
*
@@ -107,7 +124,7 @@
if (NULL == u_nbo)
return -1;
- *u_nbo = *u_hbo;
+ *u_nbo = *u_hbo;
qbind->buffer = (void *) u_nbo;
qbind->buffer_length = sizeof(uint16_t);
@@ -116,6 +133,7 @@
return 1;
}
+
/**
* Generate query parameter for an uint16_t in host byte order.
*
@@ -126,7 +144,7 @@
{
struct GNUNET_MY_QueryParam res = {
.conv = &my_conv_uint16,
- .cleaner = NULL,
+ .cleaner = &my_clean_query,
.conv_cls = NULL,
.num_params = 1,
.data = x,
@@ -136,6 +154,7 @@
return res;
}
+
/**
* Function called to convert input argument into SQL parameters
*
@@ -155,9 +174,9 @@
GNUNET_assert (1 == qp->num_params);
u_nbo = GNUNET_new (uint32_t);
-// *u_nbo = htonl (*u_hbo);
- *u_nbo = *u_hbo;
+ *u_nbo = *u_hbo;
+
qbind->buffer = (void *) u_nbo;
qbind->buffer_length = sizeof(uint32_t);
qbind->buffer_type = MYSQL_TYPE_LONG;
@@ -165,6 +184,7 @@
return 1;
}
+
/**
* Generate query parameter for an uint32_t in host byte order
*
@@ -175,7 +195,7 @@
{
struct GNUNET_MY_QueryParam res = {
.conv = &my_conv_uint32,
- .cleaner = NULL,
+ .cleaner = &my_clean_query,
.conv_cls = NULL,
.num_params = 1,
.data = x,
@@ -185,6 +205,7 @@
return res;
}
+
/**
* Function called to convert input argument into SQL parameters
*
@@ -204,9 +225,9 @@
GNUNET_assert (1 == qp->num_params);
u_nbo = GNUNET_new(uint64_t);
- //*u_nbo = GNUNET_htonll (*u_hbo);
- *u_nbo = *u_hbo;
+ *u_nbo = *u_hbo;
+
qbind->buffer = (void *) u_nbo;
qbind->buffer_length = sizeof (uint64_t);
qbind->buffer_type = MYSQL_TYPE_LONGLONG;
@@ -214,6 +235,7 @@
return 1;
}
+
/**
* Generate query parameter for an uint64_t in host byte order
*
@@ -224,7 +246,7 @@
{
struct GNUNET_MY_QueryParam res = {
.conv = &my_conv_uint64,
- .cleaner = NULL,
+ .cleaner = &my_clean_query,
.conv_cls = NULL,
.num_params = 1,
.data = x,
@@ -234,6 +256,7 @@
return res;
}
+
/**
* Function called to convert input argument into SQL parameters
*
@@ -246,57 +269,36 @@
my_conv_rsa_public_key (void *cls,
const struct GNUNET_MY_QueryParam *qp,
MYSQL_BIND * qbind)
- {
- const struct GNUNET_CRYPTO_RsaPublicKey *rsa = qp->data;
- char *buf;
- size_t buf_size;
+{
+ const struct GNUNET_CRYPTO_RsaPublicKey *rsa = qp->data;
+ char *buf;
+ size_t buf_size;
- GNUNET_assert(1 == qp->num_params);
- // FIXME: this leaks memory right now...
- buf_size = GNUNET_CRYPTO_rsa_public_key_encode (rsa, &buf);
+ GNUNET_assert(1 == qp->num_params);
+ // FIXME: this leaks memory right now...
+ buf_size = GNUNET_CRYPTO_rsa_public_key_encode (rsa, &buf);
- qbind->buffer = (void *) buf;
- qbind->buffer_length = buf_size;
- qbind->buffer_type = MYSQL_TYPE_BLOB;
+ qbind->buffer = (void *) buf;
+ qbind->buffer_length = buf_size;
+ qbind->buffer_type = MYSQL_TYPE_BLOB;
- return 1;
- }
+ return 1;
+}
/**
- * 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.
- *
- * @param x the query parameter to pass
- * @return array entry for the query parameters to use
- */
+ * Generate query parameter for an RSA public key. The
+ * database must contain a BLOB type in the respective position.
+ *
+ * @param x the query parameter to pass
+ * @return array entry for the query parameters to use
+ */
struct GNUNET_MY_QueryParam
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,
+ .cleaner = &my_clean_query,
.conv_cls = NULL,
.num_params = 1,
.data = x,
@@ -312,7 +314,7 @@
*
address@hidden cls closure
address@hidden pq data about the query
- * @param qbind array of parameters to initialize
+ address@hidden qbind array of parameters to initialize
address@hidden -1 on error
*/
static int
@@ -337,27 +339,6 @@
/**
- * 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
*
@@ -369,7 +350,7 @@
{
struct GNUNET_MY_QueryParam res = {
.conv = &my_conv_rsa_signature,
- .cleaner = &my_clean_rsa_signature,
+ .cleaner = &my_clean_query,
.conv_cls = NULL,
.num_params = 1,
.data = (x),
@@ -378,6 +359,7 @@
return res;
}
+
/**
* Generate query parameter for an absolute time value.
* The database must store a 64-bit integer.
@@ -391,6 +373,7 @@
return GNUNET_MY_query_param_uint64 (&x->abs_value_us);
}
+
/**
* Generate query parameter for an absolute time value.
* The database must store a 64-bit integer.
@@ -403,4 +386,5 @@
return GNUNET_MY_query_param_auto_from_type (&x->abs_value_us__);
}
-/* end of my_query_helper.c */
+
+/* end of my_query_helper.c */
\ No newline at end of file
Modified: gnunet/src/my/my_result_helper.c
===================================================================
--- gnunet/src/my/my_result_helper.c 2016-06-09 14:36:14 UTC (rev 37258)
+++ gnunet/src/my/my_result_helper.c 2016-06-09 15:39:57 UTC (rev 37259)
@@ -74,6 +74,7 @@
if (rs->mysql_bind_output_length != size)
return GNUNET_SYSERR; /* 'unsigned long' does not fit in size_t!? */
+
buf = GNUNET_malloc (size);
results[0].buffer = buf;
@@ -80,8 +81,6 @@
results[0].buffer_length = size;
results[0].buffer_type = MYSQL_TYPE_BLOB;
- fprintf(stderr, "size : %d\n", size);
-
if (0 !=
mysql_stmt_fetch_column (stmt,
results,
@@ -92,8 +91,6 @@
return GNUNET_SYSERR;
}
- printf("buf : %s\n", (char*)buf);
-
*(void **) rs->dst = buf;
*rs->result_size = size;
@@ -111,17 +108,15 @@
cleanup_varsize_blob (void *cls,
struct GNUNET_MY_ResultSpec *rs)
{
- void *ptr;
+ void **ptr = (void **)rs->dst;
- ptr = * (void **) rs->dst;
- if (NULL == ptr)
- return;
- GNUNET_free (ptr);
- *(void **) rs->dst = NULL;
- *rs->result_size = 0;
+ if (NULL != *ptr)
+ {
+ GNUNET_free (*ptr);
+ *ptr = NULL;
+ }
}
-
/**
* Variable-size result expected
*
@@ -219,6 +214,7 @@
{
.pre_conv = &pre_extract_fixed_blob,
.post_conv = &post_extract_fixed_blob,
+ .cleaner = NULL,
.dst = (void *)(ptr),
.dst_size = ptr_size,
.num_fields = 1
@@ -508,31 +504,8 @@
results[0].buffer = (char *)rs->dst;
results[0].buffer_length = rs->dst_size;
results[0].length = &rs->mysql_bind_output_length;
-/*
- char **str = rs->dst;
- size_t len;
- const char *res;
-
- *str = NULL;
-
- if (results->is_null)
- {
- return GNUNET_SYSERR;
- }
-
- len = results->buffer_length;
- res = results->buffer;
-
- *str = GNUNET_strndup (res,
- len);
-
- if (NULL == *str)
- {
- GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
- "Results contains bogus value (fail to decode)\n");
- return GNUNET_SYSERR;
- }
-*/ return GNUNET_OK;
+
+ return GNUNET_OK;
}
@@ -559,32 +532,6 @@
if (rs->dst_size != rs->mysql_bind_output_length)
return GNUNET_SYSERR;
return GNUNET_OK;
-/*
- char **str = rs->dst;
- size_t len;
- const char *res;
-
- *str = NULL;
-
- if (results->is_null)
- {
- return GNUNET_SYSERR;
- }
-
- len = results->buffer_length;
- res = results->buffer;
-
- *str = GNUNET_strndup (res,
- len);
-
- if (NULL == *str)
- {
- GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
- "Results contains bogus value (fail to decode)\n");
- return GNUNET_SYSERR;
- }
- return GNUNET_OK;
-*/
}
@@ -600,6 +547,7 @@
struct GNUNET_MY_ResultSpec res = {
.pre_conv = &pre_extract_string,
.post_conv = &post_extract_string,
+ .cleaner = NULL,
.dst = (void *) dst,
.dst_size = 0,
.num_fields = 1
@@ -704,6 +652,7 @@
struct GNUNET_MY_ResultSpec res = {
.pre_conv = &pre_extract_uint16,
.post_conv = &post_extract_uint16,
+ .cleaner = NULL,
.dst = (void *) u16,
.dst_size = sizeof (*u16),
.num_fields = 1
@@ -778,6 +727,7 @@
struct GNUNET_MY_ResultSpec res = {
.pre_conv = &pre_extract_uint32,
.post_conv = &post_extract_uint32,
+ .cleaner = NULL,
.dst = (void *) u32,
.dst_size = sizeof (*u32),
.num_fields = 1
@@ -854,6 +804,7 @@
struct GNUNET_MY_ResultSpec res = {
.pre_conv = &pre_extract_uint64,
.post_conv = &post_extract_uint64,
+ .cleaner = NULL,
.dst = (void *) u64,
.dst_size = sizeof (*u64),
.num_fields = 1
Modified: gnunet/src/my/test_my.c
===================================================================
--- gnunet/src/my/test_my.c 2016-06-09 14:36:14 UTC (rev 37258)
+++ gnunet/src/my/test_my.c 2016-06-09 15:39:57 UTC (rev 37259)
@@ -37,9 +37,9 @@
static int
run_queries (struct GNUNET_MYSQL_Context *context)
{
- struct GNUNET_CRYPTO_RsaPublicKey *pub;
+ struct GNUNET_CRYPTO_RsaPublicKey *pub = NULL;
struct GNUNET_CRYPTO_RsaPublicKey *pub2 = NULL;
- struct GNUNET_CRYPTO_RsaSignature *sig;
+ struct GNUNET_CRYPTO_RsaSignature *sig = NULL;;
struct GNUNET_CRYPTO_RsaSignature *sig2 = NULL;
struct GNUNET_TIME_Absolute abs_time = GNUNET_TIME_absolute_get ();
struct GNUNET_TIME_Absolute abs_time2;
@@ -48,7 +48,7 @@
struct GNUNET_HashCode hc;
struct GNUNET_HashCode hc2;
const char msg[] = "hello";
- void *msg2;
+ void *msg2 = NULL;;
size_t msg2_len;
uint16_t u16;
@@ -60,10 +60,10 @@
int ret;
- struct GNUNET_MYSQL_StatementHandle *statements_handle_insert;
- struct GNUNET_MYSQL_StatementHandle *statements_handle_select;
+ struct GNUNET_MYSQL_StatementHandle *statements_handle_insert = NULL;
+ struct GNUNET_MYSQL_StatementHandle *statements_handle_select = NULL;
- struct GNUNET_CRYPTO_RsaPrivateKey *priv;
+ struct GNUNET_CRYPTO_RsaPrivateKey *priv = NULL;
struct GNUNET_HashCode hmsg;
priv = GNUNET_CRYPTO_rsa_private_key_create (1024);
@@ -163,7 +163,7 @@
ret = GNUNET_MY_extract_result (statements_handle_select,
results_select);
- GNUNET_break (GNUNET_YES == ret);
+ GNUNET_assert (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 ==
@@ -171,6 +171,8 @@
&hc2,
sizeof (struct GNUNET_HashCode)));
+ GNUNET_assert (NULL != sig2);
+ GNUNET_assert (NULL != pub2);
GNUNET_break (0 ==
GNUNET_CRYPTO_rsa_signature_cmp (sig,
sig2));
@@ -192,7 +194,7 @@
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;
@@ -251,5 +253,8 @@
ret = run_queries (context);
+ GNUNET_MYSQL_context_destroy (context);
+ GNUNET_free (config);
+
return ret;
}
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [GNUnet-SVN] r37259 - in gnunet/src: include my,
gnunet <=