Some error paths in read_ftrace_printk() 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@xxxxxxxxx> --- lib/trace-cmd/trace-input.c | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/lib/trace-cmd/trace-input.c b/lib/trace-cmd/trace-input.c index 14052a11..5eaf030e 100644 --- a/lib/trace-cmd/trace-input.c +++ b/lib/trace-cmd/trace-input.c @@ -888,7 +888,8 @@ out: static int read_ftrace_printk(struct tracecmd_input *handle) { unsigned int size; - char *buf; + char *buf = NULL; + int ret; if (CHECK_READ_STATE(handle, TRACECMD_FILE_PRINTK)) return 0; @@ -897,29 +898,33 @@ static int read_ftrace_printk(struct tracecmd_input *handle) add_section(handle, TRACECMD_OPTION_PRINTK, 0, 0, lseek64(handle->fd, 0, SEEK_CUR)); - - if (read4(handle, &size) < 0) - return -1; - if (!size) - return 0; /* OK? */ + ret = read4(handle, &size); + if (ret < 0) + goto out; + if (!size) { + handle->file_state = TRACECMD_FILE_PRINTK; + goto out; /* OK? */ + } buf = malloc(size + 1); - if (!buf) - return -1; - if (do_read_check(handle, buf, size)) { - free(buf); - return -1; + if (!buf) { + ret = -1; + goto out; } + ret = do_read_check(handle, buf, size); + if (ret < 0) + goto out; buf[size] = 0; tep_parse_printk_formats(handle->pevent, buf); - free(buf); - handle->file_state = TRACECMD_FILE_PRINTK; + ret = 0; - return 0; +out: + free(buf); + return ret; } static int read_and_parse_cmdlines(struct tracecmd_input *handle); -- 2.31.1