gui/model: Make sure that the queue model can represent a NULL queue

Otherwise this could lead to crashes or buggy behavior.

Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
This commit is contained in:
Anna Schumaker 2016-08-03 14:25:55 -04:00
parent 40bce8cc59
commit 65b43c7ae5
2 changed files with 13 additions and 3 deletions

View File

@ -32,7 +32,7 @@ static gboolean __queue_model_iter_nth(GuiQueueModel *model,
GtkTreeIter *iter,
gint n)
{
if (n >= queue_size(model->gqm_queue))
if (!model->gqm_queue || n >= queue_size(model->gqm_queue))
return FALSE;
queue_iter_set(model->gqm_queue, &model->gqm_iter, n);
@ -175,7 +175,7 @@ static gboolean _queue_model_iter_has_child(GtkTreeModel *model,
static gint _queue_model_iter_n_children(GtkTreeModel *model,
GtkTreeIter *iter)
{
if (iter != NULL)
if (iter != NULL || !queue_model->gqm_queue)
return 0;
return queue_size(GUI_QUEUE_MODEL(model)->gqm_queue);
}
@ -236,7 +236,11 @@ static void _queue_tree_model_init(GtkTreeModelIface *iface)
static void __gui_queue_model_set_runtime(void)
{
gchar *len = string_sec2str_long(queue_model->gqm_queue->q_length);
gchar *len = NULL;
if (queue_model->gqm_queue)
len = string_sec2str_long(queue_model->gqm_queue->q_length);
gtk_label_set_text(GTK_LABEL(gui_builder_widget("o_runtime")), len);
g_free(len);
}

View File

@ -261,6 +261,12 @@ static void test_model()
test_equal(gtk_tree_model_iter_n_children(model, NULL), 3);
test_equal(gtk_label_get_text(label), "10 minutes, 46 seconds");
gui_queue_model_set_queue(NULL);
test_equal(count_delete, 16);
test_equal(count_insert, 14);
test_equal(gtk_tree_model_iter_n_children(model, NULL), 0);
test_equal(gtk_label_get_text(label), "");
gui_queue_model_set_queue(playlist_get_queue(PL_SYSTEM, "Collection"));
test_equal(gtk_label_get_text(label), "42 minutes, 45 seconds");
test_equal(count_delete, 16);