[PATCH i-g-t v4 2/4] lib/igt_sysfs: stop leaking fd on write failures

Lucas De Marchi lucas.demarchi at intel.com
Wed Feb 28 22:31:32 UTC 2024


Make sure to close the fd before returning.

Signed-off-by: Lucas De Marchi <lucas.demarchi at intel.com>
---
 lib/igt_sysfs.c | 21 ++++++++++++++-------
 1 file changed, 14 insertions(+), 7 deletions(-)

diff --git a/lib/igt_sysfs.c b/lib/igt_sysfs.c
index 2b0225138..2997925e5 100644
--- a/lib/igt_sysfs.c
+++ b/lib/igt_sysfs.c
@@ -515,29 +515,36 @@ int igt_sysfs_vprintf(int dir, const char *attr, const char *fmt, va_list ap)
 	va_copy(tmp, ap);
 	ret = vsnprintf(stack, sizeof(stack), fmt, tmp);
 	va_end(tmp);
-	if (igt_debug_on(ret < 0))
-		return -EINVAL;
+	if (igt_debug_on(ret < 0)) {
+		ret = -EINVAL;
+		goto end;
+	}
 
 	if (ret > sizeof(stack)) {
 		unsigned int len = ret + 1;
 
 		buf = malloc(len);
-		if (igt_debug_on(!buf))
-			return -ENOMEM;
+		if (igt_debug_on(!buf)) {
+			ret = -ENOMEM;
+			goto end;
+		}
 
 		ret = vsnprintf(buf, ret, fmt, ap);
 		if (igt_debug_on(ret > len)) {
-			free(buf);
-			return -EINVAL;
+			ret = -EINVAL;
+			goto free_buf;
 		}
 	}
 
 	ret = igt_writen(fd, buf, ret);
 
-	close(fd);
+free_buf:
 	if (buf != stack)
 		free(buf);
 
+end:
+	close(fd);
+
 	return ret;
 }
 
-- 
2.43.0



More information about the igt-dev mailing list