Module: sip-router
Branch: 3.2
Commit: 4d674b9715873d06f3856215a6da3f5594b30f73
URL:
http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=4d674b9…
Author: Timo Teräs <timo.teras(a)iki.fi>
Committer: Timo Teräs <timo.teras(a)iki.fi>
Date: Thu Nov 15 16:11:41 2012 +0200
modules_k/db_sqlite: fix crash with computed fields in custom queries
Computed fields do not have decltype available, so guess the proper
field type based on the result type of the first row. This does not
work if the first row has null type as result, but is the best we can
do easily and fixes gives right result in most cases.
Reported-by: Pedro Antonio Vico Solano <pvsolano(a)amper.es>
(cherry picked from commit 09205865f98136e0354539f09f4961ca016a915b)
---
modules_k/db_sqlite/dbase.c | 28 +++++++++++++++++++++++++++-
1 files changed, 27 insertions(+), 1 deletions(-)
diff --git a/modules_k/db_sqlite/dbase.c b/modules_k/db_sqlite/dbase.c
index d5dc058..f88ce7a 100644
--- a/modules_k/db_sqlite/dbase.c
+++ b/modules_k/db_sqlite/dbase.c
@@ -265,6 +265,24 @@ static int decltype_to_dbtype(const char *decltype)
return DB1_INT;
}
+static int type_to_dbtype(int type)
+{
+ switch (type) {
+ case SQLITE_INTEGER:
+ return DB1_INT;
+ case SQLITE_FLOAT:
+ return DB1_DOUBLE;
+ case SQLITE_TEXT:
+ return DB1_STR;
+ case SQLITE_BLOB:
+ return DB1_BLOB;
+ default:
+ /* Unknown, or NULL column value. Assume this is a
+ * string. */
+ return DB1_STR;
+ }
+}
+
static str* str_dup(const char *_s)
{
str *s;
@@ -325,10 +343,18 @@ int db_sqlite_store_result(const db1_con_t* _h, db1_res_t** _r)
RES_COL_N(res) = rc;
for (i = 0; i < RES_COL_N(res); i++) {
+ const char *decltype;
+ int dbtype;
+
RES_NAMES(res)[i] = str_dup(sqlite3_column_name(conn->stmt, i));
if (RES_NAMES(res)[i] == NULL)
goto no_mem;
- RES_TYPES(res)[i] = decltype_to_dbtype(sqlite3_column_decltype(conn->stmt, i));
+ decltype = sqlite3_column_decltype(conn->stmt, i);
+ if (decltype != NULL)
+ dbtype = decltype_to_dbtype(decltype);
+ else
+ dbtype = type_to_dbtype(sqlite3_column_type(conn->stmt, i));
+ RES_TYPES(res)[i] = dbtype;
}
}
if (num_rows >= num_alloc) {