diff mbox

[U-Boot] tools: kwbimage: Fix unchecked return value and fd leak

Message ID 20170213091155.12302-1-mario.six@gdsys.cc
State Accepted
Commit 1f6c8a573381ed7d4f506ab45eeb23fb2a896660
Delegated to: Stefan Roese
Headers show

Commit Message

Mario Six Feb. 13, 2017, 9:11 a.m. UTC
The return value of fstat was not checked in kwbimage, and in the case
of an error, the already open file was not closed. Fix both errors.

Reported-by: Coverity (CID: 155971)
Reported-by: Coverity (CID: 155969)
Signed-off-by: Mario Six <mario.six@gdsys.cc>
---
 tools/kwbimage.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)

Comments

Stefan Roese Feb. 17, 2017, 11:53 a.m. UTC | #1
On 13.02.2017 10:11, Mario Six wrote:
> The return value of fstat was not checked in kwbimage, and in the case
> of an error, the already open file was not closed. Fix both errors.
>
> Reported-by: Coverity (CID: 155971)
> Reported-by: Coverity (CID: 155969)
> Signed-off-by: Mario Six <mario.six@gdsys.cc>
> ---
>  tools/kwbimage.c | 13 +++++++++++--
>  1 file changed, 11 insertions(+), 2 deletions(-)
>
> diff --git a/tools/kwbimage.c b/tools/kwbimage.c
> index 93797c99da..2c637c7446 100644
> --- a/tools/kwbimage.c
> +++ b/tools/kwbimage.c
> @@ -992,7 +992,11 @@ int add_binary_header_v1(uint8_t *cur)
>  		return -1;
>  	}
>
> -	fstat(fileno(bin), &s);
> +	if (fstat(fileno(bin), &s)) {
> +		fprintf(stderr, "Cannot stat binary file %s\n",
> +			binarye->binary.file);
> +		goto err_close;
> +	}
>
>  	binhdrsz = sizeof(struct opt_hdr_v1) +
>  		(binarye->binary.nargs + 2) * sizeof(uint32_t) +
> @@ -1022,7 +1026,7 @@ int add_binary_header_v1(uint8_t *cur)
>  		fprintf(stderr,
>  			"Could not read binary image %s\n",
>  			binarye->binary.file);
> -		return -1;
> +		goto err_close;
>  	}
>
>  	fclose(bin);
> @@ -1040,6 +1044,11 @@ int add_binary_header_v1(uint8_t *cur)
>  	cur += sizeof(uint32_t);
>
>  	return 0;
> +
> +err_close:
> +	fclose(bin);
> +
> +	return -1;
>  }
>
>  #if defined(CONFIG_KWB_SECURE)
>

Applied to u-boot-marvell/master

Thanks,
Stefan
diff mbox

Patch

diff --git a/tools/kwbimage.c b/tools/kwbimage.c
index 93797c99da..2c637c7446 100644
--- a/tools/kwbimage.c
+++ b/tools/kwbimage.c
@@ -992,7 +992,11 @@  int add_binary_header_v1(uint8_t *cur)
 		return -1;
 	}
 
-	fstat(fileno(bin), &s);
+	if (fstat(fileno(bin), &s)) {
+		fprintf(stderr, "Cannot stat binary file %s\n",
+			binarye->binary.file);
+		goto err_close;
+	}
 
 	binhdrsz = sizeof(struct opt_hdr_v1) +
 		(binarye->binary.nargs + 2) * sizeof(uint32_t) +
@@ -1022,7 +1026,7 @@  int add_binary_header_v1(uint8_t *cur)
 		fprintf(stderr,
 			"Could not read binary image %s\n",
 			binarye->binary.file);
-		return -1;
+		goto err_close;
 	}
 
 	fclose(bin);
@@ -1040,6 +1044,11 @@  int add_binary_header_v1(uint8_t *cur)
 	cur += sizeof(uint32_t);
 
 	return 0;
+
+err_close:
+	fclose(bin);
+
+	return -1;
 }
 
 #if defined(CONFIG_KWB_SECURE)