diff mbox

[4/4] devicetree: dt_base: fix resource leak on error exit paths

Message ID 1464691358-18134-5-git-send-email-colin.king@canonical.com
State Accepted
Headers show

Commit Message

Colin Ian King May 31, 2016, 10:42 a.m. UTC
From: Colin Ian King <colin.king@canonical.com>

There are a couple of resource leaks on the error exit paths, free
the variable output and re-work the exit paths to have a common
resource free and return point.

Signed-off-by: Colin Ian King <colin.king@canonical.com>
---
 src/devicetree/dt_base/dt_base.c | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)

Comments

Alex Hung June 1, 2016, 12:36 a.m. UTC | #1
On 2016-05-31 06:42 PM, Colin King wrote:
> From: Colin Ian King <colin.king@canonical.com>
>
> There are a couple of resource leaks on the error exit paths, free
> the variable output and re-work the exit paths to have a common
> resource free and return point.
>
> Signed-off-by: Colin Ian King <colin.king@canonical.com>
> ---
>   src/devicetree/dt_base/dt_base.c | 15 ++++++++++-----
>   1 file changed, 10 insertions(+), 5 deletions(-)
>
> diff --git a/src/devicetree/dt_base/dt_base.c b/src/devicetree/dt_base/dt_base.c
> index c325045..efb05fc 100644
> --- a/src/devicetree/dt_base/dt_base.c
> +++ b/src/devicetree/dt_base/dt_base.c
> @@ -57,10 +57,10 @@ static int dt_base_check_valid(fwts_framework *fw)
>
>   static int dt_base_check_warnings(fwts_framework *fw)
>   {
> -	int rc, status, in_fd, out_fd;
> +	int rc, status, in_fd, out_fd, ret = FWTS_ERROR;
>   	ssize_t in_len, out_len;
>   	const char *command;
> -	char *output;
> +	char *output = NULL;
>   	pid_t pid;
>
>   	if (!fw->fdt)
> @@ -88,18 +88,23 @@ static int dt_base_check_warnings(fwts_framework *fw)
>   		fwts_failed(fw, LOG_LEVEL_HIGH, "DeviceTreeBaseDTCFailed",
>   				"dtc reports fatal device tree errors:\n%s\n",
>   				output);
> -		return FWTS_ERROR;
> +		goto err;
>   	}
>
>   	if (out_len > 0) {
>   		fwts_failed(fw, LOG_LEVEL_MEDIUM, "DeviceTreeBaseDTCWarnings",
>   				"dtc reports warnings from device tree:\n%s\n",
>   				output);
> -		return FWTS_ERROR;
> +		goto err;
>   	}
>
>   	fwts_passed(fw, "No warnings from dtc");
> -	return FWTS_OK;
> +
> +	ret = FWTS_OK;
> +
> +err:
> +	free(output);
> +	return ret;
>   }
>
>   static fwts_framework_minor_test dt_base_tests[] = {
>

Acked-by: Alex Hung <alex.hung@canonical.com>
Ivan Hu June 3, 2016, 2:35 a.m. UTC | #2
On 05/31/2016 06:42 PM, Colin King wrote:
> From: Colin Ian King <colin.king@canonical.com>
>
> There are a couple of resource leaks on the error exit paths, free
> the variable output and re-work the exit paths to have a common
> resource free and return point.
>
> Signed-off-by: Colin Ian King <colin.king@canonical.com>
> ---
>   src/devicetree/dt_base/dt_base.c | 15 ++++++++++-----
>   1 file changed, 10 insertions(+), 5 deletions(-)
>
> diff --git a/src/devicetree/dt_base/dt_base.c b/src/devicetree/dt_base/dt_base.c
> index c325045..efb05fc 100644
> --- a/src/devicetree/dt_base/dt_base.c
> +++ b/src/devicetree/dt_base/dt_base.c
> @@ -57,10 +57,10 @@ static int dt_base_check_valid(fwts_framework *fw)
>   
>   static int dt_base_check_warnings(fwts_framework *fw)
>   {
> -	int rc, status, in_fd, out_fd;
> +	int rc, status, in_fd, out_fd, ret = FWTS_ERROR;
>   	ssize_t in_len, out_len;
>   	const char *command;
> -	char *output;
> +	char *output = NULL;
>   	pid_t pid;
>   
>   	if (!fw->fdt)
> @@ -88,18 +88,23 @@ static int dt_base_check_warnings(fwts_framework *fw)
>   		fwts_failed(fw, LOG_LEVEL_HIGH, "DeviceTreeBaseDTCFailed",
>   				"dtc reports fatal device tree errors:\n%s\n",
>   				output);
> -		return FWTS_ERROR;
> +		goto err;
>   	}
>   
>   	if (out_len > 0) {
>   		fwts_failed(fw, LOG_LEVEL_MEDIUM, "DeviceTreeBaseDTCWarnings",
>   				"dtc reports warnings from device tree:\n%s\n",
>   				output);
> -		return FWTS_ERROR;
> +		goto err;
>   	}
>   
>   	fwts_passed(fw, "No warnings from dtc");
> -	return FWTS_OK;
> +
> +	ret = FWTS_OK;
> +
> +err:
> +	free(output);
> +	return ret;
>   }
>   
>   static fwts_framework_minor_test dt_base_tests[] = {
Acked-by: Ivan Hu <ivan.hu@canonical.com>
diff mbox

Patch

diff --git a/src/devicetree/dt_base/dt_base.c b/src/devicetree/dt_base/dt_base.c
index c325045..efb05fc 100644
--- a/src/devicetree/dt_base/dt_base.c
+++ b/src/devicetree/dt_base/dt_base.c
@@ -57,10 +57,10 @@  static int dt_base_check_valid(fwts_framework *fw)
 
 static int dt_base_check_warnings(fwts_framework *fw)
 {
-	int rc, status, in_fd, out_fd;
+	int rc, status, in_fd, out_fd, ret = FWTS_ERROR;
 	ssize_t in_len, out_len;
 	const char *command;
-	char *output;
+	char *output = NULL;
 	pid_t pid;
 
 	if (!fw->fdt)
@@ -88,18 +88,23 @@  static int dt_base_check_warnings(fwts_framework *fw)
 		fwts_failed(fw, LOG_LEVEL_HIGH, "DeviceTreeBaseDTCFailed",
 				"dtc reports fatal device tree errors:\n%s\n",
 				output);
-		return FWTS_ERROR;
+		goto err;
 	}
 
 	if (out_len > 0) {
 		fwts_failed(fw, LOG_LEVEL_MEDIUM, "DeviceTreeBaseDTCWarnings",
 				"dtc reports warnings from device tree:\n%s\n",
 				output);
-		return FWTS_ERROR;
+		goto err;
 	}
 
 	fwts_passed(fw, "No warnings from dtc");
-	return FWTS_OK;
+
+	ret = FWTS_OK;
+
+err:
+	free(output);
+	return ret;
 }
 
 static fwts_framework_minor_test dt_base_tests[] = {