[PATCH 16/38] trace-cmd lib: don't double close a file descriptor in read_header_files()

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



In read_header_files(), if we encounter an error after we close the
first file but before we open the second one, the exit code tries to
close it again.

Move the call to close() just before the second open to prevent that.

Fixes a USE_AFTER_FREE error (CWE-416)

Signed-off-by: Jerome Marchand <jmarchan@xxxxxxxxxx>
---
 lib/trace-cmd/trace-output.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lib/trace-cmd/trace-output.c b/lib/trace-cmd/trace-output.c
index 35904620..c270d03f 100644
--- a/lib/trace-cmd/trace-output.c
+++ b/lib/trace-cmd/trace-output.c
@@ -685,7 +685,6 @@ static int read_header_files(struct tracecmd_output *handle, bool compress)
 	if (do_write_check(handle, &endian8, 8))
 		goto out_free;
 	check_size = copy_file_fd(handle, fd, 0);
-	close(fd);
 	if (size != check_size) {
 		tracecmd_warning("wrong size for '%s' size=%lld read=%lld", path, size, check_size);
 		errno = EINVAL;
@@ -697,6 +696,7 @@ static int read_header_files(struct tracecmd_output *handle, bool compress)
 	if (!path)
 		goto out_close;
 
+	close(fd);
 	fd = open(path, O_RDONLY);
 	if (fd < 0) {
 		tracecmd_warning("can't read '%s'", path);
-- 
2.44.0





[Index of Archives]     [Linux USB Development]     [Linux USB Development]     [Linux Audio Users]     [Yosemite Hiking]     [Linux Kernel]     [Linux SCSI]

  Powered by Linux