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.
This commit is contained in:
Paweł Forysiuk 2011-07-30 20:11:45 +02:00 committed by Christian Dywan
parent 5f9497358c
commit e4d4ec26fb
5 changed files with 38 additions and 10 deletions

View file

@ -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");

View file

@ -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

View file

@ -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 "

View file

@ -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

View file

@ -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",