git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH] pack-revindex.c: don't close unopened file descriptors

From
Junio C Hamano <gitster@pobox.com>
Date
Feb 26, 2021, 22:41 UTC
Message-ID
<xmqqtupyqwnz.fsf@gitster.g>
In-Reply-To
<34c017296a7c5fe4a2ea70f8a0b2d8586b34a4dc.1614357030.git.me@ttaylorr.com>
Taylor Blau <me@ttaylorr.com> writes:
Show 16 quoted lines
> When opening a reverse index, load_revindex_from_disk() jumps to the
> 'cleanup' label in case something goes wrong: the reverse index had the
> wrong size, an unrecognized version, or similar.
>
> It also jumps to this label when the reverse index couldn't be opened in
> the first place, which will cause an error with the unguarded close()
> call in the label.
>
> Guard this call with "if (fd >= 0)" to make sure that we have a valid
> file descriptor to close before attempting to close it.
>
> Reported-by: Johannes Schindelin <Johannes.Schindelin@gmx.de>
> Signed-off-by: Taylor Blau <me@ttaylorr.com>
> ---
> Dscho mentioned this to me privately when reviewing Coverity results for
> -rc0. This one is legitimate, and the fix is easy enough, too.
Thanks, both.
Show 20 quoted lines
>
>  pack-revindex.c | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
>
> diff --git a/pack-revindex.c b/pack-revindex.c
> index 83fe4de773..4262530449 100644
> --- a/pack-revindex.c
> +++ b/pack-revindex.c
> @@ -253,7 +253,8 @@ static int load_revindex_from_disk(char *revindex_name,
>  		*data_p = (const uint32_t *)data;
>  	}
>
> -	close(fd);
> +	if (fd >= 0)
> +		close(fd);
>  	return ret;
>  }
>
> --
> 2.30.0.667.g81c0cbc6fd
Previous: Johannes Schindelin
Message 4 of 4 in “pack-revindex.c: don't close unopened file descriptors”
  1. pack-revindex.c: don't close unopened file descriptorsTaylor Blau, Feb 26, 2021
  2. Jeff KingFeb 26, 2021
  3. Johannes SchindelinMar 1, 2021
  4. Junio C HamanoFeb 26, 2021

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.