# HG changeset patch # User Markus Bröker # Date 1229179214 -3600 # Node ID a769385a59c61f5c06b328bfaafa3e58ca744e35 # Parent 311ea5fa60dd2980a4188753d672dfba7d688b9b 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 diff --git a/include/config.h b/include/config.h --- a/include/config.h +++ b/include/config.h @@ -19,5 +19,4 @@ typedef struct UserConfig UC; int config (UC *, char *); - #endif diff --git a/src/config.c b/src/config.c --- 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; diff --git a/src/irc.c b/src/irc.c --- 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 */ diff --git a/src/main.c b/src/main.c --- 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) { diff --git a/src/parse.c b/src/parse.c --- 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)