From 882c3150e23a86b1d4f92b9a60ab35d5fa5c1670 Mon Sep 17 00:00:00 2001 From: Kevin Easton Date: Tue, 18 Jun 2013 12:55:32 +0000 Subject: [PATCH] Fix open file leak in /LASTLOG, in situations like: /LASTLOG -FILE /tmp/a -FILE /tmp/a /LASTLOG -FILE /tmp/a -CLEAR /LASTLOG -FILE /tmp/a -LITERAL /LASTLOG -FILE /tmp/a -BOGUS Found by Coverity. git-svn-id: svn://svn.code.sf.net/p/bitchx/code/trunk@243 13b04d17-f746-0410-82c6-800466cd88b0 --- Changelog | 2 ++ source/lastlog.c | 25 +++++++++++++++---------- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/Changelog b/Changelog index e00e12b..9ca3933 100644 --- a/Changelog +++ b/Changelog @@ -1,5 +1,7 @@ [Changes 1.2c01] +* Fix open file leak in /LASTLOG found by Coverity. (caf) + * Fix file descriptor leak on SOCKS4 connection failure found by Coverity. (caf) diff --git a/source/lastlog.c b/source/lastlog.c index 5069115..3798f2a 100644 --- a/source/lastlog.c +++ b/source/lastlog.c @@ -282,8 +282,8 @@ BUILT_IN_COMMAND(lastlog) Lastlog *start_pos; char *match = NULL, *arg; - char *file_open[] = { "wt", "at" }; - int file_open_type = 0; + const char *file_open_type = "w"; + const char *filename = NULL; char *blah = NULL; FILE *fp = NULL; @@ -343,7 +343,7 @@ BUILT_IN_COMMAND(lastlog) return; } else if (!my_strnicmp(arg, "APPEND", len)) - file_open_type = 1; + file_open_type = "a"; else if (!my_strnicmp(arg, "FILE", len)) { #ifdef PUBLIC_ACCESS @@ -352,13 +352,11 @@ BUILT_IN_COMMAND(lastlog) #else if (args && *args) { - char *filename; - filename = next_arg(args, &args); - if (!(fp = fopen(filename, file_open[file_open_type]))) - { - bitchsay("cannot open file %s", filename); - return; - } + char *filename_arg = next_arg(args, &args); + if (filename) + say("Additional -FILE argument ignored"); + else + filename = filename_arg; } else { @@ -436,6 +434,13 @@ BUILT_IN_COMMAND(lastlog) } } } + + if (!(fp = fopen(filename, file_open_type))) + { + bitchsay("cannot open file %s", filename); + return; + } + start_pos = current_window->lastlog_head; level = current_window->lastlog_level; msg_level = set_lastlog_msg_level(0);