{"thread":{"id":"64643","subject":"[PATCH] open: check fd_flags value before calling fcntl","startedAt":"2025-12-18T03:14:05Z","lastAt":"2025-12-18T03:45:32Z","messageCount":2,"participants":["AZero13 via GitGitGadget","Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"532406","messageId":"pull.2131.git.git.1766027643744.gitgitgadget@gmail.com","threadId":"64643","inReplyTo":null,"subject":"[PATCH] open: check fd_flags value before calling fcntl","fromName":"AZero13 via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2025-12-18T03:14:03Z","receivedAt":"2025-12-18T03:14:05Z","isPatch":true,"sender":{"key":"name:AZero13","avatar":null},"body":"From: AZero13 <gfunni234@gmail.com>\n\nOtherwise, fcntl can be called with invalid fd_flags.\n\nSigned-off-by: Greg Funni <gfunni234@gmail.com>\n---\n    open: check fd_flags value before calling fcntl\n    \n    Otherwise, fcntl can be called with invalid fd_flags.\n    \n    Signed-off-by:Greg Funni gfunni234@gmail.com\n\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2131%2FAZero13%2Ffixopen-v1\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2131/AZero13/fixopen-v1\nPull-Request: https://github.com/git/git/pull/2131\n\n compat/open.c | 4 ++--\n 1 file changed, 2 insertions(+), 2 deletions(-)\n\ndiff --git a/compat/open.c b/compat/open.c\nindex 37ae2b1aeb..b313bcd364 100644\n--- a/compat/open.c\n+++ b/compat/open.c\n@@ -44,8 +44,8 @@ int git_open_cloexec(const char *name, int flags)\n \n \t\tif (!o_cloexec && 0 <= fd && fd_cloexec) {\n \t\t\t/* Opened w/o O_CLOEXEC?  try with fcntl(2) to add it */\n-\t\t\tint flags = fcntl(fd, F_GETFD);\n-\t\t\tif (fcntl(fd, F_SETFD, flags | fd_cloexec))\n+\t\t\tint fd_flags = fcntl(fd, F_GETFD);\n+\t\t\tif (fd_flags < 0 || fcntl(fd, F_SETFD, fd_flags | fd_cloexec))\n \t\t\t\tfd_cloexec = 0;\n \t\t}\n \t}\n\nbase-commit: c4a0c8845e2426375ad257b6c221a3a7d92ecfda\n-- \ngitgitgadget\n"},{"id":"532410","messageId":"xmqqldj0xxk6.fsf@gitster.g","threadId":"64643","inReplyTo":"pull.2131.git.git.1766027643744.gitgitgadget@gmail.com","subject":"Re: [PATCH] open: check fd_flags value before calling fcntl","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2025-12-18T03:45:29Z","receivedAt":"2025-12-18T03:45:32Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"\"AZero13 via GitGitGadget\" <gitgitgadget@gmail.com> writes:\n\n> From: AZero13 <gfunni234@gmail.com>\n>\n> Otherwise, fcntl can be called with invalid fd_flags.\n\nThis somehow feels mischaracterised.  The intent of the code is\nalready to check the current flags value using getfd and or-in the\ncloexec bit to call setfd.  What this patch fixes is to handle\na failed getfd case correctly.\n\n    Subject: git_open_cloexec(): handle failing F_GETFD\n\n    Before using F_SETFD to add in fd_cloexec bit, the code uses\n    F_GETFD to see the current set of flags.  As it does not pay\n    attention to potential failures, F_SETFD may be called with a\n    set of invalid fcntl bits.\n\n    Continue without calling F_SETFD and behave the same way as the\n    case where F_SETFD failed, when the initial F_GETFD failed.\n\nor something like that, perhaps?\n\n> Signed-off-by: Greg Funni <gfunni234@gmail.com>\n\nThe in-body \"From: AZer...\" line we see above should say \"From: Greg...\"\ninstead.  Set \"git config set user.name 'Greg Funni'\" in the repository\nyou use to contribute to this project, amend the commit so that it will\nrecord \"Greg...\" instead of \"AZer...\" as its author, and force push\nto GGG and tell GGG to send out the email, perhaps?\n\n> ---\n>     open: check fd_flags value before calling fcntl\n>     \n>     Otherwise, fcntl can be called with invalid fd_flags.\n>     \n>     Signed-off-by:Greg Funni gfunni234@gmail.com\n>\n> Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2131%2FAZero13%2Ffixopen-v1\n> Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2131/AZero13/fixopen-v1\n> Pull-Request: https://github.com/git/git/pull/2131\n>\n>  compat/open.c | 4 ++--\n>  1 file changed, 2 insertions(+), 2 deletions(-)\n>\n> diff --git a/compat/open.c b/compat/open.c\n> index 37ae2b1aeb..b313bcd364 100644\n> --- a/compat/open.c\n> +++ b/compat/open.c\n> @@ -44,8 +44,8 @@ int git_open_cloexec(const char *name, int flags)\n>  \n>  \t\tif (!o_cloexec && 0 <= fd && fd_cloexec) {\n>  \t\t\t/* Opened w/o O_CLOEXEC?  try with fcntl(2) to add it */\n> -\t\t\tint flags = fcntl(fd, F_GETFD);\n> -\t\t\tif (fcntl(fd, F_SETFD, flags | fd_cloexec))\n> +\t\t\tint fd_flags = fcntl(fd, F_GETFD);\n> +\t\t\tif (fd_flags < 0 || fcntl(fd, F_SETFD, fd_flags | fd_cloexec))\n>  \t\t\t\tfd_cloexec = 0;\n>  \t\t}\n>  \t}\n>\n> base-commit: c4a0c8845e2426375ad257b6c221a3a7d92ecfda\n"}]}