summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlan Coopersmith <alan.coopersmith@oracle.com>2013-04-21 10:36:41 -0700
committerAlan Coopersmith <alan.coopersmith@oracle.com>2013-04-25 21:39:44 -0700
commit58b857bcc005552b1ae90733c52d456dc56c3f9b (patch)
treefa7eef0ad8deccec3ace24263583796ca0674dc6
parentd25a3b87ce9fdf950b42f45b644242d72e7167b3 (diff)
Fix file leak in ConstructCommand()
File was never closed in any code path. Found by parfait 1.1 bug checking tool: Error: File Leak File Leak: Leaked File file at line 729 of misc.c in function 'ConstructCommand'. file initialized at line 703 with fopen file leaks when fopen(fname, "r") != NULL at line 703. at line 771 of misc.c in function 'ConstructCommand'. file initialized at line 703 with fopen file leaks when fopen(fname, "r") != NULL at line 703. at line 773 of misc.c in function 'ConstructCommand'. file initialized at line 703 with fopen file leaks when fopen(fname, "r") != NULL at line 703. at line 763 of misc.c in function 'ConstructCommand'. file initialized at line 703 with fopen file leaks when fopen(fname, "r") != NULL at line 703 and left <= 1 at line 762. Signed-off-by: Alan Coopersmith <alan.coopersmith@oracle.com>
-rw-r--r--misc.c29
1 files changed, 16 insertions, 13 deletions
diff --git a/misc.c b/misc.c
index 8ed6ee9..ecc1166 100644
--- a/misc.c
+++ b/misc.c
@@ -685,10 +685,7 @@ ConstructCommand(char *cmdbuf, const char *path,
int left = BUFSIZ; /* space left in buffer */
int used;
const char *fmt;
- FILE *file;
char fmtbuf[128];
- int gotfmt = 0; /* set to 1 if we got a directive from source */
- char fname[PATH_MAX];
fmt = NULL;
/* If you have a command line option that gives a setting for fmt,
@@ -699,23 +696,29 @@ ConstructCommand(char *cmdbuf, const char *path,
* Annoyingly, filename might be relative or absolute. We cheat and
* use system to get the thing to a known absolute filename.
*/
+ FILE *file;
+ int gotfmt = 0; /* set to 1 if we got a directive from source */
+ char fname[PATH_MAX];
+
if (filename[0] == '/') {
snprintf(fname, sizeof(fname), "%s", filename);
}
else {
snprintf(fname, sizeof(fname), "%s/%s", path, filename);
}
- if ((file = fopen(fname, "r")) &&
- (fgets(fmtbuf, sizeof(fmtbuf), file)) &&
- (!memcmp(fmtbuf, "'\\\" ", 4))) {
- /* that's squote-backslash-dquote-space */
- int len = strlen(fmtbuf);
-
- if (len && (fmtbuf[len - 1] == '\n')) {
- fmtbuf[len - 1] = 0;
- fmt = fmtbuf + 3;
- gotfmt++;
+ if ((file = fopen(fname, "r")) != NULL) {
+ if ((fgets(fmtbuf, sizeof(fmtbuf), file)) &&
+ (!memcmp(fmtbuf, "'\\\" ", 4))) {
+ /* that's squote-backslash-dquote-space */
+ int len = strlen(fmtbuf);
+
+ if (len && (fmtbuf[len - 1] == '\n')) {
+ fmtbuf[len - 1] = 0;
+ fmt = fmtbuf + 3;
+ gotfmt++;
+ }
}
+ fclose(file);
}
if (!gotfmt) { /* not there or some error */
fmt = getenv("MANROFFSEQ");