Patchwork lib: fwts_cpu: free cpu on error

login
register
mail settings
Submitter Colin King
Date May 6, 2013, 10:02 p.m.
Message ID <1367877749-17828-1-git-send-email-colin.king@canonical.com>
Download mbox | patch
Permalink /patch/241801/
State Accepted
Headers show

Comments

Colin King - May 6, 2013, 10:02 p.m.
From: Colin Ian King <colin.king@canonical.com>

Coverity CID #997346, Resource leak.

Need to free cpu before we return.

Signed-off-by: Colin Ian King <colin.king@canonical.com>
---
 src/lib/src/fwts_cpu.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
Alex Hung - May 7, 2013, 3:30 a.m.
On 05/07/2013 06:02 AM, Colin King wrote:
> From: Colin Ian King <colin.king@canonical.com>
>
> Coverity CID #997346, Resource leak.
>
> Need to free cpu before we return.
>
> Signed-off-by: Colin Ian King <colin.king@canonical.com>
> ---
>   src/lib/src/fwts_cpu.c | 4 +++-
>   1 file changed, 3 insertions(+), 1 deletion(-)
>
> diff --git a/src/lib/src/fwts_cpu.c b/src/lib/src/fwts_cpu.c
> index d6c11ba..282bc42 100644
> --- a/src/lib/src/fwts_cpu.c
> +++ b/src/lib/src/fwts_cpu.c
> @@ -114,8 +114,10 @@ fwts_cpuinfo_x86 *fwts_cpu_get_info(const int which_cpu)
>   	if ((cpu = (fwts_cpuinfo_x86*)calloc(1, sizeof(fwts_cpuinfo_x86))) == NULL)
>   		return NULL;
>
> -	if ((fp = fopen("/proc/cpuinfo", "r")) == NULL)
> +	if ((fp = fopen("/proc/cpuinfo", "r")) == NULL) {
> +		free(cpu);
>   		return NULL;
> +	}
>
>   	while (fgets(buffer, sizeof(buffer), fp) != NULL) {
>   		char *ptr = strstr(buffer, ":");
>
Acked-by: Alex Hung <alex.hung@canonical.com>
Ivan Hu - May 10, 2013, 8:18 a.m.
On 05/07/2013 06:02 AM, Colin King wrote:
> From: Colin Ian King <colin.king@canonical.com>
>
> Coverity CID #997346, Resource leak.
>
> Need to free cpu before we return.
>
> Signed-off-by: Colin Ian King <colin.king@canonical.com>
> ---
>   src/lib/src/fwts_cpu.c | 4 +++-
>   1 file changed, 3 insertions(+), 1 deletion(-)
>
> diff --git a/src/lib/src/fwts_cpu.c b/src/lib/src/fwts_cpu.c
> index d6c11ba..282bc42 100644
> --- a/src/lib/src/fwts_cpu.c
> +++ b/src/lib/src/fwts_cpu.c
> @@ -114,8 +114,10 @@ fwts_cpuinfo_x86 *fwts_cpu_get_info(const int which_cpu)
>   	if ((cpu = (fwts_cpuinfo_x86*)calloc(1, sizeof(fwts_cpuinfo_x86))) == NULL)
>   		return NULL;
>
> -	if ((fp = fopen("/proc/cpuinfo", "r")) == NULL)
> +	if ((fp = fopen("/proc/cpuinfo", "r")) == NULL) {
> +		free(cpu);
>   		return NULL;
> +	}
>
>   	while (fgets(buffer, sizeof(buffer), fp) != NULL) {
>   		char *ptr = strstr(buffer, ":");
>


Acked-by: Ivan Hu <ivan.hu@canonical.com>

Patch

diff --git a/src/lib/src/fwts_cpu.c b/src/lib/src/fwts_cpu.c
index d6c11ba..282bc42 100644
--- a/src/lib/src/fwts_cpu.c
+++ b/src/lib/src/fwts_cpu.c
@@ -114,8 +114,10 @@  fwts_cpuinfo_x86 *fwts_cpu_get_info(const int which_cpu)
 	if ((cpu = (fwts_cpuinfo_x86*)calloc(1, sizeof(fwts_cpuinfo_x86))) == NULL)
 		return NULL;
 
-	if ((fp = fopen("/proc/cpuinfo", "r")) == NULL)
+	if ((fp = fopen("/proc/cpuinfo", "r")) == NULL) {
+		free(cpu);
 		return NULL;
+	}
 
 	while (fgets(buffer, sizeof(buffer), fp) != NULL) {
 		char *ptr = strstr(buffer, ":");