Skip to content

Commit 6f593a7

Browse files
committed
tools/tpm2_eventlog.c: Fix resource leak when exiting with error
Signed-off-by: Imran Desai <imran.desai@intel.com>
1 parent a1976d2 commit 6f593a7

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

tools/misc/tpm2_eventlog.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,17 +76,18 @@ static tool_rc tpm2_tool_onrun(ESYS_CONTEXT *ectx, tpm2_option_flags flags) {
7676

7777
/* Read the file in chunks. Usually the file will reside in
7878
securityfs, and those files do not have a public file size */
79-
tool_rc rc = tool_rc_success;
8079
FILE *fileptr = fopen(filename, "rb");
8180
if (!fileptr) {
8281
return tool_rc_general_error;
8382
}
8483

8584
/* Reserve the buffer for the first chunk */
85+
tool_rc rc = tool_rc_success;
8686
UINT8 *eventlog = calloc(1, CHUNK_SIZE);
8787
if (eventlog == NULL){
8888
LOG_ERR("failed to allocate %d bytes: %s", CHUNK_SIZE, strerror(errno));
89-
return tool_rc_general_error;
89+
rc = tool_rc_general_error;
90+
goto out;
9091
}
9192

9293
size_t size = 0;
@@ -101,7 +102,6 @@ static tool_rc tpm2_tool_onrun(ESYS_CONTEXT *ectx, tpm2_option_flags flags) {
101102
}
102103
eventlog = eventlog_tmp;
103104
} while (is_file_read);
104-
fclose(fileptr);
105105

106106
/* Parse eventlog data */
107107
bool ret = yaml_eventlog(eventlog, size, eventlog_version);
@@ -111,6 +111,10 @@ static tool_rc tpm2_tool_onrun(ESYS_CONTEXT *ectx, tpm2_option_flags flags) {
111111
}
112112

113113
out:
114+
if (fileptr) {
115+
fclose(fileptr);
116+
}
117+
114118
if (eventlog) {
115119
free(eventlog);
116120
}

0 commit comments

Comments
 (0)