Skip to contentSkip to conversation

lib/safe_file_ops.c: Fix resource leak

PATCH Superseded Bogdan Lezhepekov 1 participant 1 message Feb 11, 2022 Library code

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 lib/safe_file_ops.c: Fix resource leak

safe_file_scanf and safe_file_vprintf suffered
from resource leak, as opened file descriptor
was not closed in case of error.

---
 lib/safe_file_ops.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)
lib/safe_file_ops.c +9 -5
index f803691d8..d7231df4d 100644
@@ -130,7 +130,7 @@ void safe_file_scanf(const char *file, const int lineno,
 	if (f == NULL) {
 		tst_brkm_(file, lineno, TBROK | TERRNO, cleanup_fn,
 			"Failed to open FILE '%s' for reading", path);
-		return;
+		goto out;
 	}
 
 	exp_convs = tst_count_scanf_conversions(fmt);
@@ -142,14 +142,14 @@ void safe_file_scanf(const char *file, const int lineno,
 	if (ret == EOF) {
 		tst_brkm_(file, lineno, TBROK, cleanup_fn,
 			"The FILE '%s' ended prematurely", path);
-		return;
+		goto out;
 	}
 
 	if (ret != exp_convs) {
 		tst_brkm_(file, lineno, TBROK, cleanup_fn,
 			"Expected %i conversions got %i FILE '%s'",
 			exp_convs, ret, path);
-		return;
+		goto out;
 	}
 
 	if (fclose(f)) {
@@ -157,6 +157,8 @@ void safe_file_scanf(const char *file, const int lineno,
 			"Failed to close FILE '%s'", path);
 		return;
 	}
+out:
+	fclose(f);
 }
 
 
@@ -261,13 +263,13 @@ static void safe_file_vprintf(const char *file, const int lineno,
 	if (f == NULL) {
 		tst_brkm_(file, lineno, TBROK | TERRNO, cleanup_fn,
 			"Failed to open FILE '%s' for writing", path);
-		return;
+		goto out;
 	}
 
 	if (vfprintf(f, fmt, va) < 0) {
 		tst_brkm_(file, lineno, TBROK, cleanup_fn,
 			"Failed to print to FILE '%s'", path);
-		return;
+		goto out;
 	}
 
 	if (fclose(f)) {
@@ -275,6 +277,8 @@ static void safe_file_vprintf(const char *file, const int lineno,
 			"Failed to close FILE '%s'", path);
 		return;
 	}
+out:
+	fclose(f);
 }
 
 void safe_file_printf(const char *file, const int lineno,
Signature · 5 lines
2.35.1


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp