LCOV - code coverage report
Current view: top level - g10 - gpgsql.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 66 95 69.5 %
Date: 2016-09-12 13:01:59 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 <http://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         264 : 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         264 :   va_start (ap, sql);
      44         264 :   sql2 = sqlite3_vmprintf (sql, ap);
      45         264 :   va_end (ap);
      46             : 
      47             : #if 0
      48             :   log_debug ("tofo db: executing: '%s'\n", sql2);
      49             : #endif
      50             : 
      51         264 :   rc = sqlite3_exec (db, sql2, callback, cookie, errmsg);
      52             : 
      53         264 :   sqlite3_free (sql2);
      54             : 
      55         264 :   return rc;
      56             : }
      57             : 
      58             : int
      59         244 : 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         244 :   int err = 0;
      68         244 :   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         244 :   const char **azColName = 0;
      79         244 :   int callback_initialized = 0;
      80             : 
      81         244 :   const char **azVals = 0;
      82             : 
      83         244 :   callback_initialized = 0;
      84             : 
      85         244 :   if (stmtp && *stmtp)
      86             :     {
      87          84 :       stmt = *stmtp;
      88             : 
      89             :       /* Make sure this statement is associated with the supplied db.  */
      90          84 :       log_assert (db == sqlite3_db_handle (stmt));
      91             : 
      92             : #if DEBUG_TOFU_CACHE
      93             :       prepares_saved ++;
      94             : #endif
      95             :     }
      96             :   else
      97             :     {
      98         160 :       const char *tail = NULL;
      99             : 
     100         160 :       rc = sqlite3_prepare_v2 (db, sql, -1, &stmt, &tail);
     101         160 :       if (rc)
     102           0 :         log_fatal ("failed to prepare SQL: %s", sql);
     103             : 
     104             :       /* We can only process a single statement.  */
     105         160 :       if (tail)
     106             :         {
     107         320 :           while (*tail == ' ' || *tail == ';' || *tail == '\n')
     108           0 :             tail ++;
     109             : 
     110         160 :           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         160 :       if (stmtp)
     118         160 :         *stmtp = stmt;
     119             :     }
     120             : 
     121             : #if DEBUG_TOFU_CACHE
     122             :   queries ++;
     123             : #endif
     124             : 
     125         244 :   args = sqlite3_bind_parameter_count (stmt);
     126         244 :   va_start (va, sql);
     127         244 :   if (args)
     128             :     {
     129         485 :       for (i = 1; i <= args; i ++)
     130             :         {
     131         343 :           t = va_arg (va, enum gpgsql_arg_type);
     132         343 :           switch (t)
     133             :             {
     134             :             case GPGSQL_ARG_INT:
     135             :               {
     136           7 :                 int value = va_arg (va, int);
     137           7 :                 err = sqlite3_bind_int (stmt, i, value);
     138           7 :                 break;
     139             :               }
     140             :             case GPGSQL_ARG_LONG_LONG:
     141             :               {
     142          19 :                 long long value = va_arg (va, long long);
     143          19 :                 err = sqlite3_bind_int64 (stmt, i, value);
     144          19 :                 break;
     145             :               }
     146             :             case GPGSQL_ARG_STRING:
     147             :               {
     148         317 :                 char *text = va_arg (va, char *);
     149         317 :                 err = sqlite3_bind_text (stmt, i, text, -1, SQLITE_STATIC);
     150         317 :                 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         343 :           if (err)
     165             :             {
     166           0 :               log_fatal ("Error binding parameter %d\n", i);
     167             :               goto out;
     168             :             }
     169             :         }
     170             : 
     171             :     }
     172         244 :   t = va_arg (va, enum gpgsql_arg_type);
     173         244 :   log_assert (t == GPGSQL_ARG_END);
     174         244 :   va_end (va);
     175             : 
     176             :   for (;;)
     177             :     {
     178         373 :       rc = sqlite3_step (stmt);
     179             : 
     180         373 :       if (rc != SQLITE_ROW)
     181             :         /* No more data (SQLITE_DONE) or an error occurred.  */
     182         244 :         break;
     183             : 
     184         129 :       if (! callback)
     185           0 :         continue;
     186             : 
     187         129 :       if (! callback_initialized)
     188             :         {
     189         128 :           cols = sqlite3_column_count (stmt);
     190         128 :           azColName = xmalloc (2 * cols * sizeof (const char *) + 1);
     191             : 
     192         372 :           for (i = 0; i < cols; i ++)
     193         244 :             azColName[i] = sqlite3_column_name (stmt, i);
     194             : 
     195         128 :           callback_initialized = 1;
     196             :         }
     197             : 
     198         129 :       azVals = &azColName[cols];
     199         374 :       for (i = 0; i < cols; i ++)
     200             :         {
     201         245 :           azVals[i] = sqlite3_column_text (stmt, i);
     202         245 :           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         129 :       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         129 :     }
     217             : 
     218             :  out:
     219         244 :   xfree (azColName);
     220             : 
     221         244 :   if (stmtp)
     222         244 :     rc = sqlite3_reset (stmt);
     223             :   else
     224           0 :     rc = sqlite3_finalize (stmt);
     225         244 :   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         244 :   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         244 :   return rc;
     251             : }

Generated by: LCOV version 1.11