{"thread":{"id":"57756","subject":"[PATCH] commit-graph: close file before returning NULL","startedAt":"2022-04-18T17:13:35Z","lastAt":"2022-04-20T20:56:08Z","messageCount":2,"participants":["Derrick Stolee via GitGitGadget","Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"453854","messageId":"pull.1213.git.1650302007395.gitgitgadget@gmail.com","threadId":"57756","inReplyTo":null,"subject":"[PATCH] commit-graph: close file before returning NULL","fromName":"Derrick Stolee via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2022-04-18T17:13:27Z","receivedAt":"2022-04-18T17:13:35Z","isPatch":true,"sender":{"key":"stolee@gmail.com","avatar":"https://avatars.githubusercontent.com/u/570044?v=4"},"body":"From: =?UTF-8?q?Kleber=20Tarc=C3=ADsio?= <klebertarcisio@yahoo.com.br>\n\nThere are two reasons that we could return NULL early within\nload_commit_graph_chain():\n\n 1. The file does not exist, so the file pointer is NULL.\n 2. The file exists, but is too small to contain a single hash.\n\nThese were grouped together when the function was first written in\n5c84b3396 (commit-graph: load commit-graph chains, 2019-06-18) in order\nto simplify how the 'chain_name' string is freed. However, the current\ncode leaves a narrow window where the file pointer is not closed when\nthe file exists, but is rejected for being too small.\n\nSplit out these cases separately to ensure we close the file in this\ncase.\n\nSigned-off-by: Kleber Tarcísio <klebertarcisio@yahoo.com.br>\nSigned-off-by: Derrick Stolee <derrickstolee@github.com>\n---\n    commit-graph: close file before returning NULL\n    \n    This change was originally submitted to the microsoft/git fork [1].\n    Kleber discovered this issue using some automated tool they are working\n    on. We recommended that this change be submitted to the core Git group,\n    but we have not had any word from the original author in some time.\n    Hence, I am submitting it on their behalf.\n    \n    [1] https://github.com/microsoft/git/pull/259\n    \n    Thanks, -Stolee\n\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-1213%2Fderrickstolee%2Fclose-file-v1\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1213/derrickstolee/close-file-v1\nPull-Request: https://github.com/gitgitgadget/git/pull/1213\n\n commit-graph.c | 9 ++++++---\n 1 file changed, 6 insertions(+), 3 deletions(-)\n\ndiff --git a/commit-graph.c b/commit-graph.c\nindex 441b36016ba..06107beedcb 100644\n--- a/commit-graph.c\n+++ b/commit-graph.c\n@@ -523,10 +523,13 @@ static struct commit_graph *load_commit_graph_chain(struct repository *r,\n \tstat_res = stat(chain_name, &st);\n \tfree(chain_name);\n \n-\tif (!fp ||\n-\t    stat_res ||\n-\t    st.st_size <= the_hash_algo->hexsz)\n+\tif (!fp)\n \t\treturn NULL;\n+\tif (stat_res ||\n+\t    st.st_size <= the_hash_algo->hexsz) {\n+\t\tfclose(fp);\n+\t\treturn NULL;\n+\t}\n \n \tcount = st.st_size / (the_hash_algo->hexsz + 1);\n \tCALLOC_ARRAY(oids, count);\n\nbase-commit: ab1f2765f78e75ee51dface57e1071b3b7f42b09\n-- \ngitgitgadget\n"},{"id":"454033","messageId":"xmqq8rrzo4og.fsf@gitster.g","threadId":"57756","inReplyTo":"pull.1213.git.1650302007395.gitgitgadget@gmail.com","subject":"Re: [PATCH] commit-graph: close file before returning NULL","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2022-04-20T20:55:43Z","receivedAt":"2022-04-20T20:56:08Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"\"Derrick Stolee via GitGitGadget\" <gitgitgadget@gmail.com> writes:\n\n> From: =?UTF-8?q?Kleber=20Tarc=C3=ADsio?= <klebertarcisio@yahoo.com.br>\n>\n> There are two reasons that we could return NULL early within\n> load_commit_graph_chain():\n>\n>  1. The file does not exist, so the file pointer is NULL.\n>  2. The file exists, but is too small to contain a single hash.\n>\n> These were grouped together when the function was first written in\n> 5c84b3396 (commit-graph: load commit-graph chains, 2019-06-18) in order\n> to simplify how the 'chain_name' string is freed. However, the current\n> code leaves a narrow window where the file pointer is not closed when\n> the file exists, but is rejected for being too small.\n>\n> Split out these cases separately to ensure we close the file in this\n> case.\n>\n> Signed-off-by: Kleber Tarcísio <klebertarcisio@yahoo.com.br>\n> Signed-off-by: Derrick Stolee <derrickstolee@github.com>\n> ---\n>     commit-graph: close file before returning NULL\n>     \n>     This change was originally submitted to the microsoft/git fork [1].\n>     Kleber discovered this issue using some automated tool they are working\n>     on. We recommended that this change be submitted to the core Git group,\n>     but we have not had any word from the original author in some time.\n>     Hence, I am submitting it on their behalf.\n\n\nMakes me wonder if it were a better world if fclose() behaved more\nlike free() ;-)\n\nWill queue.  Thanks.\n\n>  commit-graph.c | 9 ++++++---\n>  1 file changed, 6 insertions(+), 3 deletions(-)\n>\n> diff --git a/commit-graph.c b/commit-graph.c\n> index 441b36016ba..06107beedcb 100644\n> --- a/commit-graph.c\n> +++ b/commit-graph.c\n> @@ -523,10 +523,13 @@ static struct commit_graph *load_commit_graph_chain(struct repository *r,\n>  \tstat_res = stat(chain_name, &st);\n>  \tfree(chain_name);\n>  \n> -\tif (!fp ||\n> -\t    stat_res ||\n> -\t    st.st_size <= the_hash_algo->hexsz)\n> +\tif (!fp)\n>  \t\treturn NULL;\n> +\tif (stat_res ||\n> +\t    st.st_size <= the_hash_algo->hexsz) {\n> +\t\tfclose(fp);\n> +\t\treturn NULL;\n> +\t}\n>  \n>  \tcount = st.st_size / (the_hash_algo->hexsz + 1);\n>  \tCALLOC_ARRAY(oids, count);\n>\n> base-commit: ab1f2765f78e75ee51dface57e1071b3b7f42b09\n"}]}