[PATCH] tools/counter: Fix fd leak in counter example

Hongtao Lee posted 1 patch 3 days, 18 hours ago
tools/counter/counter_example.c | 4 ++++
1 file changed, 4 insertions(+)
[PATCH] tools/counter: Fix fd leak in counter example
Posted by Hongtao Lee 3 days, 18 hours ago
After open /dev/counter0, it's necessary to close(fd) before return.

Fixes: 086099893fce ("tools/counter: Create Counter tools")
Signed-off-by: Hongtao Lee <lihongtao@kylinos.cn>
---
 tools/counter/counter_example.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/tools/counter/counter_example.c b/tools/counter/counter_example.c
index be55287b950f..2f0f35e88fe0 100644
--- a/tools/counter/counter_example.c
+++ b/tools/counter/counter_example.c
@@ -57,12 +57,14 @@ int main(void)
 		if (ret == -1) {
 			fprintf(stderr, "Error adding watches[%d]: %s\n", i,
 				strerror(errno));
+			close(fd);
 			return 1;
 		}
 	}
 	ret = ioctl(fd, COUNTER_ENABLE_EVENTS_IOCTL);
 	if (ret == -1) {
 		perror("Error enabling events");
+		close(fd);
 		return 1;
 	}
 
@@ -70,11 +72,13 @@ int main(void)
 		ret = read(fd, event_data, sizeof(event_data));
 		if (ret == -1) {
 			perror("Failed to read event data");
+			close(fd);
 			return 1;
 		}
 
 		if (ret != sizeof(event_data)) {
 			fprintf(stderr, "Failed to read event data\n");
+			close(fd);
 			return -EIO;
 		}
 
-- 
2.25.1