{"thread":{"id":"40088","subject":"[PATCH] Close config file handle if the entry to unset is not found","startedAt":"2015-08-14T19:18:59Z","lastAt":"2015-08-14T21:00:54Z","messageCount":10,"participants":["Sven Strickroth","Eric Sunshine","Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"268059","messageId":"55CE3F23.7040702@cs-ware.de","threadId":"40088","inReplyTo":null,"subject":"[PATCH] Close config file handle if the entry to unset is not found","fromName":"Sven Strickroth","fromEmail":"sven@cs-ware.de","sentAt":"2015-08-14T19:18:59Z","receivedAt":"2015-08-14T19:18:59Z","isPatch":true,"sender":{"key":"sven@cs-ware.de","avatar":null},"body":"Without this patch there might be open handle leaks.\n\nSigned-off-by: Sup Yut Sum <ch3cooli@gmail.com>\nSigned-off-by: Sven Strickroth <email@cs-ware.de>\n---\n config.c | 1 +\n 1 file changed, 1 insertion(+)\n\ndiff --git a/config.c b/config.c\nindex 9fd275f..89b49e3 100644\n--- a/config.c\n+++ b/config.c\n@@ -2048,6 +2048,7 @@ int git_config_set_multivar_in_file(const char *config_filename,\n \t\tif ((store.seen == 0 && value == NULL) ||\n \t\t\t\t(store.seen > 1 && multi_replace == 0)) {\n \t\t\tret = CONFIG_NOTHING_SET;\n+\t\t\tclose(in_fd);\n \t\t\tgoto out_free;\n \t\t}\n \n-- \nBest regards,\n Sven Strickroth\n PGP key id F5A9D4C4 @ any key-server\n"},{"id":"268060","messageId":"CAPig+cQMng-bPjO3WLYb1z7aghYhhHqewSr+EGFgV6Gs6_JZRQ@mail.gmail.com","threadId":"40088","inReplyTo":"55CE3F23.7040702@cs-ware.de","subject":"Re: [PATCH] Close config file handle if the entry to unset is not found","fromName":"Eric Sunshine","fromEmail":"sunshine@sunshineco.com","sentAt":"2015-08-14T19:35:39Z","receivedAt":"2015-08-14T19:35:39Z","isPatch":true,"sender":{"key":"sunshine@sunshineco.com","avatar":"https://avatars.githubusercontent.com/u/163641?v=4"},"body":"On Fri, Aug 14, 2015 at 3:18 PM, Sven Strickroth <sven@cs-ware.de> wrote:\n> Without this patch there might be open handle leaks.\n\nThanks for the patch. A question below...\n\n> Signed-off-by: Sup Yut Sum <ch3cooli@gmail.com>\n> Signed-off-by: Sven Strickroth <email@cs-ware.de>\n> ---\n> diff --git a/config.c b/config.c\n> index 9fd275f..89b49e3 100644\n> --- a/config.c\n> +++ b/config.c\n> @@ -2048,6 +2048,7 @@ int git_config_set_multivar_in_file(const char *config_filename,\n>                 if ((store.seen == 0 && value == NULL) ||\n>                                 (store.seen > 1 && multi_replace == 0)) {\n>                         ret = CONFIG_NOTHING_SET;\n> +                       close(in_fd);\n>                         goto out_free;\n\n>From a cursory read of the code, it appears that there are several\nother places where the open 'in_fd' gets leaked which would deserve\nthe same treatment. So, it's not clear why this patch handles only\nthis one case. Am I missing something?\n\n>                 }\n>\n> --\n"},{"id":"268062","messageId":"55CE439A.6070601@cs-ware.de","threadId":"40088","inReplyTo":"CAPig+cQMng-bPjO3WLYb1z7aghYhhHqewSr+EGFgV6Gs6_JZRQ@mail.gmail.com","subject":"Re: [PATCH] Close config file handle if the entry to unset is not found","fromName":"Sven Strickroth","fromEmail":"sven@cs-ware.de","sentAt":"2015-08-14T19:38:02Z","receivedAt":"2015-08-14T19:38:02Z","isPatch":true,"sender":{"key":"sven@cs-ware.de","avatar":null},"body":"Am 14.08.2015 um 21:35 schrieb Eric Sunshine:\n>> Signed-off-by: Sup Yut Sum <ch3cooli@gmail.com>\n>> Signed-off-by: Sven Strickroth <email@cs-ware.de>\n>> ---\n>> diff --git a/config.c b/config.c\n>> index 9fd275f..89b49e3 100644\n>> --- a/config.c\n>> +++ b/config.c\n>> @@ -2048,6 +2048,7 @@ int git_config_set_multivar_in_file(const char *config_filename,\n>>                 if ((store.seen == 0 && value == NULL) ||\n>>                                 (store.seen > 1 && multi_replace == 0)) {\n>>                         ret = CONFIG_NOTHING_SET;\n>> +                       close(in_fd);\n>>                         goto out_free;\n> \n> From a cursory read of the code, it appears that there are several\n> other places where the open 'in_fd' gets leaked which would deserve\n> the same treatment. So, it's not clear why this patch handles only\n> this one case. Am I missing something?\n\nGood point.\n\nI suppose only this place was adjusted, becasue this is the only case\nwhich occurred so far.\n\nShall I re-create another patch?\n\n-- \nBest regards,\n Sven Strickroth\n PGP key id F5A9D4C4 @ any key-server\n"},{"id":"268064","messageId":"55CE4511.6050704@cs-ware.de","threadId":"40088","inReplyTo":"CAPig+cQMng-bPjO3WLYb1z7aghYhhHqewSr+EGFgV6Gs6_JZRQ@mail.gmail.com","subject":"[PATCH] Correctly close config file handle in case of error","fromName":"Sven Strickroth","fromEmail":"sven@cs-ware.de","sentAt":"2015-08-14T19:44:17Z","receivedAt":"2015-08-14T19:44:17Z","isPatch":true,"sender":{"key":"sven@cs-ware.de","avatar":null},"body":"Without this patch there might be open file handle leaks.\n\nSigned-off-by: Sven Strickroth <email@cs-ware.de>\nSigned-off-by: Sup Yut Sum <ch3cooli@gmail.com>\n---\n config.c | 4 ++++\n 1 file changed, 4 insertions(+)\n\ndiff --git a/config.c b/config.c\nindex 9fd275f..c06dc2f 100644\n--- a/config.c\n+++ b/config.c\n@@ -2010,6 +2010,7 @@ int git_config_set_multivar_in_file(const char *config_filename,\n \t\t\t\terror(\"invalid pattern: %s\", value_regex);\n \t\t\t\tfree(store.value_regex);\n \t\t\t\tret = CONFIG_INVALID_PATTERN;\n+\t\t\t\tclose(in_fd);\n \t\t\t\tgoto out_free;\n \t\t\t}\n \t\t}\n@@ -2034,6 +2035,7 @@ int git_config_set_multivar_in_file(const char *config_filename,\n \t\t\t\tfree(store.value_regex);\n \t\t\t}\n \t\t\tret = CONFIG_INVALID_FILE;\n+\t\t\tclose(in_fd);\n \t\t\tgoto out_free;\n \t\t}\n \n@@ -2048,6 +2050,7 @@ int git_config_set_multivar_in_file(const char *config_filename,\n \t\tif ((store.seen == 0 && value == NULL) ||\n \t\t\t\t(store.seen > 1 && multi_replace == 0)) {\n \t\t\tret = CONFIG_NOTHING_SET;\n+\t\t\tclose(in_fd);\n \t\t\tgoto out_free;\n \t\t}\n \n@@ -2062,6 +2065,7 @@ int git_config_set_multivar_in_file(const char *config_filename,\n \t\t\t      config_filename, strerror(errno));\n \t\t\tret = CONFIG_INVALID_FILE;\n \t\t\tcontents = NULL;\n+\t\t\tclose(in_fd);\n \t\t\tgoto out_free;\n \t\t}\n \t\tclose(in_fd);\n-- \nBest regards,\n Sven Strickroth\n PGP key id F5A9D4C4 @ any key-server\n"},{"id":"268066","messageId":"CAPig+cStMdZUwDgSfzT_DgBpF-OMQe-PqTKbSLkzXFHQW8ca=Q@mail.gmail.com","threadId":"40088","inReplyTo":"55CE4511.6050704@cs-ware.de","subject":"Re: [PATCH] Correctly close config file handle in case of error","fromName":"Eric Sunshine","fromEmail":"sunshine@sunshineco.com","sentAt":"2015-08-14T19:54:50Z","receivedAt":"2015-08-14T19:54:50Z","isPatch":true,"sender":{"key":"sunshine@sunshineco.com","avatar":"https://avatars.githubusercontent.com/u/163641?v=4"},"body":"On Fri, Aug 14, 2015 at 3:44 PM, Sven Strickroth <sven@cs-ware.de> wrote:\n> Without this patch there might be open file handle leaks.\n\nThanks, this looks better. One comment below...\n\n> Signed-off-by: Sven Strickroth <email@cs-ware.de>\n> Signed-off-by: Sup Yut Sum <ch3cooli@gmail.com>\n> ---\n> diff --git a/config.c b/config.c\n> index 9fd275f..c06dc2f 100644\n> --- a/config.c\n> +++ b/config.c\n> @@ -2010,6 +2010,7 @@ int git_config_set_multivar_in_file(const char *config_filename,\n>                                 error(\"invalid pattern: %s\", value_regex);\n>                                 free(store.value_regex);\n>                                 ret = CONFIG_INVALID_PATTERN;\n> +                               close(in_fd);\n>                                 goto out_free;\n>                         }\n>                 }\n> @@ -2034,6 +2035,7 @@ int git_config_set_multivar_in_file(const char *config_filename,\n>                                 free(store.value_regex);\n>                         }\n>                         ret = CONFIG_INVALID_FILE;\n> +                       close(in_fd);\n>                         goto out_free;\n>                 }\n>\n> @@ -2048,6 +2050,7 @@ int git_config_set_multivar_in_file(const char *config_filename,\n>                 if ((store.seen == 0 && value == NULL) ||\n>                                 (store.seen > 1 && multi_replace == 0)) {\n>                         ret = CONFIG_NOTHING_SET;\n> +                       close(in_fd);\n>                         goto out_free;\n>                 }\n>\n> @@ -2062,6 +2065,7 @@ int git_config_set_multivar_in_file(const char *config_filename,\n>                               config_filename, strerror(errno));\n>                         ret = CONFIG_INVALID_FILE;\n>                         contents = NULL;\n> +                       close(in_fd);\n>                         goto out_free;\n\nEach of these cases flows through 'out_free', so an alternate approach\nwould be to close 'in_fd' there instead. Doing so has the benefit that\nit is less likely for future code changes to make the same mistake of\nfailing to close the file descriptor.\n\nOf course, you'd need to initialize 'in_fd' to some \"invalid\" value\n(such as -1) which 'out_free' can check, as well as setting 'in_fd' to\nthat invalid value after the legitimate existing close().\n\n    int in_fd = -1;\n    ...\n    if (whatever_error)\n    goto out_free;\n    ...\n    close(in_fd);\n    in_fd = -1;\n    ...\n    out_free:\n    if (in_fd >= 0)\n        close(in_fd);\n    ...\n\nor something...\n\n>                 }\n>                 close(in_fd);\n"},{"id":"268070","messageId":"55CE49AD.4010605@cs-ware.de","threadId":"40088","inReplyTo":"CAPig+cStMdZUwDgSfzT_DgBpF-OMQe-PqTKbSLkzXFHQW8ca=Q@mail.gmail.com","subject":"[PATCH] Correctly close config file handle in case of error","fromName":"Sven Strickroth","fromEmail":"sven@cs-ware.de","sentAt":"2015-08-14T20:03:57Z","receivedAt":"2015-08-14T20:03:57Z","isPatch":true,"sender":{"key":"sven@cs-ware.de","avatar":null},"body":"Without this patch there might be open file handle leaks.\n\nSigned-off-by: Sven Strickroth <email@cs-ware.de>\nSigned-off-by: Sup Yut Sum <ch3cooli@gmail.com>\n---\n config.c | 3 +++\n 1 file changed, 3 insertions(+)\n\ndiff --git a/config.c b/config.c\nindex 9fd275f..8138d5d 100644\n--- a/config.c\n+++ b/config.c\n@@ -2065,6 +2065,7 @@ int git_config_set_multivar_in_file(const char\n*config_filename,\n \t\t\tgoto out_free;\n \t\t}\n \t\tclose(in_fd);\n+\t\tin_fd = -1;\n\n \t\tif (chmod(lock->filename.buf, st.st_mode & 07777) < 0) {\n \t\t\terror(\"chmod on %s failed: %s\",\n@@ -2148,6 +2149,8 @@ out_free:\n \tfree(filename_buf);\n \tif (contents)\n \t\tmunmap(contents, contents_sz);\n+\tif (in_fd >= 0)\n+\t\tclose(in_fd);\n \treturn ret;\n\n write_err_out:\n-- \nBest regards,\n Sven Strickroth\n PGP key id F5A9D4C4 @ any key-server\n"},{"id":"268071","messageId":"CAPig+cTo=iiMvvcfRu68feofoBVSFjrd4zG8GYwXN5L6THr=8Q@mail.gmail.com","threadId":"40088","inReplyTo":"55CE49AD.4010605@cs-ware.de","subject":"Re: [PATCH] Correctly close config file handle in case of error","fromName":"Eric Sunshine","fromEmail":"sunshine@sunshineco.com","sentAt":"2015-08-14T20:14:11Z","receivedAt":"2015-08-14T20:14:11Z","isPatch":true,"sender":{"key":"sunshine@sunshineco.com","avatar":"https://avatars.githubusercontent.com/u/163641?v=4"},"body":"On Fri, Aug 14, 2015 at 4:03 PM, Sven Strickroth <sven@cs-ware.de> wrote:\n> Without this patch there might be open file handle leaks.\n>\n> Signed-off-by: Sven Strickroth <email@cs-ware.de>\n> Signed-off-by: Sup Yut Sum <ch3cooli@gmail.com>\n> ---\n> diff --git a/config.c b/config.c\n> index 9fd275f..8138d5d 100644\n> --- a/config.c\n> +++ b/config.c\n> @@ -2065,6 +2065,7 @@ int git_config_set_multivar_in_file(const char\n> *config_filename,\n>                         goto out_free;\n>                 }\n>                 close(in_fd);\n> +               in_fd = -1;\n\nYou also need to initialize 'in_fd' to -1 at its point of declaration\nsince there are goto's to 'out_free' which occur before the `in_fd =\nopen(...)'.\n\n>                 if (chmod(lock->filename.buf, st.st_mode & 07777) < 0) {\n>                         error(\"chmod on %s failed: %s\",\n> @@ -2148,6 +2149,8 @@ out_free:\n>         free(filename_buf);\n>         if (contents)\n>                 munmap(contents, contents_sz);\n> +       if (in_fd >= 0)\n> +               close(in_fd);\n>         return ret;\n>\n>  write_err_out:\n> --\n"},{"id":"268072","messageId":"55CE4DBD.2070308@cs-ware.de","threadId":"40088","inReplyTo":"CAPig+cTo=iiMvvcfRu68feofoBVSFjrd4zG8GYwXN5L6THr=8Q@mail.gmail.com","subject":"[PATCH] Correctly close config file handle in case of error","fromName":"Sven Strickroth","fromEmail":"sven@cs-ware.de","sentAt":"2015-08-14T20:21:17Z","receivedAt":"2015-08-14T20:21:17Z","isPatch":true,"sender":{"key":"sven@cs-ware.de","avatar":null},"body":"Without this patch there might be open file handle leaks.\n\nSigned-off-by: Sven Strickroth <email@cs-ware.de>\nSigned-off-by: Sup Yut Sum <ch3cooli@gmail.com>\n---\n config.c | 5 ++++-\n 1 file changed, 4 insertions(+), 1 deletion(-)\n\ndiff --git a/config.c b/config.c\nindex 9fd275f..83caa25 100644\n--- a/config.c\n+++ b/config.c\n@@ -1935,7 +1935,7 @@ int git_config_set_multivar_in_file(const char *config_filename,\n \t\t\t\tconst char *key, const char *value,\n \t\t\t\tconst char *value_regex, int multi_replace)\n {\n-\tint fd = -1, in_fd;\n+\tint fd = -1, in_fd = -1;\n \tint ret;\n \tstruct lock_file *lock = NULL;\n \tchar *filename_buf = NULL;\n@@ -2065,6 +2065,7 @@ int git_config_set_multivar_in_file(const char *config_filename,\n \t\t\tgoto out_free;\n \t\t}\n \t\tclose(in_fd);\n+\t\tin_fd = -1;\n \n \t\tif (chmod(lock->filename.buf, st.st_mode & 07777) < 0) {\n \t\t\terror(\"chmod on %s failed: %s\",\n@@ -2148,6 +2149,8 @@ out_free:\n \tfree(filename_buf);\n \tif (contents)\n \t\tmunmap(contents, contents_sz);\n+\tif (in_fd >= 0)\n+\t\tclose(in_fd);\n \treturn ret;\n \n write_err_out:\n-- \nBest regards,\n Sven Strickroth\n PGP key id F5A9D4C4 @ any key-server\n"},{"id":"268076","messageId":"CAPig+cSSdGeMuV1XLqROXvSeYfmkNc_N7E_pzfJWdDR6wfD80A@mail.gmail.com","threadId":"40088","inReplyTo":"55CE4DBD.2070308@cs-ware.de","subject":"Re: [PATCH] Correctly close config file handle in case of error","fromName":"Eric Sunshine","fromEmail":"sunshine@sunshineco.com","sentAt":"2015-08-14T20:32:35Z","receivedAt":"2015-08-14T20:32:35Z","isPatch":true,"sender":{"key":"sunshine@sunshineco.com","avatar":"https://avatars.githubusercontent.com/u/163641?v=4"},"body":"On Fri, Aug 14, 2015 at 4:21 PM, Sven Strickroth <sven@cs-ware.de> wrote:\n> Without this patch there might be open file handle leaks.\n>\n> Signed-off-by: Sven Strickroth <email@cs-ware.de>\n> Signed-off-by: Sup Yut Sum <ch3cooli@gmail.com>\n\nBetter. Thanks.\n\nReviewed-by: Eric Sunshine <sunshine@sunshineco.com>\n\n> ---\n> diff --git a/config.c b/config.c\n> index 9fd275f..83caa25 100644\n> --- a/config.c\n> +++ b/config.c\n> @@ -1935,7 +1935,7 @@ int git_config_set_multivar_in_file(const char *config_filename,\n>                                 const char *key, const char *value,\n>                                 const char *value_regex, int multi_replace)\n>  {\n> -       int fd = -1, in_fd;\n> +       int fd = -1, in_fd = -1;\n>         int ret;\n>         struct lock_file *lock = NULL;\n>         char *filename_buf = NULL;\n> @@ -2065,6 +2065,7 @@ int git_config_set_multivar_in_file(const char *config_filename,\n>                         goto out_free;\n>                 }\n>                 close(in_fd);\n> +               in_fd = -1;\n>\n>                 if (chmod(lock->filename.buf, st.st_mode & 07777) < 0) {\n>                         error(\"chmod on %s failed: %s\",\n> @@ -2148,6 +2149,8 @@ out_free:\n>         free(filename_buf);\n>         if (contents)\n>                 munmap(contents, contents_sz);\n> +       if (in_fd >= 0)\n> +               close(in_fd);\n>         return ret;\n>\n>  write_err_out:\n> --\n> Best regards,\n>  Sven Strickroth\n>  PGP key id F5A9D4C4 @ any key-server\n"},{"id":"268086","messageId":"xmqqsi7lh9fd.fsf@gitster.dls.corp.google.com","threadId":"40088","inReplyTo":"55CE4DBD.2070308@cs-ware.de","subject":"Re: [PATCH] Correctly close config file handle in case of error","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2015-08-14T21:00:54Z","receivedAt":"2015-08-14T21:00:54Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Sven Strickroth <sven@cs-ware.de> writes:\n\n> Without this patch there might be open file handle leaks.\n>\n> Signed-off-by: Sven Strickroth <email@cs-ware.de>\n> Signed-off-by: Sup Yut Sum <ch3cooli@gmail.com>\n\nWho are these two people and how are they related to this change?\nI am wondering if \"Sup\" is the author of this change and \"Sven\" is\nonly relaying his change, or the other way around or what.\n\nIn any case, your log message left a lot of room for improvement;\nhere is my attempt.\n\n-- >8 --\nFrom: Sven Strickroth <sven@cs-ware.de>\nDate: Fri, 14 Aug 2015 22:21:17 +0200\nSubject: [PATCH] config: close config file handle in case of error\n\nWhen updating an existing configuration file, we did not always\nclose the filehandle that is reading from the current configuration\nfile when we encountered an error (e.g. when unsetting a variable\nthat does not exist).\n\nSigned-off-by: Sven Strickroth <email@cs-ware.de>\nSigned-off-by: Sup Yut Sum <ch3cooli@gmail.com>\nReviewed-by: Eric Sunshine <sunshine@sunshineco.com>\nSigned-off-by: Junio C Hamano <gitster@pobox.com>\n---\n config.c | 5 ++++-\n 1 file changed, 4 insertions(+), 1 deletion(-)\n\ndiff --git a/config.c b/config.c\nindex 9fd275f..83caa25 100644\n--- a/config.c\n+++ b/config.c\n@@ -1935,7 +1935,7 @@ int git_config_set_multivar_in_file(const char *config_filename,\n \t\t\t\tconst char *key, const char *value,\n \t\t\t\tconst char *value_regex, int multi_replace)\n {\n-\tint fd = -1, in_fd;\n+\tint fd = -1, in_fd = -1;\n \tint ret;\n \tstruct lock_file *lock = NULL;\n \tchar *filename_buf = NULL;\n@@ -2065,6 +2065,7 @@ int git_config_set_multivar_in_file(const char *config_filename,\n \t\t\tgoto out_free;\n \t\t}\n \t\tclose(in_fd);\n+\t\tin_fd = -1;\n \n \t\tif (chmod(lock->filename.buf, st.st_mode & 07777) < 0) {\n \t\t\terror(\"chmod on %s failed: %s\",\n@@ -2148,6 +2149,8 @@ out_free:\n \tfree(filename_buf);\n \tif (contents)\n \t\tmunmap(contents, contents_sz);\n+\tif (in_fd >= 0)\n+\t\tclose(in_fd);\n \treturn ret;\n \n write_err_out:\n-- \n2.5.0-487-g2edca40\n"}]}