From: Kleber Tarcísio Date: Mon, 18 Apr 2022 17:13:27 +0000 (+0000) Subject: commit-graph: close file before returning NULL X-Git-Tag: v2.37.0-rc0~95^2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=c0befa0c033b2754b93a666fe0e925c080b7d64b;p=thirdparty%2Fgit.git commit-graph: close file before returning NULL 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 Signed-off-by: Derrick Stolee Signed-off-by: Junio C Hamano --- diff --git a/commit-graph.c b/commit-graph.c index 441b36016b..06107beedc 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);