[OpenWrt-Devel] usign: fix some resource leaks
diff mbox series

Message ID 20190902202836.26945-1-hauke@hauke-m.de
State Under Review
Delegated to: Hauke Mehrtens
Headers show
Series
  • [OpenWrt-Devel] usign: fix some resource leaks
Related show

Commit Message

Hauke Mehrtens Sept. 2, 2019, 8:28 p.m. UTC
This fixes some resources leaks mostly in error patches.

Coverity: #1330236, #1330237, #1330238
Signed-off-by: Hauke Mehrtens <hauke@hauke-m.de>
---
 main.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)

Comments

Rosen Penev Sept. 2, 2019, 9:27 p.m. UTC | #1
On Mon, Sep 2, 2019 at 1:29 PM Hauke Mehrtens <hauke@hauke-m.de> wrote:
>
> This fixes some resources leaks mostly in error patches.
>
> Coverity: #1330236, #1330237, #1330238
> Signed-off-by: Hauke Mehrtens <hauke@hauke-m.de>
> ---
>  main.c | 12 ++++++++++--
>  1 file changed, 10 insertions(+), 2 deletions(-)
>
> diff --git a/main.c b/main.c
> index 3536443..ef47b28 100644
> --- a/main.c
> +++ b/main.c
> @@ -129,6 +129,7 @@ get_file(const char *filename, char *buf, int buflen)
>
>         len = fread(buf, 1, buflen - 1, f);
>         buf[len] = 0;
> +       fclose(f);
It's more compact to use __attribute__((cleanup)) instead. Although
last time I tried making changes like those they were shot sown since
it's a GNU extension.
>  }
>
>  static bool
> @@ -171,6 +172,7 @@ static int verify(const char *msgfile)
>         if (!get_base64_file(sigfile, &sig, sizeof(sig), buf, sizeof(buf)) ||
>             memcmp(sig.pkalg, "Ed", 2) != 0) {
>                 fprintf(stderr, "Failed to decode signature\n");
> +               fclose(f);
>                 return 1;
>         }
>
> @@ -183,6 +185,7 @@ static int verify(const char *msgfile)
>         if (!get_base64_file(pubkeyfile, &pkey, sizeof(pkey), buf, sizeof(buf)) ||
>             memcmp(pkey.pkalg, "Ed", 2) != 0) {
>                 fprintf(stderr, "Failed to decode public key\n");
> +               fclose(f);
>                 return 1;
>         }
>
> @@ -292,11 +295,16 @@ static int generate(void)
>         FILE *f;
>
>         f = fopen("/dev/urandom", "r");
> -       if (!f ||
> -           fread(skey.fingerprint, sizeof(skey.fingerprint), 1, f) != 1 ||
> +       if (!f) {
> +               fprintf(stderr, "Can't open /dev/urandom\n");
> +               return 1;
> +       }
> +
> +       if (fread(skey.fingerprint, sizeof(skey.fingerprint), 1, f) != 1 ||
>             fread(skey.seckey, EDSIGN_SECRET_KEY_SIZE, 1, f) != 1 ||
>             fread(skey.salt, sizeof(skey.salt), 1, f) != 1) {
>                 fprintf(stderr, "Can't read data from /dev/urandom\n");
> +               fclose(f);
>                 return 1;
>         }
>         if (f)
> --
> 2.20.1
>
>
> _______________________________________________
> openwrt-devel mailing list
> openwrt-devel@lists.openwrt.org
> https://lists.openwrt.org/mailman/listinfo/openwrt-devel

Patch
diff mbox series

diff --git a/main.c b/main.c
index 3536443..ef47b28 100644
--- a/main.c
+++ b/main.c
@@ -129,6 +129,7 @@  get_file(const char *filename, char *buf, int buflen)
 
 	len = fread(buf, 1, buflen - 1, f);
 	buf[len] = 0;
+	fclose(f);
 }
 
 static bool
@@ -171,6 +172,7 @@  static int verify(const char *msgfile)
 	if (!get_base64_file(sigfile, &sig, sizeof(sig), buf, sizeof(buf)) ||
 	    memcmp(sig.pkalg, "Ed", 2) != 0) {
 		fprintf(stderr, "Failed to decode signature\n");
+		fclose(f);
 		return 1;
 	}
 
@@ -183,6 +185,7 @@  static int verify(const char *msgfile)
 	if (!get_base64_file(pubkeyfile, &pkey, sizeof(pkey), buf, sizeof(buf)) ||
 	    memcmp(pkey.pkalg, "Ed", 2) != 0) {
 		fprintf(stderr, "Failed to decode public key\n");
+		fclose(f);
 		return 1;
 	}
 
@@ -292,11 +295,16 @@  static int generate(void)
 	FILE *f;
 
 	f = fopen("/dev/urandom", "r");
-	if (!f ||
-	    fread(skey.fingerprint, sizeof(skey.fingerprint), 1, f) != 1 ||
+	if (!f) {
+		fprintf(stderr, "Can't open /dev/urandom\n");
+		return 1;
+	}
+
+	if (fread(skey.fingerprint, sizeof(skey.fingerprint), 1, f) != 1 ||
 	    fread(skey.seckey, EDSIGN_SECRET_KEY_SIZE, 1, f) != 1 ||
 	    fread(skey.salt, sizeof(skey.salt), 1, f) != 1) {
 		fprintf(stderr, "Can't read data from /dev/urandom\n");
+		fclose(f);
 		return 1;
 	}
 	if (f)