@@ -3122,7 +3122,8 @@ static int read_and_parse_cmdlines(struct tracecmd_input *handle)
{
struct tep_handle *pevent = handle->pevent;
unsigned long long size;
- char *cmdlines;
+ char *cmdlines = NULL;
+ int ret;
if (CHECK_READ_STATE(handle, TRACECMD_FILE_CMD_LINES))
return 0;
@@ -3131,15 +3132,20 @@ static int read_and_parse_cmdlines(struct tracecmd_input *handle)
add_section(handle, TRACECMD_OPTION_CMDLINES, 0, 0,
lseek64(handle->fd, 0, SEEK_CUR));
- if (read_data_and_size(handle, &cmdlines, &size) < 0)
- return -1;
+ ret = read_data_and_size(handle, &cmdlines, &size);
+ if (ret < 0)
+ goto out;
+ if (!size) {
+ handle->file_state = TRACECMD_FILE_CMD_LINES;
+ goto out;
+ }
cmdlines[size] = 0;
tep_parse_saved_cmdlines(pevent, cmdlines);
- free(cmdlines);
-
handle->file_state = TRACECMD_FILE_CMD_LINES;
-
- return 0;
+ ret = 0;
+out:
+ free(cmdlines);
+ return ret;
}
static void extract_trace_clock(struct tracecmd_input *handle, char *line)
Some error paths in read_and_parse_cmdlines() may lead to a memory leak. Improved the error handling of this internal function to avoid it. Signed-off-by: Tzvetomir Stoyanov (VMware) <tz.stoyanov@gmail.com> --- lib/trace-cmd/trace-input.c | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-)