changeset 34622:e96ee4cac59f

Revise listSet(). Improve doxygen comments, replace for loop with index by while loop with pointer, fix bug with wrong allocation size, and check whether allocation succeeded.
author ib
date Mon, 13 Feb 2012 12:18:03 +0000
parents 97148652b0c6
children f8b848c178e5
files gui/util/list.c
diffstat 1 files changed, 16 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/gui/util/list.c	Mon Feb 13 10:22:02 2012 +0000
+++ b/gui/util/list.c	Mon Feb 13 12:18:03 2012 +0000
@@ -190,22 +190,32 @@
 }
 
 /**
- * \brief This actually creates a new list containing only one element...
+ * @brief Set list to @a entry.
+ *
+ * @param list pointer to the char pointer list
+ * @param entry the new (and only) element of the list
+ *
+ * @note Actually, a new list will be created and the old list will be freed.
  */
 void listSet(char ***list, const char *entry)
 {
-    int i;
+    if (*list) {
+        char **l = *list;
 
-    if (*list) {
-        for (i = 0; (*list)[i]; i++)
-            free((*list)[i]);
+        while (*l) {
+            free(*l);
+            l++;
+        }
 
         free(*list);
     }
 
-    *list      = malloc(2 * sizeof(char **));
+    *list      = malloc(2 * sizeof(char *));
+
+    if (*list) {
     (*list)[0] = gstrdup(entry);
     (*list)[1] = NULL;
+    }
 }
 
 /**