Many Memory Leaks in config.c
authorMarkus Bröker <mbroeker@largo.dyndns.tv>
Sat, 13 Dec 2008 15:40:14 +0100
changeset 11 a769385a59c6
parent 10 311ea5fa60dd
child 12 213c3d4abc66
Many Memory Leaks in config.c * strdup is safer Still reachable memory holes in irc.c * theLine is strdup(ed) and not released. committer: Markus Bröker <mbroeker@largo.homelinux.org>
include/config.h
src/config.c
src/irc.c
src/main.c
src/parse.c
--- a/include/config.h
+++ b/include/config.h
@@ -19,5 +19,4 @@
 typedef struct UserConfig UC;
 
 int config (UC *, char *);
-
 #endif
--- a/src/config.c
+++ b/src/config.c
@@ -13,7 +13,7 @@
 const
 char *CONFIG_OPTIONS[] = {
     "NICK", "PASSWORD", "SERVER", "PORT",
-    "CHANNEL", "TOPIC",
+    "CHANNEL", "TOPIC", NULL
 };
 
 int config (UC * uc, char *fname)
@@ -31,7 +31,7 @@
     while (CONFIG_OPTIONS[i] != NULL)
         i++;
 
-    line = calloc (i, sizeof (char *));
+    line = calloc ((size_t) (i + 1), sizeof (char *));
 
     /*
      * We can easily provide default values ...
@@ -39,22 +39,14 @@
     uc->nick = uc->pass = uc->server = uc->channel = uc->topic = NULL;
     uc->port = 6667;
 
-    *buffer = 0;
-    fgets (buffer, 512, f);
-
-    token = buffer;
+    while (!feof (f)) {
+        *buffer = '\0';
+        (void)fgets (buffer, 512, f);
+        token = buffer;
 
-    while (!feof (f)) {
-        /*
-         * eat trailing tabs
-         */
-        while (*token == '\t')
+        while (*token == '\t')  /* Eat trailing tabs */
             token++;
-
-        /*
-         * eat trailing whitespaces
-         */
-        while (*token == ' ')
+        while (*token == ' ')   /* Eat trailing whitespaces */
             token++;
 
         token = strtok (token, ":");
@@ -63,15 +55,13 @@
             value = strtok (NULL, "\n");
             i = 0;
             while (CONFIG_OPTIONS[i] != NULL) {
-                if (!strcmp (token, CONFIG_OPTIONS[i])) {
-                    line[i] = malloc (strlen (value) + 1);
-                    strncpy (line[i], value, strlen (value));
+                if (!strcmp (CONFIG_OPTIONS[i], token)) {
+                    if (value)
+                        line[i] = strdup (value);
                 }
                 i++;
             }
         }
-        *buffer = 0;
-        fgets (buffer, 512, f);
     }
 
     if (fclose (f) != 0)
@@ -82,27 +72,36 @@
         if (line[i] != NULL)
             switch (i) {
             case 0:            /* NICK */
-                uc->nick = line[i];
+                uc->nick = strdup (line[i]);
+                free (line[i]);
                 break;
             case 1:            /* PASSWORD */
-                uc->pass = line[i];
+                uc->pass = strdup (line[i]);
+                free (line[i]);
                 break;
             case 2:            /* SERVER */
-                uc->server = line[i];
+                uc->server = strdup (line[i]);
+                free (line[i]);
                 break;
             case 3:            /* PORT */
                 uc->port = atoi (line[i]);
+                free (line[i]);
                 break;
             case 4:            /* CHANNEL */
-                uc->channel = line[i];
+                uc->channel = strdup (line[i]);
+                free (line[i]);
                 break;
             case 5:            /* TOPIC */
-                uc->topic = line[i];
+                uc->topic = strdup (line[i]);
+                free (line[i]);
                 break;
             }
         i++;
     }
 
+    if (line != NULL)
+        free (line);
+
     if (!(uc->nick && uc->pass && uc->server && uc->channel))
         return -2;
 
--- a/src/irc.c
+++ b/src/irc.c
@@ -25,6 +25,7 @@
     "NOTICE", "MODE", "JOIN", "PART",
     "TOPIC", "PING", "ENOMEM", "ERROR",
     "VERSION", "PRIVMSG", "QUIT", "NICK",
+    NULL
 };
 
 FILE *irc_connect (char *server, unsigned int port)
@@ -103,7 +104,7 @@
     fprintf (stream, "PRIVMSG NICKSERV :IDENTIFY %s\r\n", password);
 
     for (;;) {
-        *msg = 0;
+        *msg = '\0';
         fgets (msg, 512, stream);
         if ((user = irc_parsemessage (msg, &message)) != NULL)
             printf ("%10s %s\n", user, message.line);
@@ -231,13 +232,13 @@
                     return command;
                 case 8:        /* VERSION */
                     if ((ptr = strchr (message->user, ' ')))
-                        *ptr = 0;
+                        *ptr = '\0';
                     return command;
                 case 9:        /* PRIVMSG */
                     if ((message->email = strchr (message->user, '=')))
                         ++message->email;
                     if ((ptr = strchr (message->user, '!')))
-                        *ptr = 0;
+                        *ptr = '\0';
 
                     message->channel = strtok (message->line, " ");
                     message->current_channel = message->channel;
@@ -314,7 +315,6 @@
             case 375:
             case 376:          /* END OF MOTD */
                 return command;
-                break;
             case 401:          /* NO SUCH NICK/CHANNEL */
             case 403:          /* THAT CHANNEL DOESN'T EXIST */
             case 441:          /* THEY AREN'T ON THIS CHANNEL */
--- a/src/main.c
+++ b/src/main.c
@@ -90,12 +90,12 @@
     }
 
     while (!feof (message.stream)) {
-        *buf = 0;
+        *buf = '\0';
         fgets (buf, 512, message.stream);
 
         if ((command = irc_parsemessage (buf, &message))) {
             printf ("%10s %s %s\n", command, message.channel, message.line);
-            if (!strcmp (command, "ERROR"))
+            if (!strcmp (command, "ERROR") || !strcmp (command, "ENOMEM"))
                 break;
         } else {
             if ((msg = parse (&message)) != NULL) {
--- a/src/parse.c
+++ b/src/parse.c
@@ -49,8 +49,7 @@
     "!ping", "!on", "!off",
     "!debug", "!vaccuum", "!logout",
     "!who", "!whois", "!time", "!tell",
-    "!op",
-    NULL,
+    "!op", NULL
 };
 
 char *parse (MSG * message)