[sr-dev] git:3.3: modules_k/db_sqlite: fix crash with computed fields in custom queries

Timo Teras timo.teras at iki.fi
Mon Nov 19 10:43:37 CET 2012


Module: sip-router
Branch: 3.3
Commit: bab07e07858464d50d310bbb52431a0b171ee771
URL:    http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=bab07e07858464d50d310bbb52431a0b171ee771

Author: Timo Teräs <timo.teras at iki.fi>
Committer: Timo Teräs <timo.teras at 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 at 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 c1d3b71..c4df4ce 100644
--- a/modules_k/db_sqlite/dbase.c
+++ b/modules_k/db_sqlite/dbase.c
@@ -288,6 +288,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;
@@ -348,10 +366,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) {




More information about the sr-dev mailing list