Patchwork lib: fwts_log: free filenames on error condition

login
register
mail settings
Submitter Colin King
Date May 7, 2013, 6:58 a.m.
Message ID <1367909935-32554-1-git-send-email-colin.king@canonical.com>
Download mbox | patch
Permalink /patch/242041/
State Accepted
Headers show

Comments

Colin King - May 7, 2013, 6:58 a.m.
From: Colin Ian King <colin.king@canonical.com>

Coverity CID#997343: Resource leak

We need to ensure filenames is free'd if it is already allocated
if fwts_log_filename() fails.

Signed-off-by: Colin Ian King <colin.king@canonical.com>
---
 src/lib/src/fwts_log.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
Alex Hung - May 8, 2013, 2 a.m.
On 05/07/2013 02:58 PM, Colin King wrote:
> From: Colin Ian King <colin.king@canonical.com>
>
> Coverity CID#997343: Resource leak
>
> We need to ensure filenames is free'd if it is already allocated
> if fwts_log_filename() fails.
>
> Signed-off-by: Colin Ian King <colin.king@canonical.com>
> ---
>   src/lib/src/fwts_log.c | 5 ++++-
>   1 file changed, 4 insertions(+), 1 deletion(-)
>
> diff --git a/src/lib/src/fwts_log.c b/src/lib/src/fwts_log.c
> index 85748cb..4667a76 100644
> --- a/src/lib/src/fwts_log.c
> +++ b/src/lib/src/fwts_log.c
> @@ -579,8 +579,11 @@ char *fwts_log_get_filenames(const char *filename, const fwts_log_type type)
>   	for (i=0; i<32; i++) {
>   		fwts_log_type mask = 1 << i;
>   		if (type & mask) {
> -			if ((tmp = fwts_log_filename(filename, mask)) == NULL)
> +			if ((tmp = fwts_log_filename(filename, mask)) == NULL) {
> +				if (filenames)
> +					free(filenames);
>   				return NULL;
> +			}
>
>   			if (filenames) {
>   				len += strlen(tmp) + 2;
>
Acked-by: Alex Hung <alex.hung@canonical.com>
Keng-Yu Lin - May 20, 2013, 7:24 a.m.
On Tue, May 7, 2013 at 2:58 PM, Colin King <colin.king@canonical.com> wrote:
> From: Colin Ian King <colin.king@canonical.com>
>
> Coverity CID#997343: Resource leak
>
> We need to ensure filenames is free'd if it is already allocated
> if fwts_log_filename() fails.
>
> Signed-off-by: Colin Ian King <colin.king@canonical.com>
> ---
>  src/lib/src/fwts_log.c | 5 ++++-
>  1 file changed, 4 insertions(+), 1 deletion(-)
>
> diff --git a/src/lib/src/fwts_log.c b/src/lib/src/fwts_log.c
> index 85748cb..4667a76 100644
> --- a/src/lib/src/fwts_log.c
> +++ b/src/lib/src/fwts_log.c
> @@ -579,8 +579,11 @@ char *fwts_log_get_filenames(const char *filename, const fwts_log_type type)
>         for (i=0; i<32; i++) {
>                 fwts_log_type mask = 1 << i;
>                 if (type & mask) {
> -                       if ((tmp = fwts_log_filename(filename, mask)) == NULL)
> +                       if ((tmp = fwts_log_filename(filename, mask)) == NULL) {
> +                               if (filenames)
> +                                       free(filenames);
>                                 return NULL;
> +                       }
>
>                         if (filenames) {
>                                 len += strlen(tmp) + 2;
> --
> 1.8.1.2
>

Acked-by: Keng-Yu Lin <kengyu@canonical.com>

Patch

diff --git a/src/lib/src/fwts_log.c b/src/lib/src/fwts_log.c
index 85748cb..4667a76 100644
--- a/src/lib/src/fwts_log.c
+++ b/src/lib/src/fwts_log.c
@@ -579,8 +579,11 @@  char *fwts_log_get_filenames(const char *filename, const fwts_log_type type)
 	for (i=0; i<32; i++) {
 		fwts_log_type mask = 1 << i;
 		if (type & mask) {
-			if ((tmp = fwts_log_filename(filename, mask)) == NULL)
+			if ((tmp = fwts_log_filename(filename, mask)) == NULL) {
+				if (filenames)
+					free(filenames);
 				return NULL;
+			}
 
 			if (filenames) {
 				len += strlen(tmp) + 2;