Git development
 help / color / mirror / Atom feed
From: Andreas Schwab <schwab@linux-m68k.org>
To: Lars Hjemli <hjemli@gmail.com>
Cc: Rys Sommefeldt <rys@pixeltards.com>,
	git@vger.kernel.org, steven@uplinklabs.net
Subject: Re: [cgit PATCH] Close file descriptor on error in readfile()
Date: Sat, 07 Nov 2009 17:14:43 +0100	[thread overview]
Message-ID: <m2ocneb9cc.fsf@igel.home> (raw)
In-Reply-To: <8c5c35580911070659h35c44421q713ddba97318e2b8@mail.gmail.com> (Lars Hjemli's message of "Sat, 7 Nov 2009 15:59:00 +0100")

Lars Hjemli <hjemli@gmail.com> writes:

> diff --git a/shared.c b/shared.c
> index d7b2d5a..a27ab30 100644
> --- a/shared.c
> +++ b/shared.c
> @@ -406,12 +406,17 @@ int readfile(const char *path, char **buf, size_t *size)
>         fd = open(path, O_RDONLY);
>         if (fd == -1)
>                 return errno;
> -       if (fstat(fd, &st))
> +       if (fstat(fd, &st)) {
> +               close(fd);
>                 return errno;

The close call can clobber errno.

> -       if (!S_ISREG(st.st_mode))
> +       }
> +       if (!S_ISREG(st.st_mode)) {
> +               close(fd);
>                 return EISDIR;
> +       }
>         *buf = xmalloc(st.st_size + 1);
>         *size = read_in_full(fd, *buf, st.st_size);
>         (*buf)[*size] = '\0';
> +       close(fd);
>         return (*size == st.st_size ? 0 : errno);

Likewise.

Andreas.

-- 
Andreas Schwab, schwab@linux-m68k.org
GPG Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."

  reply	other threads:[~2009-11-07 16:14 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-11-07  2:01 [cgit PATCH] Close file descriptor on error in readfile() Rys Sommefeldt
2009-11-07  2:22 ` Steven Noonan
2009-11-07  2:26   ` Rys Sommefeldt
2009-11-07 12:23 ` Rys Sommefeldt
2009-11-07 14:59   ` Lars Hjemli
2009-11-07 16:14     ` Andreas Schwab [this message]
2009-11-07 17:15       ` Lars Hjemli

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

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

  git send-email \
    --in-reply-to=m2ocneb9cc.fsf@igel.home \
    --to=schwab@linux-m68k.org \
    --cc=git@vger.kernel.org \
    --cc=hjemli@gmail.com \
    --cc=rys@pixeltards.com \
    --cc=steven@uplinklabs.net \
    /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