diff mbox series

util: Fix memory leak

Message ID 20200207161820.20186-1-ceggers@arri.de
State Accepted
Headers show
Series util: Fix memory leak | expand

Commit Message

Christian Eggers Feb. 7, 2020, 4:18 p.m. UTC
Strings b1 and b2 were not freed if length check fails.

Signed-off-by: Christian Eggers <ceggers@arri.de>
---
 core/util.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

Comments

Stefano Babic Feb. 8, 2020, 1:32 p.m. UTC | #1
On 07/02/20 17:18, Christian Eggers wrote:
> Strings b1 and b2 were not freed if length check fails.
> 
> Signed-off-by: Christian Eggers <ceggers@arri.de>
> ---
>  core/util.c | 8 ++++++--
>  1 file changed, 6 insertions(+), 2 deletions(-)
> 
> diff --git a/core/util.c b/core/util.c
> index 3c6cde5..3c7db79 100644
> --- a/core/util.c
> +++ b/core/util.c
> @@ -270,16 +270,20 @@ int get_hw_revision(struct hw_type *hw)
>  	if ((strlen(b1) > (SWUPDATE_GENERAL_STRING_SIZE) - 1) ||
>  		(strlen(b2) > (SWUPDATE_GENERAL_STRING_SIZE - 1))) {
>  		ERROR("Board name or revision too long");
> -		return -1;
> +		ret = -1;
> +		goto out;
>  	}
>  

Good catch, thanks !

>  	strncpy(hw->boardname, b1, sizeof(hw->boardname));
>  	strncpy(hw->revision, b2, sizeof(hw->revision));
>  
> +	ret = 0;
> +
> +out:
>  	free(b1);
>  	free(b2);
>  
> -	return 0;
> +	return ret;
>  }
>  
>  /**
> 

Acked-by: Stefano Babic <sbabic@denx.de>

Best regards,
Stefano Babic
diff mbox series

Patch

diff --git a/core/util.c b/core/util.c
index 3c6cde5..3c7db79 100644
--- a/core/util.c
+++ b/core/util.c
@@ -270,16 +270,20 @@  int get_hw_revision(struct hw_type *hw)
 	if ((strlen(b1) > (SWUPDATE_GENERAL_STRING_SIZE) - 1) ||
 		(strlen(b2) > (SWUPDATE_GENERAL_STRING_SIZE - 1))) {
 		ERROR("Board name or revision too long");
-		return -1;
+		ret = -1;
+		goto out;
 	}
 
 	strncpy(hw->boardname, b1, sizeof(hw->boardname));
 	strncpy(hw->revision, b2, sizeof(hw->revision));
 
+	ret = 0;
+
+out:
 	free(b1);
 	free(b2);
 
-	return 0;
+	return ret;
 }
 
 /**