diff options
author | Alan Coopersmith <alan.coopersmith@oracle.com> | 2013-04-21 15:18:28 -0700 |
---|---|---|
committer | Alan Coopersmith <alan.coopersmith@oracle.com> | 2013-04-21 15:18:28 -0700 |
commit | 93986e62c7793ef7bb3123748249302e974b72e6 (patch) | |
tree | 5bf201bcee4614afd305fefa80a337fd0863bf37 | |
parent | 23371e39e595056b3db2e361fad8a21ed4a0d9e7 (diff) |
Close file leak when fdopen() fails in OpenFile()
Found by parfait 1.1 bug checking tool:
Error: File Leak
File Descriptor Leak: Leaked File Descriptor fd
at line 457 of app/xditview/Dvi.c in function 'OpenFile'.
fd initialized at line 449 with mkstemp
Also reorganize & clean up the code a bit while we're here
Signed-off-by: Alan Coopersmith <alan.coopersmith@oracle.com>
-rw-r--r-- | Dvi.c | 18 |
1 files changed, 9 insertions, 9 deletions
@@ -434,22 +434,22 @@ CloseFile (DviWidget dw) static void OpenFile (DviWidget dw) { - char tmpName[sizeof ("/tmp/dviXXXXXX")]; -#ifdef HAS_MKSTEMP - int fd; -#endif - dw->dvi.tmpFile = NULL; if (!dw->dvi.seek) { - strcpy (tmpName, "/tmp/dviXXXXXX"); + char tmpName[] = "/tmp/dviXXXXXX"; + #ifndef HAS_MKSTEMP mktemp (tmpName); dw->dvi.tmpFile = fopen (tmpName, "w+"); #else - fd = mkstemp(tmpName); - dw->dvi.tmpFile = fdopen(fd, "w+"); + int fd = mkstemp(tmpName); + if (fd != -1) { + dw->dvi.tmpFile = fdopen(fd, "w+"); + if (dw->dvi.tmpFile == NULL) + close(fd); + } #endif - unlink (tmpName); + remove (tmpName); } if (dw->dvi.requested_page < 1) dw->dvi.requested_page = 1; |