Fix file descriptor leak and pointer scope issue in MiniVM when sending mail

When MiniVM sends an e-mail and it has the volgain option set, it will spawn
sox in a separate process to handle the manipulation of the sound file.  In
doing so, it creates a temporary file.  There are two problems here:
  1) The file descriptor returned from mkstemp is leaked
  2) The finalfilename character pointer points to a buffer that loses scope
     once volgain processing is finished.

Note that in r316265, Russell fixed some gcc warnings by using the return
value of the mkstemp call.  A warning was placed in minivm that the file
descriptor was going to be leaked.  This patch reverts that change, as it
handles the leak and 'uses' the file descriptor returned from mkstemp.

(closes issue ASTERISK-17133)
Reported by: Tzafrir Cohen
patches:
  minivm_18501_demo.diff uploaded by Tzafrir Cohen (license #5035)
........

Merged revisions 372554 from http://svn.asterisk.org/svn/asterisk/branches/1.8
........

Merged revisions 372555 from http://svn.asterisk.org/svn/asterisk/branches/10
........

Merged revisions 372556 from http://svn.asterisk.org/svn/asterisk/branches/11


git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@372557 65c4cc65-6c06-0410-ace0-fbb531ad65f3
This commit is contained in:
Matthew Jordan 2012-09-07 02:16:54 +00:00
parent 5da59112b7
commit 31407fc32c
1 changed files with 9 additions and 13 deletions

View File

@ -1229,6 +1229,7 @@ static int sendmail(struct minivm_template *template, struct minivm_account *vmu
char dur[PATH_MAX];
char tmp[80] = "/tmp/astmail-XXXXXX";
char tmp2[PATH_MAX];
char newtmp[PATH_MAX]; /* Only used with volgain */
struct timeval now;
struct ast_tm tm;
struct minivm_zone *the_zone = NULL;
@ -1268,26 +1269,21 @@ static int sendmail(struct minivm_template *template, struct minivm_account *vmu
/* If we have a gain option, process it now with sox */
if (type == MVM_MESSAGE_EMAIL && (vmu->volgain < -.001 || vmu->volgain > .001) ) {
char newtmp[PATH_MAX];
char tmpcmd[PATH_MAX];
int tmpfd;
/**
* XXX
* /bug tmpfd is a leaked fd. The file is also never unlinked.
* See app_voicemail.c for how the code works there that
* doesn't have this bug.
*/
ast_copy_string(newtmp, "/tmp/XXXXXX", sizeof(newtmp));
ast_debug(3, "newtmp: %s\n", newtmp);
tmpfd = mkstemp(newtmp);
if (tmpfd > -1) {
snprintf(tmpcmd, sizeof(tmpcmd), "sox -v %.4f %s.%s %s.%s", vmu->volgain, filename, format, newtmp, format);
ast_safe_system(tmpcmd);
finalfilename = newtmp;
ast_debug(3, "VOLGAIN: Stored at: %s.%s - Level: %.4f - Mailbox: %s\n", filename, format, vmu->volgain, vmu->username);
if (tmpfd < 0) {
ast_log(LOG_WARNING, "Failed to create temporary file for volgain: %d\n", errno);
return -1;
}
snprintf(tmpcmd, sizeof(tmpcmd), "sox -v %.4f %s.%s %s.%s", vmu->volgain, filename, format, newtmp, format);
ast_safe_system(tmpcmd);
close(tmpfd);
finalfilename = newtmp;
ast_debug(3, "VOLGAIN: Stored at: %s.%s - Level: %.4f - Mailbox: %s\n", filename, format, vmu->volgain, vmu->username);
} else {
finalfilename = ast_strdupa(filename);
}