gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[GNUnet-SVN] r37249 - gnunet/src/my


From: gnunet
Subject: [GNUnet-SVN] r37249 - gnunet/src/my
Date: Tue, 7 Jun 2016 13:47:47 +0200

Author: grothoff
Date: 2016-06-07 13:47:47 +0200 (Tue, 07 Jun 2016)
New Revision: 37249

Modified:
   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 misc issues

Modified: gnunet/src/my/my.c
===================================================================
--- gnunet/src/my/my.c  2016-06-07 11:20:50 UTC (rev 37248)
+++ gnunet/src/my/my.c  2016-06-07 11:47:47 UTC (rev 37249)
@@ -39,7 +39,6 @@
       #GNUNET_YES if we can prepare all statement
       #GNUNET_SYSERR if we can't prepare all statement
  */
-
 int
 GNUNET_MY_exec_prepared (struct GNUNET_MYSQL_Context *mc,
                          struct GNUNET_MYSQL_StatementHandle *sh,
@@ -82,16 +81,16 @@
       return GNUNET_SYSERR;
     }
 
+    if (mysql_stmt_execute (stmt))
+    {
+      GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "mysql",
+                       _("`%s' failed at %s:%d with error: %s\n"),
+                       "mysql_stmt_execute", __FILE__, __LINE__,
+                       mysql_stmt_error (stmt));
+      GNUNET_MYSQL_statements_invalidate (mc);
+      return GNUNET_SYSERR;
+    }
   }
-  if (mysql_stmt_execute (stmt))
-  {
-    GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "mysql",
-                     _("`%s' failed at %s:%d with error: %s\n"),
-                     "mysql_stmt_execute", __FILE__, __LINE__,
-                     mysql_stmt_error (stmt));
-    GNUNET_MYSQL_statements_invalidate (mc);
-    return GNUNET_SYSERR;
-  }
 
   return GNUNET_OK;
 }
@@ -175,7 +174,7 @@
     }
 
     ret = mysql_stmt_fetch (stmt);
-   
+
     if (MYSQL_NO_DATA == ret)
       return GNUNET_NO;
     if ((0 != ret ) & (MYSQL_DATA_TRUNCATED != ret))

Modified: gnunet/src/my/my_query_helper.c
===================================================================
--- gnunet/src/my/my_query_helper.c     2016-06-07 11:20:50 UTC (rev 37248)
+++ gnunet/src/my/my_query_helper.c     2016-06-07 11:47:47 UTC (rev 37249)
@@ -89,7 +89,7 @@
   address@hidden cls closure
   address@hidden pq data about the query
  * @param qbind array of parameters to initialize
