src/config.c
changeset 51 5d9aed3948a6
parent 47 a689b6a8e6ed
child 58 500a5ea7fcb8
--- a/src/config.c
+++ b/src/config.c
@@ -37,7 +37,10 @@
     while (CONFIG_OPTIONS[map] != NULL)
         map++;
 
-    line = calloc ((size_t) (map + 1), sizeof (char *));
+    if ((line = calloc ((size_t) (map + 1), sizeof (char *))) == NULL) {
+        fclose (f);
+        return -1;
+    }
 
     /*
      * We can easily provide default values ...
@@ -50,9 +53,7 @@
             break;
         token = buffer;
 
-        while (*token == '\t')  /* Eat trailing tabs */
-            token++;
-        while (*token == ' ')   /* Eat trailing whitespaces */
+        while (*token == '\t' || *token == ' ') /* Eat trailing tabs and whitespaces */
             token++;
 
         token = strtok (token, ":");
@@ -62,20 +63,20 @@
             map = 0;
             while (CONFIG_OPTIONS[map] != NULL) {
                 if (!strcmp (CONFIG_OPTIONS[map], token)) {
-                    if (value)
+                    if (value) {
+                        if (line[map] != '\0')
+                            free (line[map]);
                         line[map] = compat_strdup (value);
+                    }
                 }
                 map++;
             }
         }
     }
 
-    if (fclose (f) != 0)
-        return -1;
-
     map = 0;
     while (CONFIG_OPTIONS[map] != NULL) {
-        if (line[map] != NULL)
+        if (line[map] != '\0')
             switch (map) {
             case NICK:
                 uc->nick = compat_strdup (line[map]);
@@ -105,8 +106,10 @@
         map++;
     }
 
-    if (line != NULL)
-        free (line);
+    free (line);
+
+    if (fclose (f) != 0)
+        return -1;
 
     if (!(uc->nick && uc->server && uc->channel))
         return -2;