Patchwork cpu: cpufreq: fix leak of file if fgets returns NULL

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

Comments

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

Coverity CID #997353, Resource leak.

Need to close file before we return.  

Signed-off-by: Colin Ian King <colin.king@canonical.com>
---
 src/cpu/cpufreq/cpufreq.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
Alex Hung - May 7, 2013, 2:23 a.m.
On 05/07/2013 05:30 AM, Colin King wrote:
> From: Colin Ian King <colin.king@canonical.com>
>
> Coverity CID #997353, Resource leak.
>
> Need to close file before we return.
>
> Signed-off-by: Colin Ian King <colin.king@canonical.com>
> ---
>   src/cpu/cpufreq/cpufreq.c | 4 +++-
>   1 file changed, 3 insertions(+), 1 deletion(-)
>
> diff --git a/src/cpu/cpufreq/cpufreq.c b/src/cpu/cpufreq/cpufreq.c
> index fa10275..5133f51 100644
> --- a/src/cpu/cpufreq/cpufreq.c
> +++ b/src/cpu/cpufreq/cpufreq.c
> @@ -250,8 +250,10 @@ static void do_cpu(fwts_framework *fw, int cpu)
>   		}
>   		return;
>   	}
> -	if (fgets(line, 4095, file) == NULL)
> +	if (fgets(line, 4095, file) == NULL) {
> +		fclose(file);
>   		return;
> +	}
>   	fclose(file);
>
>   	if (total_tests == 1)
>
Acked-by: Alex Hung <alex.hung@canonical.com>
Keng-Yu Lin - May 9, 2013, 6:35 a.m.
On Tue, May 7, 2013 at 5:30 AM, Colin King <colin.king@canonical.com> wrote:
> From: Colin Ian King <colin.king@canonical.com>
>
> Coverity CID #997353, Resource leak.
>
> Need to close file before we return.
>
> Signed-off-by: Colin Ian King <colin.king@canonical.com>
> ---
>  src/cpu/cpufreq/cpufreq.c | 4 +++-
>  1 file changed, 3 insertions(+), 1 deletion(-)
>
> diff --git a/src/cpu/cpufreq/cpufreq.c b/src/cpu/cpufreq/cpufreq.c
> index fa10275..5133f51 100644
> --- a/src/cpu/cpufreq/cpufreq.c
> +++ b/src/cpu/cpufreq/cpufreq.c
> @@ -250,8 +250,10 @@ static void do_cpu(fwts_framework *fw, int cpu)
>                 }
>                 return;
>         }
> -       if (fgets(line, 4095, file) == NULL)
> +       if (fgets(line, 4095, file) == NULL) {
> +               fclose(file);
>                 return;
> +       }
>         fclose(file);
>
>         if (total_tests == 1)
> --
> 1.8.1.2
>
Acked-by: Keng-Yu Lin <kengyu@canonical.com>

Patch

diff --git a/src/cpu/cpufreq/cpufreq.c b/src/cpu/cpufreq/cpufreq.c
index fa10275..5133f51 100644
--- a/src/cpu/cpufreq/cpufreq.c
+++ b/src/cpu/cpufreq/cpufreq.c
@@ -250,8 +250,10 @@  static void do_cpu(fwts_framework *fw, int cpu)
 		}
 		return;
 	}
-	if (fgets(line, 4095, file) == NULL)
+	if (fgets(line, 4095, file) == NULL) {
+		fclose(file);
 		return;
+	}
 	fclose(file);
 
 	if (total_tests == 1)