diff mbox series

[2/5] gconv_parseconfdir: Fix memory leak

Message ID 20210727174129.3612656-3-siddhesh@sourceware.org
State New
Headers show
Series Static analysis fixes | expand

Commit Message

Siddhesh Poyarekar July 27, 2021, 5:41 p.m. UTC
The allocated `conf` would leak if we have to skip over the file due
to the underlying filesystem not supporting dt_type.
---
 iconv/gconv_parseconfdir.h | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)

Comments

Arjun Shankar Aug. 3, 2021, 3:09 p.m. UTC | #1
Hi Siddhesh,

> The allocated `conf` would leak if we have to skip over the file due
> to the underlying filesystem not supporting dt_type.
> ---
>  iconv/gconv_parseconfdir.h | 9 ++++-----
>  1 file changed, 4 insertions(+), 5 deletions(-)
> 
> diff --git a/iconv/gconv_parseconfdir.h b/iconv/gconv_parseconfdir.h
> index a4153e54c6..2f062689ec 100644
> --- a/iconv/gconv_parseconfdir.h
> +++ b/iconv/gconv_parseconfdir.h
> @@ -153,12 +153,11 @@ gconv_parseconfdir (const char *dir, size_t dir_len)
>  	      struct stat64 st;
>  	      if (asprintf (&conf, "%s/%s", buf, ent->d_name) < 0)
>  		continue;
> -	      if (ent->d_type == DT_UNKNOWN
> -		  && (lstat64 (conf, &st) == -1
> -		      || !S_ISREG (st.st_mode)))
> -		continue;
>  
> -	      found |= read_conf_file (conf, dir, dir_len);
> +	      if (ent->d_type != DT_UNKNOWN
> +		  || (lstat64 (conf, &st) != -1 && S_ISREG (st.st_mode)))
> +		found |= read_conf_file (conf, dir, dir_len);
> +
>  	      free (conf);
>  	    }
>  	}

Reversed the condition to conditionally modify `found' first, then
unconditionally free `conf' afterward to avoid a leak. The change looks
correct to me.

I'm wondering if the new condition is harder to read than simply !ing the
old one. But I guess it's not that important compared to the fix itself.

Reviewed-by: Arjun Shankar <arjun@redhat.com>

Cheers!
diff mbox series

Patch

diff --git a/iconv/gconv_parseconfdir.h b/iconv/gconv_parseconfdir.h
index a4153e54c6..2f062689ec 100644
--- a/iconv/gconv_parseconfdir.h
+++ b/iconv/gconv_parseconfdir.h
@@ -153,12 +153,11 @@  gconv_parseconfdir (const char *dir, size_t dir_len)
 	      struct stat64 st;
 	      if (asprintf (&conf, "%s/%s", buf, ent->d_name) < 0)
 		continue;
-	      if (ent->d_type == DT_UNKNOWN
-		  && (lstat64 (conf, &st) == -1
-		      || !S_ISREG (st.st_mode)))
-		continue;
 
-	      found |= read_conf_file (conf, dir, dir_len);
+	      if (ent->d_type != DT_UNKNOWN
+		  || (lstat64 (conf, &st) != -1 && S_ISREG (st.st_mode)))
+		found |= read_conf_file (conf, dir, dir_len);
+
 	      free (conf);
 	    }
 	}