-  address@hidden -1 on error 
+  address@hidden -1 on error
   */
 static int
 my_conv_uint16 (void *cls,
@@ -124,7 +124,7 @@
 struct GNUNET_MY_QueryParam
 GNUNET_MY_query_param_uint16 (const uint16_t *x)
 {
-  struct GNUNET_MY_QueryParam res = { 
+  struct GNUNET_MY_QueryParam res = {
       .conv = &my_conv_uint16,
       .conv_cls = NULL,
       .num_params = 1,
@@ -141,9 +141,9 @@
   address@hidden cls closure
   address@hidden pq data about the query
  * @param qbind array of parameters to initialize
-  address@hidden -1 on error 
+  address@hidden -1 on error
   */
-static int 
+static int
 my_conv_uint32 (void *cls,
                 const struct GNUNET_MY_QueryParam *qp,
                 MYSQL_BIND *qbind)
@@ -176,10 +176,10 @@
     .conv = &my_conv_uint32,
     .conv_cls = NULL,
     .num_params = 1,
-    .data = x, 
+    .data = x,
     .data_len = sizeof (*x)
   };
-  
+
   return res;
 }
 
@@ -189,7 +189,7 @@
   address@hidden cls closure
   address@hidden pq data about the query
  * @param qbind array of parameters to initialize
-  address@hidden -1 on error 
+  address@hidden -1 on error
   */
 static int
 my_conv_uint64 (void *cls,
@@ -209,7 +209,7 @@
   qbind->buffer_length = sizeof (uint64_t);
   qbind->buffer_type = MYSQL_TYPE_LONGLONG;
 
-  return 1;  
+  return 1;
 }
 
 /**
@@ -237,7 +237,7 @@
   address@hidden cls closure
   address@hidden pq data about the query
  * @param qbind array of parameters to initialize
-  address@hidden -1 on error 
+  address@hidden -1 on error
   */
 static int
 my_conv_rsa_public_key (void *cls,
@@ -249,16 +249,17 @@
     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);
 
-    qbind->buffer = (void *)buf;
-    qbind->buffer_length = buf_size-1;
+    qbind->buffer = (void *) buf;
+    qbind->buffer_length = buf_size;
     qbind->buffer_type = MYSQL_TYPE_BLOB;
 
     return 1;
   }
 
+
   /**
     * Generate query parameter for an RSA public key. The
     * database must contain a BLOB type in the respective position.
@@ -280,6 +281,7 @@
   return res;
 }
 
+
 /**
   * Function called to convert input argument into SQL parameters
   *
@@ -286,12 +288,12 @@
   address@hidden cls closure
   address@hidden pq data about the query
  * @param qbind array of parameters to initialize
-  address@hidden -1 on error 
+  address@hidden -1 on error
   */
-static int 
+static int
 my_conv_rsa_signature (void *cls,
-                      const struct GNUNET_MY_QueryParam *qp,
-                      MYSQL_BIND * qbind)
+                       const struct GNUNET_MY_QueryParam *qp,
+                       MYSQL_BIND *qbind)
 {
   const struct GNUNET_CRYPTO_RsaSignature *sig = qp->data;
   char *buf;
@@ -299,16 +301,16 @@
 
   GNUNET_assert(1 == qp->num_params);
 
-  buf_size = GNUNET_CRYPTO_rsa_signature_encode(sig,
-                                                &buf);
-
-  qbind->buffer = (void *)buf;
-  qbind->buffer_length = buf_size-1;
+  buf_size = GNUNET_CRYPTO_rsa_signature_encode (sig,
+                                                 &buf);
+  qbind->buffer = (void *) buf;
+  qbind->buffer_length = buf_size;
   qbind->buffer_type = MYSQL_TYPE_BLOB;
 
   return 1;
 }
 
+
 /**
   * Generate query parameter for an RSA signature. The
   * database must contain a BLOB type in the respective position
@@ -354,4 +356,4 @@
   return GNUNET_MY_query_param_auto_from_type (&x->abs_value_us__);
 }
 
-/* end of my_query_helper.c */
\ No newline at end of file
+/* end of my_query_helper.c */

Modified: gnunet/src/my/my_result_helper.c
===================================================================
--- gnunet/src/my/my_result_helper.c    2016-06-07 11:20:50 UTC (rev 37248)
+++ gnunet/src/my/my_result_helper.c    2016-06-07 11:47:47 UTC (rev 37249)
@@ -1,12 +1,15 @@
  /*
   This file is part of GNUnet
   Copyright (C) 2014, 2015, 2016 GNUnet e.V.
+
   GNUnet is free software; you can redistribute it and/or modify it under the
   terms of the GNU General Public License as published by the Free Software
   Foundation; either version 3, or (at your option) any later version.
+
   GNUnet is distributed in the hope that it will be useful, but WITHOUT ANY
   WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
   A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+
   You should have received a copy of the GNU General Public License along with
   GNUnet; see the file COPYING.  If not, If not, see 
<http://www.gnu.org/licenses/>
 */
@@ -225,6 +228,7 @@
   return res;
 }
 
+
 /**
   * Extract data from a Mysql database @a result at row @a row
   *
@@ -239,12 +243,11 @@
   *   #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
   */
 static int
