[Xfce4-commits] <midori:master> Always NULL-check database before using it

Christian Dywan noreply at xfce.org
Sat Jul 30 20:18:01 CEST 2011


Updating branch refs/heads/master
         to e4d4ec26fbb1cb46a55c45d6892e478c1cc29c42 (commit)
       from 5f9497358c4361420f859bed750f51d1f6a689dd (commit)

commit e4d4ec26fbb1cb46a55c45d6892e478c1cc29c42
Author: Paweł Forysiuk <tuxator at o2.pl>
Date:   Sat Jul 30 20:11:45 2011 +0200

    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 e49181d..036a2ba 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 c42492b..108f14c 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 968af2f..e7225ce 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 680fef6..90cf124 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 d575e6e..975b410 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",


More information about the Xfce4-commits mailing list