diff mbox series

python/audit2allow: close file stream on error

Message ID 20220520125107.8423-1-cgzones@googlemail.com (mailing list archive)
State Accepted
Commit c14a86af9a23
Headers show
Series python/audit2allow: close file stream on error | expand

Commit Message

Christian Göttsche May 20, 2022, 12:51 p.m. UTC
sepolgen-ifgen-attr-helper.c: In function ‘load_policy’:
    sepolgen-ifgen-attr-helper.c:196:17: warning: leak of FILE ‘fp’ [CWE-775] [-Wanalyzer-file-leak]
      196 |                 fprintf(stderr, "Out of memory!\n");
          |                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Signed-off-by: Christian Göttsche <cgzones@googlemail.com>
---
 python/audit2allow/sepolgen-ifgen-attr-helper.c | 3 +++
 1 file changed, 3 insertions(+)

Comments

James Carter June 8, 2022, 6:03 p.m. UTC | #1
On Mon, May 23, 2022 at 4:10 AM Christian Göttsche
<cgzones@googlemail.com> wrote:
>
>     sepolgen-ifgen-attr-helper.c: In function ‘load_policy’:
>     sepolgen-ifgen-attr-helper.c:196:17: warning: leak of FILE ‘fp’ [CWE-775] [-Wanalyzer-file-leak]
>       196 |                 fprintf(stderr, "Out of memory!\n");
>           |                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>
> Signed-off-by: Christian Göttsche <cgzones@googlemail.com>

Acked-by: James Carter <jwcart2@gmail.com>

> ---
>  python/audit2allow/sepolgen-ifgen-attr-helper.c | 3 +++
>  1 file changed, 3 insertions(+)
>
> diff --git a/python/audit2allow/sepolgen-ifgen-attr-helper.c b/python/audit2allow/sepolgen-ifgen-attr-helper.c
> index 6f3ba962..5e6cffc1 100644
> --- a/python/audit2allow/sepolgen-ifgen-attr-helper.c
> +++ b/python/audit2allow/sepolgen-ifgen-attr-helper.c
> @@ -194,12 +194,14 @@ static policydb_t *load_policy(const char *filename)
>         policydb = malloc(sizeof(policydb_t));
>         if (policydb == NULL) {
>                 fprintf(stderr, "Out of memory!\n");
> +               fclose(fp);
>                 return NULL;
>         }
>
>         if (policydb_init(policydb)) {
>                 fprintf(stderr, "Out of memory!\n");
>                 free(policydb);
> +               fclose(fp);
>                 return NULL;
>         }
>
> @@ -208,6 +210,7 @@ static policydb_t *load_policy(const char *filename)
>                 fprintf(stderr,
>                         "error(s) encountered while parsing configuration\n");
>                 free(policydb);
> +               fclose(fp);
>                 return NULL;
>         }
>
> --
> 2.36.1
>
James Carter June 15, 2022, 1:46 p.m. UTC | #2
On Wed, Jun 8, 2022 at 2:03 PM James Carter <jwcart2@gmail.com> wrote:
>
> On Mon, May 23, 2022 at 4:10 AM Christian Göttsche
> <cgzones@googlemail.com> wrote:
> >
> >     sepolgen-ifgen-attr-helper.c: In function ‘load_policy’:
> >     sepolgen-ifgen-attr-helper.c:196:17: warning: leak of FILE ‘fp’ [CWE-775] [-Wanalyzer-file-leak]
> >       196 |                 fprintf(stderr, "Out of memory!\n");
> >           |                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> >
> > Signed-off-by: Christian Göttsche <cgzones@googlemail.com>
>
> Acked-by: James Carter <jwcart2@gmail.com>
>
Merged.
Thanks,
Jim

> > ---
> >  python/audit2allow/sepolgen-ifgen-attr-helper.c | 3 +++
> >  1 file changed, 3 insertions(+)
> >
> > diff --git a/python/audit2allow/sepolgen-ifgen-attr-helper.c b/python/audit2allow/sepolgen-ifgen-attr-helper.c
> > index 6f3ba962..5e6cffc1 100644
> > --- a/python/audit2allow/sepolgen-ifgen-attr-helper.c
> > +++ b/python/audit2allow/sepolgen-ifgen-attr-helper.c
> > @@ -194,12 +194,14 @@ static policydb_t *load_policy(const char *filename)
> >         policydb = malloc(sizeof(policydb_t));
> >         if (policydb == NULL) {
> >                 fprintf(stderr, "Out of memory!\n");
> > +               fclose(fp);
> >                 return NULL;
> >         }
> >
> >         if (policydb_init(policydb)) {
> >                 fprintf(stderr, "Out of memory!\n");
> >                 free(policydb);
> > +               fclose(fp);
> >                 return NULL;
> >         }
> >
> > @@ -208,6 +210,7 @@ static policydb_t *load_policy(const char *filename)
> >                 fprintf(stderr,
> >                         "error(s) encountered while parsing configuration\n");
> >                 free(policydb);
> > +               fclose(fp);
> >                 return NULL;
> >         }
> >
> > --
> > 2.36.1
> >
diff mbox series

Patch

diff --git a/python/audit2allow/sepolgen-ifgen-attr-helper.c b/python/audit2allow/sepolgen-ifgen-attr-helper.c
index 6f3ba962..5e6cffc1 100644
--- a/python/audit2allow/sepolgen-ifgen-attr-helper.c
+++ b/python/audit2allow/sepolgen-ifgen-attr-helper.c
@@ -194,12 +194,14 @@  static policydb_t *load_policy(const char *filename)
 	policydb = malloc(sizeof(policydb_t));
 	if (policydb == NULL) {
 		fprintf(stderr, "Out of memory!\n");
+		fclose(fp);
 		return NULL;
 	}
 
 	if (policydb_init(policydb)) {
 		fprintf(stderr, "Out of memory!\n");
 		free(policydb);
+		fclose(fp);
 		return NULL;
 	}
 
@@ -208,6 +210,7 @@  static policydb_t *load_policy(const char *filename)
 		fprintf(stderr,
 			"error(s) encountered while parsing configuration\n");
 		free(policydb);
+		fclose(fp);
 		return NULL;
 	}