git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* [PATCH] commit-graph: close file before returning NULL
@ 2022-04-18 17:13 Derrick Stolee via GitGitGadget
  2022-04-20 20:55 ` Junio C Hamano
  0 siblings, 1 reply; 2+ messages in thread
From: Derrick Stolee via GitGitGadget @ 2022-04-18 17:13 UTC (permalink / raw)
  To: git; +Cc: gitster, me, Derrick Stolee, Kleber Tarcísio

From: =?UTF-8?q?Kleber=20Tarc=C3=ADsio?= <klebertarcisio@yahoo.com.br>

There are two reasons that we could return NULL early within
load_commit_graph_chain():

 1. The file does not exist, so the file pointer is NULL.
 2. The file exists, but is too small to contain a single hash.

These were grouped together when the function was first written in
5c84b3396 (commit-graph: load commit-graph chains, 2019-06-18) in order
to simplify how the 'chain_name' string is freed. However, the current
code leaves a narrow window where the file pointer is not closed when
the file exists, but is rejected for being too small.

Split out these cases separately to ensure we close the file in this
case.

Signed-off-by: Kleber Tarcísio <klebertarcisio@yahoo.com.br>
Signed-off-by: Derrick Stolee <derrickstolee@github.com>
---
    commit-graph: close file before returning NULL
    
    This change was originally submitted to the microsoft/git fork [1].
    Kleber discovered this issue using some automated tool they are working
    on. We recommended that this change be submitted to the core Git group,
    but we have not had any word from the original author in some time.
    Hence, I am submitting it on their behalf.
    
    [1] https://github.com/microsoft/git/pull/259
    
    Thanks, -Stolee

Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-1213%2Fderrickstolee%2Fclose-file-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1213/derrickstolee/close-file-v1
Pull-Request: https://github.com/gitgitgadget/git/pull/1213

 commit-graph.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/commit-graph.c b/commit-graph.c
index 441b36016ba..06107beedcb 100644
--- a/commit-graph.c
+++ b/commit-graph.c
@@ -523,10 +523,13 @@ static struct commit_graph *load_commit_graph_chain(struct repository *r,
 	stat_res = stat(chain_name, &st);
 	free(chain_name);
 
-	if (!fp ||
-	    stat_res ||
-	    st.st_size <= the_hash_algo->hexsz)
+	if (!fp)
 		return NULL;
+	if (stat_res ||
+	    st.st_size <= the_hash_algo->hexsz) {
+		fclose(fp);
+		return NULL;
+	}
 
 	count = st.st_size / (the_hash_algo->hexsz + 1);
 	CALLOC_ARRAY(oids, count);

base-commit: ab1f2765f78e75ee51dface57e1071b3b7f42b09
-- 
gitgitgadget

^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] commit-graph: close file before returning NULL
  2022-04-18 17:13 [PATCH] commit-graph: close file before returning NULL Derrick Stolee via GitGitGadget
@ 2022-04-20 20:55 ` Junio C Hamano
  0 siblings, 0 replies; 2+ messages in thread
From: Junio C Hamano @ 2022-04-20 20:55 UTC (permalink / raw)
  To: Derrick Stolee via GitGitGadget
  Cc: git, me, Derrick Stolee, Kleber Tarcísio

"Derrick Stolee via GitGitGadget" <gitgitgadget@gmail.com> writes:

> From: =?UTF-8?q?Kleber=20Tarc=C3=ADsio?= <klebertarcisio@yahoo.com.br>
>
> There are two reasons that we could return NULL early within
> load_commit_graph_chain():
>
>  1. The file does not exist, so the file pointer is NULL.
>  2. The file exists, but is too small to contain a single hash.
>
> These were grouped together when the function was first written in
> 5c84b3396 (commit-graph: load commit-graph chains, 2019-06-18) in order
> to simplify how the 'chain_name' string is freed. However, the current
> code leaves a narrow window where the file pointer is not closed when
> the file exists, but is rejected for being too small.
>
> Split out these cases separately to ensure we close the file in this
> case.
>
> Signed-off-by: Kleber Tarcísio <klebertarcisio@yahoo.com.br>
> Signed-off-by: Derrick Stolee <derrickstolee@github.com>
> ---
>     commit-graph: close file before returning NULL
>     
>     This change was originally submitted to the microsoft/git fork [1].
>     Kleber discovered this issue using some automated tool they are working
>     on. We recommended that this change be submitted to the core Git group,
>     but we have not had any word from the original author in some time.
>     Hence, I am submitting it on their behalf.


Makes me wonder if it were a better world if fclose() behaved more
like free() ;-)

Will queue.  Thanks.

>  commit-graph.c | 9 ++++++---
>  1 file changed, 6 insertions(+), 3 deletions(-)
>
> diff --git a/commit-graph.c b/commit-graph.c
> index 441b36016ba..06107beedcb 100644
> --- a/commit-graph.c
> +++ b/commit-graph.c
> @@ -523,10 +523,13 @@ static struct commit_graph *load_commit_graph_chain(struct repository *r,
>  	stat_res = stat(chain_name, &st);
>  	free(chain_name);
>  
> -	if (!fp ||
> -	    stat_res ||
> -	    st.st_size <= the_hash_algo->hexsz)
> +	if (!fp)
>  		return NULL;
> +	if (stat_res ||
> +	    st.st_size <= the_hash_algo->hexsz) {
> +		fclose(fp);
> +		return NULL;
> +	}
>  
>  	count = st.st_size / (the_hash_algo->hexsz + 1);
>  	CALLOC_ARRAY(oids, count);
>
> base-commit: ab1f2765f78e75ee51dface57e1071b3b7f42b09

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-04-20 20:56 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-04-18 17:13 [PATCH] commit-graph: close file before returning NULL Derrick Stolee via GitGitGadget
2022-04-20 20:55 ` Junio C Hamano

Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

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).