From: Jonathan Rose Date: Mon, 5 Mar 2012 18:49:58 +0000 (+0000) Subject: Eliminate double close of file descriptor in manager.c X-Git-Tag: 1.8.10.1~39 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=47244a11d64e790c0ca4b391ced96905944f9e2a;p=thirdparty%2Fasterisk.git Eliminate double close of file descriptor in manager.c The process_output function in manager.c attempted to call fclose and close immediately afterwards. Since fclose implies close, this resulted in a potential double free on file descriptors. This patch changes that behavior and also adds error checking to fclose and close depending on which was deemed necessary. Also error messages. Thanks to Rosen Iliev for pointing out the location of the problem. (closes issue ASTERISK-18453) Reported By: Jaco Kroon Review: https://reviewboard.asterisk.org/r/1793/ git-svn-id: https://origsvn.digium.com/svn/asterisk/branches/1.8@358214 65c4cc65-6c06-0410-ace0-fbb531ad65f3 --- diff --git a/main/manager.c b/main/manager.c index 77ed06f1fa..d830a6e834 100644 --- a/main/manager.c +++ b/main/manager.c @@ -5698,10 +5698,20 @@ static void process_output(struct mansession *s, struct ast_str **out, struct as xml_translate(out, "", params, format); } - fclose(s->f); - s->f = NULL; - close(s->fd); - s->fd = -1; + if (s->f) { + if (fclose(s->f)) { + ast_log(LOG_ERROR, "fclose() failed: %s\n", strerror(errno)); + } + s->f = NULL; + s->fd = -1; + } else if (s->fd != -1) { + if (close(s->fd)) { + ast_log(LOG_ERROR, "close() failed: %s\n", strerror(errno)); + } + s->fd = -1; + } else { + ast_log(LOG_ERROR, "process output attempted to close file/file descriptor on mansession without a valid file or file descriptor.\n"); + } } static int generic_http_callback(struct ast_tcptls_session_instance *ser,