From fd76adc98ba2b959083dfbbc972d621a5ffe7abf Mon Sep 17 00:00:00 2001 From: Kern Sibbald Date: Sun, 6 Dec 2009 20:33:05 +0100 Subject: [PATCH] Attempt to fix SQLite seg fault when listing fields --- bacula/src/cats/sqlite.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/bacula/src/cats/sqlite.c b/bacula/src/cats/sqlite.c index 003036647a..946a87e8fa 100644 --- a/bacula/src/cats/sqlite.c +++ b/bacula/src/cats/sqlite.c @@ -1,7 +1,7 @@ /* Bacula® - The Network Backup Solution - Copyright (C) 2000-2007 Free Software Foundation Europe e.V. + Copyright (C) 2000-2009 Free Software Foundation Europe e.V. The main author of Bacula is Kern Sibbald, with contributions from many others, a complete list can be found in the file AUTHORS. @@ -356,7 +356,7 @@ bool db_sql_query(B_DB *mdb, const char *query, DB_RESULT_HANDLER *result_handle rh_data.result_handler = result_handler; rh_data.ctx = ctx; stat = sqlite_exec(mdb->db, query, sqlite_result, (void *)&rh_data, &mdb->sqlite_errmsg); - if (stat != 0) { + if (stat != SQLITE_OK) { Mmsg(mdb->errmsg, _("Query failed: %s: ERR=%s\n"), query, sql_strerror(mdb)); db_unlock(mdb); return false; @@ -383,14 +383,17 @@ int my_sqlite_query(B_DB *mdb, const char *cmd) } stat = sqlite_get_table(mdb->db, (char *)cmd, &mdb->result, &mdb->nrow, &mdb->ncolumn, &mdb->sqlite_errmsg); - mdb->row = 0; /* row fetched */ + mdb->row = 0; /* no row fetched yet */ + if (stat != 0) { /* something went wrong */ + mdb->nrow = mdb->ncolumn = 0; + } return stat; } /* Fetch one row at a time */ SQL_ROW my_sqlite_fetch_row(B_DB *mdb) { - if (mdb->row >= mdb->nrow) { + if (!mdb->result || (mdb->row >= mdb->nrow)) { return NULL; } mdb->row++; @@ -425,6 +428,7 @@ void my_sqlite_field_seek(B_DB *mdb, int field) { int i, j; if (mdb->result == NULL) { + mdb->field = 0; return; } /* On first call, set up the fields */ @@ -451,7 +455,9 @@ void my_sqlite_field_seek(B_DB *mdb, int field) } mdb->fields_defined = true; } - if (field > sql_num_fields(mdb) - 1) { + if (sql_num_fields(mdb) <= 0) { + field = 0; + } else if (field > sql_num_fields(mdb) - 1) { field = sql_num_fields(mdb) - 1; } mdb->field = field; -- 2.39.5