LCOV - code coverage report
Current view: top level - g10 - gpgsql.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 67 95 70.5 %
Date: 2016-11-29 15:00:56 Functions: 2 2 100.0 %

          Line data    Source code
       1             : /* gpgsql.c - SQLite helper functions.
       2             :  * Copyright (C) 2015 g10 Code GmbH
       3             :  *
       4             :  * This file is part of GnuPG.
       5             :  *
       6             :  * GnuPG is free software; you can redistribute it and/or modify
       7             :  * it under the terms of the GNU General Public License as published by
       8             :  * the Free Software Foundation; either version 3 of the License, or
       9             :  * (at your option) any later version.
      10             :  *
      11             :  * GnuPG is distributed in the hope that it will be useful,
      12             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      13             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      14             :  * GNU General Public License for more details.
      15             :  *
      16             :  * You should have received a copy of the GNU General Public License
      17             :  * along with this program; if not, see <https://www.gnu.org/licenses/>.
      18             :  */
      19             : 
      20             : #include <config.h>
      21             : #include <stdarg.h>
      22             : #include <stdlib.h>
      23             : #include <string.h>
      24             : 
      25             : #include "gpg.h"
      26             : #include "util.h"
      27             : #include "logging.h"
      28             : 
      29             : #include "gpgsql.h"
      30             : 
      31             : /* This is a convenience function that combines sqlite3_mprintf and
      32             :    sqlite3_exec.  */
      33             : int
      34         840 : gpgsql_exec_printf (sqlite3 *db,
      35             :                     int (*callback)(void*,int,char**,char**), void *cookie,
      36             :                     char **errmsg,
      37             :                     const char *sql, ...)
      38             : {
      39             :   va_list ap;
      40             :   int rc;
      41             :   char *sql2;
      42             : 
      43         840 :   va_start (ap, sql);
      44         840 :   sql2 = sqlite3_vmprintf (sql, ap);
      45         840 :   va_end (ap);
      46             : 
      47             : #if 0
      48             :   log_debug ("tofo db: executing: '%s'\n", sql2);
      49             : #endif
      50             : 
      51         840 :   rc = sqlite3_exec (db, sql2, callback, cookie, errmsg);
      52             : 
      53         840 :   sqlite3_free (sql2);
      54             : 
      55         840 :   return rc;
      56             : }
      57             : 
      58             : int
      59         721 : gpgsql_stepx (sqlite3 *db,
      60             :               sqlite3_stmt **stmtp,
      61             :               gpgsql_stepx_callback callback,
      62             :               void *cookie,
      63             :               char **errmsg,
      64             :               const char *sql, ...)
      65             : {
      66             :   int rc;
      67         721 :   int err = 0;
      68         721 :   sqlite3_stmt *stmt = NULL;
      69             : 
      70             :   va_list va;
      71             :   int args;
      72             :   enum gpgsql_arg_type t;
      73             :   int i;
      74             : 
      75             :   int cols;
      76             :   /* Names of the columns.  We initialize this lazily to avoid the
      77             :      overhead in case the query doesn't return any results.  */
      78         721 :   const char **azColName = 0;
      79         721 :   int callback_initialized = 0;
      80             : 
      81         721 :   const char **azVals = 0;
      82             : 
      83         721 :   callback_initialized = 0;
      84             : 
      85         721 :   if (stmtp && *stmtp)
      86             :     {
      87         347 :       stmt = *stmtp;
      88             : 
      89             :       /* Make sure this statement is associated with the supplied db.  */
      90         347 :       log_assert (db == sqlite3_db_handle (stmt));
      91             : 
      92             : #if DEBUG_TOFU_CACHE
      93             :       prepares_saved ++;
      94             : #endif
      95             :     }
      96             :   else
      97             :     {
      98         374 :       const char *tail = NULL;
      99             : 
     100         374 :       rc = sqlite3_prepare_v2 (db, sql, -1, &stmt, &tail);
     101         374 :       if (rc)
     102           0 :         log_fatal ("failed to prepare SQL: %s", sql);
     103             : 
     104             :       /* We can only process a single statement.  */
     105         374 :       if (tail)
     106             :         {
     107         748 :           while (*tail == ' ' || *tail == ';' || *tail == '\n')
     108           0 :             tail ++;
     109             : 
     110         374 :           if (*tail)
     111           0 :             log_fatal
     112             :               ("sqlite3_stepx can only process a single SQL statement."
     113             :                "  Second statement starts with: '%s'\n",
     114             :                tail);
     115             :         }
     116             : 
     117         374 :       if (stmtp)
     118         362 :         *stmtp = stmt;
     119             :     }
     120             : 
     121             : #if DEBUG_TOFU_CACHE
     122             :   queries ++;
     123             : #endif
     124             : 
     125         721 :   args = sqlite3_bind_parameter_count (stmt);
     126         721 :   va_start (va, sql);
     127         721 :   if (args)
     128             :     {
     129        1735 :       for (i = 1; i <= args; i ++)
     130             :         {
     131        1316 :           t = va_arg (va, enum gpgsql_arg_type);
     132        1316 :           switch (t)
     133             :             {
     134             :             case GPGSQL_ARG_INT:
     135             :               {
     136         123 :                 int value = va_arg (va, int);
     137         123 :                 err = sqlite3_bind_int (stmt, i, value);
     138         123 :                 break;
     139             :               }
     140             :             case GPGSQL_ARG_LONG_LONG:
     141             :               {
     142          91 :                 long long value = va_arg (va, long long);
     143          91 :                 err = sqlite3_bind_int64 (stmt, i, value);
     144          91 :                 break;
     145             :               }
     146             :             case GPGSQL_ARG_STRING:
     147             :               {
     148        1102 :                 char *text = va_arg (va, char *);
     149        1102 :                 err = sqlite3_bind_text (stmt, i, text, -1, SQLITE_STATIC);
     150        1102 :                 break;
     151             :               }
     152             :             case GPGSQL_ARG_BLOB:
     153             :               {
     154           0 :                 char *blob = va_arg (va, void *);
     155           0 :                 long long length = va_arg (va, long long);
     156           0 :                 err = sqlite3_bind_blob (stmt, i, blob, length, SQLITE_STATIC);
     157           0 :                 break;
     158             :               }
     159             :             default:
     160             :               /* Internal error.  Likely corruption.  */
     161           0 :               log_fatal ("Bad value for parameter type %d.\n", t);
     162             :             }
     163             : 
     164        1316 :           if (err)
     165             :             {
     166           0 :               log_fatal ("Error binding parameter %d\n", i);
     167             :               goto out;
     168             :             }
     169             :         }
     170             : 
     171             :     }
     172         721 :   t = va_arg (va, enum gpgsql_arg_type);
     173         721 :   log_assert (t == GPGSQL_ARG_END);
     174         721 :   va_end (va);
     175             : 
     176             :   for (;;)
     177             :     {
     178        1109 :       rc = sqlite3_step (stmt);
     179             : 
     180        1109 :       if (rc != SQLITE_ROW)
     181             :         /* No more data (SQLITE_DONE) or an error occurred.  */
     182         721 :         break;
     183             : 
     184         388 :       if (! callback)
     185           0 :         continue;
     186             : 
     187         388 :       if (! callback_initialized)
     188             :         {
     189         344 :           cols = sqlite3_column_count (stmt);
     190         344 :           azColName = xmalloc (2 * cols * sizeof (const char *) + 1);
     191             : 
     192        1222 :           for (i = 0; i < cols; i ++)
     193         878 :             azColName[i] = sqlite3_column_name (stmt, i);
     194             : 
     195         344 :           callback_initialized = 1;
     196             :         }
     197             : 
     198         388 :       azVals = &azColName[cols];
     199        1310 :       for (i = 0; i < cols; i ++)
     200             :         {
     201         922 :           azVals[i] = sqlite3_column_text (stmt, i);
     202         922 :           if (! azVals[i] && sqlite3_column_type (stmt, i) != SQLITE_NULL)
     203             :             /* Out of memory.  */
     204             :             {
     205           0 :               err = SQLITE_NOMEM;
     206           0 :               break;
     207             :             }
     208             :         }
     209             : 
     210         388 :       if (callback (cookie, cols, (char **) azVals, (char **) azColName, stmt))
     211             :         /* A non-zero result means to abort.  */
     212             :         {
     213           0 :           err = SQLITE_ABORT;
     214           0 :           break;
     215             :         }
     216         388 :     }
     217             : 
     218             :  out:
     219         721 :   xfree (azColName);
     220             : 
     221         721 :   if (stmtp)
     222         709 :     rc = sqlite3_reset (stmt);
     223             :   else
     224          12 :     rc = sqlite3_finalize (stmt);
     225         721 :   if (rc == SQLITE_OK && err)
     226             :     /* Local error.  */
     227             :     {
     228           0 :       rc = err;
     229           0 :       if (errmsg)
     230             :         {
     231           0 :           const char *e = sqlite3_errstr (err);
     232           0 :           size_t l = strlen (e) + 1;
     233           0 :           *errmsg = sqlite3_malloc (l);
     234           0 :           if (! *errmsg)
     235           0 :             log_fatal ("Out of memory.\n");
     236           0 :           memcpy (*errmsg, e, l);
     237             :         }
     238             :     }
     239         721 :   else if (rc != SQLITE_OK && errmsg)
     240             :     /* Error reported by sqlite.  */
     241             :     {
     242           0 :       const char * e = sqlite3_errmsg (db);
     243           0 :       size_t l = strlen (e) + 1;
     244           0 :       *errmsg = sqlite3_malloc (l);
     245           0 :       if (! *errmsg)
     246           0 :         log_fatal ("Out of memory.\n");
     247           0 :       memcpy (*errmsg, e, l);
     248             :     }
     249             : 
     250         721 :   return rc;
     251             : }

Generated by: LCOV version 1.11