From 36734357812e0631d57ae1c6ef2b2d90926f52de Mon Sep 17 00:00:00 2001 From: Eric Koegel Date: Mon, 3 Nov 2014 16:42:52 +0300 Subject: [PATCH] Fix for extra blank lines in backdrop list (Bug #10763) If xfdesktop-settings contains multiple blank lines it would cause an infinite loop in the loader code, this patch fixes that. --- common/xfdesktop-common.c | 43 ++++++++++++++++++------------------------- settings/main.c | 2 ++ 2 files changed, 20 insertions(+), 25 deletions(-) diff --git a/common/xfdesktop-common.c b/common/xfdesktop-common.c index 64aee8d..2b8e80b 100644 --- a/common/xfdesktop-common.c +++ b/common/xfdesktop-common.c @@ -81,9 +81,9 @@ xfdesktop_backdrop_list_load(const gchar *filename, gint *n_items, GError **error) { - gchar *contents = NULL, **files = NULL, *p, *q; + gchar *contents = NULL, **files = NULL; gsize length = 0; - gint arr_size = 10, count = 0; + gint i, items; g_return_val_if_fail(filename && (!error || !*error), NULL); @@ -99,33 +99,26 @@ xfdesktop_backdrop_list_load(const gchar *filename, return NULL; } - /* i'd use g_strsplit() here, but then counting is slower. we can - * also filter out blank lines */ - files = g_malloc(sizeof(gchar *) * (arr_size+1)); - p = contents + sizeof(LIST_TEXT); - while(p && *p) { - q = strstr(p, "\n"); - if(q) { - if(p == q) /* blank line */ - continue; - *q = 0; - } else - q = contents + length; /* assume no trailing '\n' at EOF */ - - if(count == arr_size) { - arr_size += 10; - files = g_realloc(files, sizeof(gchar *) * (arr_size+1)); + items = 0; + files = g_strsplit(contents, "\n", -1); + + /* Since the first line is the file identifier, we need to skip it. + * Additionally, we want to skip blank lines. */ + for(i = 1; files[i] != NULL; i++) { + if(g_strcmp0(files[i], "") != 0) { + g_free(files[items]); + files[items] = g_strdup(files[i]); + DBG("files[items] %s", files[items]); + items++; } - - files[count++] = g_strdup(p); - if(q != contents + length) - p = q + 1; } - files[count] = NULL; - files = g_realloc(files, sizeof(gchar *) * (count+1)); + files[items+1] = NULL; + + files = g_realloc(files, sizeof(gchar *) * (items+1)); + DBG("items %d", items); if(n_items) - *n_items = count; + *n_items = items; g_free(contents); diff --git a/settings/main.c b/settings/main.c index 3d6166f..3d52c80 100644 --- a/settings/main.c +++ b/settings/main.c @@ -568,6 +568,7 @@ xfdesktop_settings_dialog_create_load_list(AppearancePanel *panel) gtk_main_iteration(); if(!xfdesktop_settings_ensure_backdrop_list(list_file, parent)) { + g_warning ("backdrop list is not valid"); g_free(list_file); return NULL; } @@ -660,6 +661,7 @@ xfdesktop_settings_dialog_populate_image_list(AppearancePanel *panel) for(i = 0; images[i]; ++i) { GtkTreeIter *iter = xfdesktop_settings_image_treeview_add(GTK_TREE_MODEL(ls), images[i]); + DBG("adding images[i] %s", images[i]); if(iter) gtk_tree_iter_free(iter); } -- 2.1.3