-pre_extract_rsa_public_key  (void *cls,
-                        struct GNUNET_MY_ResultSpec *rs,
-                        MYSQL_STMT *stmt,
-                        unsigned int column,
-                        MYSQL_BIND *results)
-
+pre_extract_rsa_public_key (void *cls,
+                            struct GNUNET_MY_ResultSpec *rs,
+                            MYSQL_STMT *stmt,
+                            unsigned int column,
+                            MYSQL_BIND *results)
 {
   results[0].buffer = NULL;
   results[0].buffer_length = 0;
@@ -271,35 +274,41 @@
   */
 static int
 post_extract_rsa_public_key  (void *cls,
-                        struct GNUNET_MY_ResultSpec *rs,
-                        MYSQL_STMT *stmt,
-                        unsigned int column,
-                        MYSQL_BIND *results)
+                              struct GNUNET_MY_ResultSpec *rs,
+                              MYSQL_STMT *stmt,
+                              unsigned int column,
+                              MYSQL_BIND *results)
 
 {
   struct GNUNET_CRYPTO_RsaPublicKey **pk = rs->dst;
-
+  void *buf;
   size_t size;
-  char *res;
 
-  results[0].buffer = res;
+  size = (size_t) rs->mysql_bind_output_length;
+
+  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;
   results[0].buffer_length = size;
-
-  if (0 != 
-        mysql_stmt_fetch_column (stmt,
-                                results,
-                                column,
-                                0))
+  results[0].buffer_type = MYSQL_TYPE_BLOB;
+  if (0 !=
+      mysql_stmt_fetch_column (stmt,
+                               results,
+                               column,
+                               0))
   {
-    return GNUNET_SYSERR; 
+    GNUNET_free (buf);
+    return GNUNET_SYSERR;
   }
-
-  *pk = GNUNET_CRYPTO_rsa_public_key_decode (res,
-                                            size);
+  *pk = GNUNET_CRYPTO_rsa_public_key_decode (buf,
+                                             size);
+  GNUNET_free (buf);
   if (NULL == *pk)
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                "Results contains bogus value (fail to decode)\n");
+                "Results contains bogus public key value (fail to decode)\n");
     return GNUNET_SYSERR;
   }
 
@@ -324,7 +333,6 @@
   {
     GNUNET_CRYPTO_rsa_public_key_free (*pk);
     *pk = NULL;
-    *rs->result_size = 0;
   }
 }
 
@@ -378,6 +386,8 @@
 
   return GNUNET_OK;
 }
+
+
 /**
   * Extract data from a Mysql database @a result at row @a row.
   *
@@ -399,31 +409,37 @@
                       MYSQL_BIND *results)
 {
   struct GNUNET_CRYPTO_RsaSignature **sig = rs->dst;
-  size_t size = 0 ;
-  char *res = NULL;
+  void *buf;
+  size_t size;
 
-  results[0].buffer = res;
+  size = (size_t) rs->mysql_bind_output_length;
+
+  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;
   results[0].buffer_length = size;
-
-  if (0 != 
+  results[0].buffer_type = MYSQL_TYPE_BLOB;
+  if (0 !=
       mysql_stmt_fetch_column (stmt,
-                              results,
-                              column,
-                              0))
+                               results,
+                               column,
+                               0))
   {
+    GNUNET_free (buf);
     return GNUNET_SYSERR;
   }
 
-  *sig = GNUNET_CRYPTO_rsa_signature_decode (res,
-                                            size);
-
-  if (NULL != *sig)
+  *sig = GNUNET_CRYPTO_rsa_signature_decode (buf,
+                                             size);
+  GNUNET_free (buf);
+  if (NULL == *sig)
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                "Resuls contains bogus value (fails to decode)\n");
+                "Resuls contains bogus signature value (fails to decode)\n");
     return GNUNET_SYSERR;
   }
-
   return GNUNET_OK;
 }
 
@@ -445,7 +461,6 @@
   {
     GNUNET_CRYPTO_rsa_signature_free (*sig);
     *sig = NULL;
-    rs->result_size = 0;
   }
 }
 
@@ -523,7 +538,7 @@
 
 
 /**
-  * Check size of extracted fixed size data from a Mysql database @a 
+  * Check size of extracted fixed size data from a Mysql database @a
   *
   * @param cls closure
   * @param result where to extract data from
@@ -545,7 +560,7 @@
   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;

Modified: gnunet/src/my/test_my.c
===================================================================
--- gnunet/src/my/test_my.c     2016-06-07 11:20:50 UTC (rev 37248)
+++ gnunet/src/my/test_my.c     2016-06-07 11:47:47 UTC (rev 37249)
@@ -37,7 +37,7 @@
 static int
 run_queries (struct GNUNET_MYSQL_Context *context)
 {
-     struct GNUNET_CRYPTO_RsaPublicKey *pub;
+  struct GNUNET_CRYPTO_RsaPublicKey *pub;
      struct GNUNET_CRYPTO_RsaPublicKey *pub2 = NULL;
      struct GNUNET_CRYPTO_RsaSignature *sig;
      struct GNUNET_CRYPTO_RsaSignature *sig2 = NULL;
@@ -154,12 +154,12 @@
                                                                  " ORDER BY 
abs_time DESC "
                                                                  " LIMIT 1;");
 
-*/ 
+*/
      statements_handle_select = GNUNET_MYSQL_statement_prepare (context,
                                                                  "SELECT"
-                                                                 //" pub"
-                                                                 //" sig"
-                                                                 " abs_time"
+                                                                 " pub"
+                                                                 ",sig"
+                                                                 ",abs_time"
                                                                  ",forever"
                                                                  ",hash"
                                                                  ",vsize"
@@ -201,8 +201,8 @@
      };
 */
      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_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),
