unofficial mirror of libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Arjun Shankar via Libc-alpha <libc-alpha@sourceware.org>
To: Siddhesh Poyarekar <siddhesh@sourceware.org>
Cc: libc-alpha@sourceware.org
Subject: Re: [PATCH 2/5] gconv_parseconfdir: Fix memory leak
Date: Tue, 3 Aug 2021 15:09:34 +0000	[thread overview]
Message-ID: <20210803150934.GB6986@lostca.se> (raw)
In-Reply-To: <20210727174129.3612656-3-siddhesh@sourceware.org>

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!

  reply	other threads:[~2021-08-03 15:09 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-07-27 17:41 [PATCH 0/5] Static analysis fixes Siddhesh Poyarekar via Libc-alpha
2021-07-27 17:41 ` [PATCH 1/5] ldconfig: avoid leak on empty paths in config file Siddhesh Poyarekar via Libc-alpha
2021-08-03 15:08   ` Arjun Shankar via Libc-alpha
2021-07-27 17:41 ` [PATCH 2/5] gconv_parseconfdir: Fix memory leak Siddhesh Poyarekar via Libc-alpha
2021-08-03 15:09   ` Arjun Shankar via Libc-alpha [this message]
2021-07-27 17:41 ` [PATCH 3/5] iconv_charmap: Close output file when done Siddhesh Poyarekar via Libc-alpha
2021-08-03 15:15   ` Arjun Shankar via Libc-alpha
2021-07-27 17:41 ` [PATCH 4/5] copy_and_spawn_sgid: Avoid double calls to close() Siddhesh Poyarekar via Libc-alpha
2021-08-03 15:25   ` Arjun Shankar via Libc-alpha
2021-07-27 17:41 ` [PATCH 5/5] gaiconf_init: Avoid double-free in label and precedence lists Siddhesh Poyarekar via Libc-alpha
2021-08-03 15:34   ` Arjun Shankar via Libc-alpha

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/libc/involved.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210803150934.GB6986@lostca.se \
    --to=libc-alpha@sourceware.org \
    --cc=arjun.is@lostca.se \
    --cc=siddhesh@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).