util: add helpers for automatic cleanup on scope exit
Leverage the cleanup attribute to attach automatic cleanup calls to
variables. Now we don't have to worry about FILE* leaks via error
exit paths.
Bug: None
Test: unittests pass
Change-Id: I1a82245c5ef29ef444c1752344ee209202e1456c
diff --git a/libminijail.c b/libminijail.c
index 55ff4af..89a4375 100644
--- a/libminijail.c
+++ b/libminijail.c
@@ -1135,7 +1135,7 @@
if (!seccomp_should_use_filters(j))
return;
- FILE *file = fopen(path, "re");
+ attribute_cleanup_fp FILE *file = fopen(path, "re");
if (!file) {
pdie("failed to open seccomp filter file '%s'", path);
}
@@ -1144,13 +1144,12 @@
die("failed to compile seccomp filter BPF program in '%s'",
path);
}
- fclose(file);
}
void API minijail_parse_seccomp_filters_from_fd(struct minijail *j, int fd)
{
char *fd_path, *path;
- FILE *file;
+ attribute_cleanup_fp FILE *file = NULL;
if (!seccomp_should_use_filters(j))
return;
@@ -1172,7 +1171,6 @@
fd);
}
free(path);
- fclose(file);
}
void API minijail_set_seccomp_filters(struct minijail *j,