@@ -244,12 +244,11 @@
 }
 
 
-int 
+int
 main (int argc, const char * const argv[])
 {
      struct GNUNET_CONFIGURATION_Handle *config;
      struct GNUNET_MYSQL_Context *context;
-
      int ret;
 
      GNUNET_log_setup (  "test-my",
@@ -257,12 +256,6 @@
                          NULL);
 
      config = GNUNET_CONFIGURATION_create ();
-     if (NULL == config)
-     {
-          fprintf (stderr, "Failed to create a configuration\n");
-          return 1;
-     }
-
      if (GNUNET_OK != GNUNET_CONFIGURATION_parse (config, "test_my.conf"))
      {
           fprintf (stderr, "Failed to parse configuaration\n");
@@ -269,7 +262,7 @@
           return 1;
      }
 
-     context = GNUNET_MYSQL_context_create (config, 
+     context = GNUNET_MYSQL_context_create (config,
                                              "datastore-mysql");
      if (NULL == context)
      {
@@ -277,24 +270,26 @@
           return 77;
      }
 
+     (void) GNUNET_MYSQL_statement_run (context,
+                                        "DROP TABLE test_my2;");
      if (GNUNET_OK != GNUNET_MYSQL_statement_run (context,
-                                                  "CREATE TABLE test_my2("
-                                                  "pub BLOB NOT NULL"
-                                                  ", sig BLOB NOT NULL"
-                                                  ", abs_time BIGINT NOT NULL"
-                                                  ", forever BIGINT NOT NULL"
-                                                  ", hash BLOB NOT NULL 
CHECK(LENGTH(hash)=64)"
-                                                  ", vsize BLOB NOT NULL"
-                                                  ", u16 SMALLINT NOT NULL"
-                                                  ", u32 INT NOT NULL"
-                                                  ", u64 BIGINT NOT NULL"
+                                                  "CREATE TABLE IF NOT EXISTS 
test_my2("
+                                                  " pub BLOB NOT NULL"
+                                                  ",sig BLOB NOT NULL"
+                                                  ",abs_time BIGINT NOT NULL"
+                                                  ",forever BIGINT NOT NULL"
+                                                  ",hash BLOB NOT NULL 
CHECK(LENGTH(hash)=64)"
+                                                  ",vsize BLOB NOT NULL"
+                                                  ",u16 SMALLINT NOT NULL"
+                                                  ",u32 INT NOT NULL"
+                                                  ",u64 BIGINT NOT NULL"
                                                   ")"))
      {
-          fprintf (stderr, 
-                    "Failed to create table \n"); 
-          GNUNET_MYSQL_statements_invalidate (context);    
+          fprintf (stderr,
+                    "Failed to create table \n");
+          GNUNET_MYSQL_statements_invalidate (context);
           GNUNET_MYSQL_context_destroy (context);
-          
+
           return 1;
      }
 
@@ -308,11 +303,11 @@
                                                   ", u64 BIGINT NOT NULL"
                                                   ")"))
      {
-          fprintf (stderr, 
-                    "Failed to create table \n"); 
-          GNUNET_MYSQL_statements_invalidate (context);    
+          fprintf (stderr,
+                    "Failed to create table \n");
+          GNUNET_MYSQL_statements_invalidate (context);
           GNUNET_MYSQL_context_destroy (context);
-          
+
           return 1;
      }
 */




reply via email to

[Prev in Thread] Current Thread [Next in Thread]