From e4d4ec26fbb1cb46a55c45d6892e478c1cc29c42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Forysiuk?= Date: Sat, 30 Jul 2011 20:11:45 +0200 Subject: [PATCH] Always NULL-check database before using it If the database is broken in whatever way the pointer is unset. A good next step would be to give more user-friendly feedback than the current rough dialog. --- midori/main.c | 8 +++----- midori/midori-browser.c | 3 +++ midori/midori-locationaction.c | 4 ++++ panels/midori-bookmarks.c | 14 +++++++++++++- panels/midori-history.c | 19 +++++++++++++++---- 5 files changed, 38 insertions(+), 10 deletions(-) diff --git a/midori/main.c b/midori/main.c index e49181df..036a2ba8 100644 --- a/midori/main.c +++ b/midori/main.c @@ -426,13 +426,11 @@ midori_history_initialize (KatzeArray* array, char** errmsg) { sqlite3* db; - gboolean has_day; + gboolean has_day = FALSE; sqlite3_stmt* stmt; gint result; gchar* sql; - has_day = FALSE; - if (sqlite3_open (filename, &db) != SQLITE_OK) { if (errmsg) @@ -2348,7 +2346,7 @@ main (int argc, { g_string_append_printf (error_messages, _("Bookmarks couldn't be loaded: %s\n"), errmsg); - g_free (errmsg); + errmsg = NULL; } else if (!bookmarks_exist) { @@ -2409,7 +2407,7 @@ main (int argc, { g_string_append_printf (error_messages, _("The history couldn't be loaded: %s\n"), errmsg); - g_free (errmsg); + errmsg = NULL; } g_free (bookmarks_file); midori_startup_timer ("History read: \t%f"); diff --git a/midori/midori-browser.c b/midori/midori-browser.c index c42492b6..108f14cd 100644 --- a/midori/midori-browser.c +++ b/midori/midori-browser.c @@ -714,6 +714,9 @@ midori_browser_edit_bookmark_dialog_new (MidoriBrowser* browser, db = g_object_get_data (G_OBJECT (browser->bookmarks), "db"); + if (!db) + return FALSE; + if (is_folder) title = new_bookmark ? _("New folder") : _("Edit folder"); else diff --git a/midori/midori-locationaction.c b/midori/midori-locationaction.c index 968af2f8..e7225cef 100644 --- a/midori/midori-locationaction.c +++ b/midori/midori-locationaction.c @@ -364,6 +364,10 @@ midori_location_action_popup_timeout_cb (gpointer data) { sqlite3* db; db = g_object_get_data (G_OBJECT (action->history), "db"); + + if (!db) + return FALSE; + sqlcmd = "SELECT type, uri, title FROM (" " SELECT 1 AS type, uri, title, count() AS ct FROM history " " WHERE uri LIKE ?1 OR title LIKE ?1 GROUP BY uri " diff --git a/panels/midori-bookmarks.c b/panels/midori-bookmarks.c index 680fef64..90cf1244 100644 --- a/panels/midori-bookmarks.c +++ b/panels/midori-bookmarks.c @@ -133,6 +133,9 @@ midori_bookmarks_export_array_db (sqlite3* db, KatzeItem* item; GList* list; + if (!db) + return; + sqlcmd = g_strdup_printf ("SELECT * FROM bookmarks where folder='%s'", folder); root_array = katze_array_from_sqlite (db, sqlcmd); g_free (sqlcmd); @@ -160,6 +163,9 @@ midori_bookmarks_import_array_db (sqlite3* db, GList* list; KatzeItem* item; + if (!db) + return; + KATZE_ARRAY_FOREACH_ITEM_L (item, array, list) { if (KATZE_IS_ARRAY (item)) @@ -181,6 +187,9 @@ midori_bookmarks_read_from_db (MidoriBookmarks* bookmarks, db = g_object_get_data (G_OBJECT (bookmarks->array), "db"); + if (!db) + return katze_array_new (KATZE_TYPE_ITEM); + if (keyword && *keyword) { gchar* filterstr; @@ -202,7 +211,7 @@ midori_bookmarks_read_from_db (MidoriBookmarks* bookmarks, } if (result != SQLITE_OK) - return NULL; + return katze_array_new (KATZE_TYPE_ITEM); return katze_array_from_statement (statement); } @@ -248,6 +257,9 @@ midori_bookmarks_insert_item_db (sqlite3* db, /* Bookmarks must have a name, import may produce invalid items */ g_return_if_fail (katze_item_get_name (item)); + if (!db) + return; + if (KATZE_ITEM_IS_BOOKMARK (item)) uri = g_strdup (katze_item_get_uri (item)); else diff --git a/panels/midori-history.c b/panels/midori-history.c index d575e6e8..975b410e 100644 --- a/panels/midori-history.c +++ b/panels/midori-history.c @@ -174,6 +174,9 @@ midori_history_remove_item_from_db (MidoriHistory* history, db = g_object_get_data (G_OBJECT (history->array), "db"); + if (!db) + return; + if (KATZE_ITEM_IS_BOOKMARK (item)) sqlcmd = sqlite3_mprintf ( "DELETE FROM history WHERE uri = '%q' AND" @@ -217,6 +220,9 @@ midori_history_read_from_db (MidoriHistory* history, db = g_object_get_data (G_OBJECT (history->array), "db"); + if (!db) + return katze_array_new (KATZE_TYPE_ITEM); + if (filter && *filter) { gchar* filterstr; @@ -249,7 +255,7 @@ midori_history_read_from_db (MidoriHistory* history, } if (result != SQLITE_OK) - return NULL; + return katze_array_new (KATZE_TYPE_ITEM); return katze_array_from_statement (statement); } @@ -327,7 +333,7 @@ midori_history_bookmark_add_cb (GtkWidget* menuitem, { GtkTreeModel* model; GtkTreeIter iter; - KatzeItem* item; + KatzeItem* item = NULL; MidoriBrowser* browser = midori_browser_get_for_widget (GTK_WIDGET (history)); if (katze_tree_view_get_selected_iter (GTK_TREE_VIEW (history->treeview), @@ -335,11 +341,12 @@ midori_history_bookmark_add_cb (GtkWidget* menuitem, gtk_tree_model_get (model, &iter, 0, &item, -1); if (KATZE_IS_ITEM (item) && katze_item_get_uri (item)) + { midori_browser_edit_bookmark_dialog_new (browser, item, TRUE, FALSE); + g_object_unref (item); + } else midori_browser_edit_bookmark_dialog_new (browser, NULL, TRUE, FALSE); - - g_object_unref (item); } static GtkWidget* @@ -674,6 +681,10 @@ midori_history_open_in_tab_activate_cb (GtkWidget* menuitem, KatzeArray* array; db = g_object_get_data (G_OBJECT (history->array), "db"); + + if (!db) + return; + sqlcmd = g_strdup_printf ("SELECT uri, title, date, day " "FROM history WHERE day = %d " "GROUP BY uri ORDER BY date ASC",