threads / patch / 40088

patchClose config file handle if the entry to unset is not found

Subject: [PATCH] Close config file handle if the entry to unset is not found

## tl;dr

10 messages between Aug 14, 2015 and Aug 14, 2015. Diffs are folded; open one to read it.

replies: 9people: 3as markdown or json

Sven Strickroth· Aug 14, 2015, 19:18 UTC · lore
Without this patch there might be open handle leaks.
Signed-off-by: Sup Yut Sum <ch3cooli@gmail.com>
Signed-off-by: Sven Strickroth <email@cs-ware.de>
---
 config.c | 1 +
 1 file changed, 1 insertion(+)
Show changes to config.c +1 −0
diff --git a/config.c b/config.c
index 9fd275f..89b49e3 100644
--- a/config.c
+++ b/config.c
@@ -2048,6 +2048,7 @@ int git_config_set_multivar_in_file(const char *config_filename,
 		if ((store.seen == 0 && value == NULL) ||
 				(store.seen > 1 && multi_replace == 0)) {
 			ret = CONFIG_NOTHING_SET;
+			close(in_fd);
 			goto out_free;
 		}
 
-- 
Best regards,
 Sven Strickroth
 PGP key id F5A9D4C4 @ any key-server
Eric Sunshine· Aug 14, 2015, 19:35 UTC · re: Sven Strickroth · lore

Re: [PATCH] Close config file handle if the entry to unset is not found

On Fri, Aug 14, 2015 at 3:18 PM, Sven Strickroth <sven@cs-ware.de> wrote:
> Without this patch there might be open handle leaks.
Thanks for the patch. A question below...
Show 13 quoted lines
> Signed-off-by: Sup Yut Sum <ch3cooli@gmail.com>
> Signed-off-by: Sven Strickroth <email@cs-ware.de>
> ---
> diff --git a/config.c b/config.c
> index 9fd275f..89b49e3 100644
> --- a/config.c
> +++ b/config.c
> @@ -2048,6 +2048,7 @@ int git_config_set_multivar_in_file(const char *config_filename,
>                 if ((store.seen == 0 && value == NULL) ||
>                                 (store.seen > 1 && multi_replace == 0)) {
>                         ret = CONFIG_NOTHING_SET;
> +                       close(in_fd);
>                         goto out_free;
>From a cursory read of the code, it appears that there are several

other places where the open 'in_fd' gets leaked which would deserve the same treatment. So, it's not clear why this patch handles only this one case. Am I missing something?

>                 }
>
> --
Sven Strickroth· Aug 14, 2015, 19:38 UTC · re: Eric Sunshine · lore

Re: [PATCH] Close config file handle if the entry to unset is not found

Am 14.08.2015 um 21:35 schrieb Eric Sunshine:
Show 18 quoted lines
>> Signed-off-by: Sup Yut Sum <ch3cooli@gmail.com>
>> Signed-off-by: Sven Strickroth <email@cs-ware.de>
>> ---
>> diff --git a/config.c b/config.c
>> index 9fd275f..89b49e3 100644
>> --- a/config.c
>> +++ b/config.c
>> @@ -2048,6 +2048,7 @@ int git_config_set_multivar_in_file(const char *config_filename,
>>                 if ((store.seen == 0 && value == NULL) ||
>>                                 (store.seen > 1 && multi_replace == 0)) {
>>                         ret = CONFIG_NOTHING_SET;
>> +                       close(in_fd);
>>                         goto out_free;
> 
> From a cursory read of the code, it appears that there are several
> other places where the open 'in_fd' gets leaked which would deserve
> the same treatment. So, it's not clear why this patch handles only
> this one case. Am I missing something?
Good point.

I suppose only this place was adjusted, becasue this is the only case which occurred so far.

Shall I re-create another patch?
-- 
Best regards,
 Sven Strickroth
 PGP key id F5A9D4C4 @ any key-server
Sven Strickroth· Aug 14, 2015, 19:44 UTC · re: Eric Sunshine · lore

[PATCH] Correctly close config file handle in case of error

Without this patch there might be open file handle leaks.
Signed-off-by: Sven Strickroth <email@cs-ware.de>
Signed-off-by: Sup Yut Sum <ch3cooli@gmail.com>
---
 config.c | 4 ++++
 1 file changed, 4 insertions(+)
Show changes to config.c +4 −0
diff --git a/config.c b/config.c
index 9fd275f..c06dc2f 100644
--- a/config.c
+++ b/config.c
@@ -2010,6 +2010,7 @@ int git_config_set_multivar_in_file(const char *config_filename,
 				error("invalid pattern: %s", value_regex);
 				free(store.value_regex);
 				ret = CONFIG_INVALID_PATTERN;
+				close(in_fd);
 				goto out_free;
 			}
 		}
@@ -2034,6 +2035,7 @@ int git_config_set_multivar_in_file(const char *config_filename,
 				free(store.value_regex);
 			}
 			ret = CONFIG_INVALID_FILE;
+			close(in_fd);
 			goto out_free;
 		}
 
@@ -2048,6 +2050,7 @@ int git_config_set_multivar_in_file(const char *config_filename,
 		if ((store.seen == 0 && value == NULL) ||
 				(store.seen > 1 && multi_replace == 0)) {
 			ret = CONFIG_NOTHING_SET;
+			close(in_fd);
 			goto out_free;
 		}
 
@@ -2062,6 +2065,7 @@ int git_config_set_multivar_in_file(const char *config_filename,
 			      config_filename, strerror(errno));
 			ret = CONFIG_INVALID_FILE;
 			contents = NULL;
+			close(in_fd);
 			goto out_free;
 		}
 		close(in_fd);
-- 
Best regards,
 Sven Strickroth
 PGP key id F5A9D4C4 @ any key-server
Eric Sunshine· Aug 14, 2015, 19:54 UTC · re: Sven Strickroth · lore

Re: [PATCH] Correctly close config file handle in case of error

On Fri, Aug 14, 2015 at 3:44 PM, Sven Strickroth <sven@cs-ware.de> wrote:
> Without this patch there might be open file handle leaks.
Thanks, this looks better. One comment below...
Show 37 quoted lines
> Signed-off-by: Sven Strickroth <email@cs-ware.de>
> Signed-off-by: Sup Yut Sum <ch3cooli@gmail.com>
> ---
> diff --git a/config.c b/config.c
> index 9fd275f..c06dc2f 100644
> --- a/config.c
> +++ b/config.c
> @@ -2010,6 +2010,7 @@ int git_config_set_multivar_in_file(const char *config_filename,
>                                 error("invalid pattern: %s", value_regex);
>                                 free(store.value_regex);
>                                 ret = CONFIG_INVALID_PATTERN;
> +                               close(in_fd);
>                                 goto out_free;
>                         }
>                 }
> @@ -2034,6 +2035,7 @@ int git_config_set_multivar_in_file(const char *config_filename,
>                                 free(store.value_regex);
>                         }
>                         ret = CONFIG_INVALID_FILE;
> +                       close(in_fd);
>                         goto out_free;
>                 }
>
> @@ -2048,6 +2050,7 @@ int git_config_set_multivar_in_file(const char *config_filename,
>                 if ((store.seen == 0 && value == NULL) ||
>                                 (store.seen > 1 && multi_replace == 0)) {
>                         ret = CONFIG_NOTHING_SET;
> +                       close(in_fd);
>                         goto out_free;
>                 }
>
> @@ -2062,6 +2065,7 @@ int git_config_set_multivar_in_file(const char *config_filename,
>                               config_filename, strerror(errno));
>                         ret = CONFIG_INVALID_FILE;
>                         contents = NULL;
> +                       close(in_fd);
>                         goto out_free;

Each of these cases flows through 'out_free', so an alternate approach would be to close 'in_fd' there instead. Doing so has the benefit that it is less likely for future code changes to make the same mistake of failing to close the file descriptor.

Of course, you'd need to initialize 'in_fd' to some "invalid" value (such as -1) which 'out_free' can check, as well as setting 'in_fd' to that invalid value after the legitimate existing close().

    int in_fd = -1;
    ...
    if (whatever_error)
    goto out_free;
    ...
    close(in_fd);
    in_fd = -1;
    ...
    out_free:
    if (in_fd >= 0)
        close(in_fd);
    ...
or something...
>                 }
>                 close(in_fd);
Sven Strickroth· Aug 14, 2015, 20:03 UTC · re: Eric Sunshine · lore

[PATCH] Correctly close config file handle in case of error

Without this patch there might be open file handle leaks.
Signed-off-by: Sven Strickroth <email@cs-ware.de>
Signed-off-by: Sup Yut Sum <ch3cooli@gmail.com>
---
 config.c | 3 +++
 1 file changed, 3 insertions(+)
Show changes to config.c +3 −0
diff --git a/config.c b/config.c
index 9fd275f..8138d5d 100644
--- a/config.c
+++ b/config.c
@@ -2065,6 +2065,7 @@ int git_config_set_multivar_in_file(const char
*config_filename,
 			goto out_free;
 		}
 		close(in_fd);
+		in_fd = -1;

 		if (chmod(lock->filename.buf, st.st_mode & 07777) < 0) {
 			error("chmod on %s failed: %s",
@@ -2148,6 +2149,8 @@ out_free:
 	free(filename_buf);
 	if (contents)
 		munmap(contents, contents_sz);
+	if (in_fd >= 0)
+		close(in_fd);
 	return ret;

 write_err_out:
-- 
Best regards,
 Sven Strickroth
 PGP key id F5A9D4C4 @ any key-server
Eric Sunshine· Aug 14, 2015, 20:14 UTC · re: Sven Strickroth · lore

Re: [PATCH] Correctly close config file handle in case of error

On Fri, Aug 14, 2015 at 4:03 PM, Sven Strickroth <sven@cs-ware.de> wrote:
Show 15 quoted lines
> Without this patch there might be open file handle leaks.
>
> Signed-off-by: Sven Strickroth <email@cs-ware.de>
> Signed-off-by: Sup Yut Sum <ch3cooli@gmail.com>
> ---
> diff --git a/config.c b/config.c
> index 9fd275f..8138d5d 100644
> --- a/config.c
> +++ b/config.c
> @@ -2065,6 +2065,7 @@ int git_config_set_multivar_in_file(const char
> *config_filename,
>                         goto out_free;
>                 }
>                 close(in_fd);
> +               in_fd = -1;

You also need to initialize 'in_fd' to -1 at its point of declaration since there are goto's to 'out_free' which occur before the `in_fd = open(...)'.

Show 12 quoted lines
>                 if (chmod(lock->filename.buf, st.st_mode & 07777) < 0) {
>                         error("chmod on %s failed: %s",
> @@ -2148,6 +2149,8 @@ out_free:
>         free(filename_buf);
>         if (contents)
>                 munmap(contents, contents_sz);
> +       if (in_fd >= 0)
> +               close(in_fd);
>         return ret;
>
>  write_err_out:
> --
Sven Strickroth· Aug 14, 2015, 20:21 UTC · re: Eric Sunshine · lore

[PATCH] Correctly close config file handle in case of error

Without this patch there might be open file handle leaks.
Signed-off-by: Sven Strickroth <email@cs-ware.de>
Signed-off-by: Sup Yut Sum <ch3cooli@gmail.com>
---
 config.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
Show changes to config.c +4 −1
diff --git a/config.c b/config.c
index 9fd275f..83caa25 100644
--- a/config.c
+++ b/config.c
@@ -1935,7 +1935,7 @@ int git_config_set_multivar_in_file(const char *config_filename,
 				const char *key, const char *value,
 				const char *value_regex, int multi_replace)
 {
-	int fd = -1, in_fd;
+	int fd = -1, in_fd = -1;
 	int ret;
 	struct lock_file *lock = NULL;
 	char *filename_buf = NULL;
@@ -2065,6 +2065,7 @@ int git_config_set_multivar_in_file(const char *config_filename,
 			goto out_free;
 		}
 		close(in_fd);
+		in_fd = -1;
 
 		if (chmod(lock->filename.buf, st.st_mode & 07777) < 0) {
 			error("chmod on %s failed: %s",
@@ -2148,6 +2149,8 @@ out_free:
 	free(filename_buf);
 	if (contents)
 		munmap(contents, contents_sz);
+	if (in_fd >= 0)
+		close(in_fd);
 	return ret;
 
 write_err_out:
-- 
Best regards,
 Sven Strickroth
 PGP key id F5A9D4C4 @ any key-server
Eric Sunshine· Aug 14, 2015, 20:32 UTC · re: Sven Strickroth · lore

Re: [PATCH] Correctly close config file handle in case of error

On Fri, Aug 14, 2015 at 4:21 PM, Sven Strickroth <sven@cs-ware.de> wrote:
> Without this patch there might be open file handle leaks.
>
> Signed-off-by: Sven Strickroth <email@cs-ware.de>
> Signed-off-by: Sup Yut Sum <ch3cooli@gmail.com>
Better. Thanks.
Reviewed-by: Eric Sunshine <sunshine@sunshineco.com>
Show 35 quoted lines
> ---
> diff --git a/config.c b/config.c
> index 9fd275f..83caa25 100644
> --- a/config.c
> +++ b/config.c
> @@ -1935,7 +1935,7 @@ int git_config_set_multivar_in_file(const char *config_filename,
>                                 const char *key, const char *value,
>                                 const char *value_regex, int multi_replace)
>  {
> -       int fd = -1, in_fd;
> +       int fd = -1, in_fd = -1;
>         int ret;
>         struct lock_file *lock = NULL;
>         char *filename_buf = NULL;
> @@ -2065,6 +2065,7 @@ int git_config_set_multivar_in_file(const char *config_filename,
>                         goto out_free;
>                 }
>                 close(in_fd);
> +               in_fd = -1;
>
>                 if (chmod(lock->filename.buf, st.st_mode & 07777) < 0) {
>                         error("chmod on %s failed: %s",
> @@ -2148,6 +2149,8 @@ out_free:
>         free(filename_buf);
>         if (contents)
>                 munmap(contents, contents_sz);
> +       if (in_fd >= 0)
> +               close(in_fd);
>         return ret;
>
>  write_err_out:
> --
> Best regards,
>  Sven Strickroth
>  PGP key id F5A9D4C4 @ any key-server
Junio C Hamano· Aug 14, 2015, 21:00 UTC · re: Sven Strickroth · lore

Re: [PATCH] Correctly close config file handle in case of error

Sven Strickroth <sven@cs-ware.de> writes:
> Without this patch there might be open file handle leaks.
>
> Signed-off-by: Sven Strickroth <email@cs-ware.de>
> Signed-off-by: Sup Yut Sum <ch3cooli@gmail.com>

Who are these two people and how are they related to this change? I am wondering if "Sup" is the author of this change and "Sven" is only relaying his change, or the other way around or what.

In any case, your log message left a lot of room for improvement; here is my attempt.

-- >8 --
From: Sven Strickroth <sven@cs-ware.de>
Date: Fri, 14 Aug 2015 22:21:17 +0200
Subject: [PATCH] config: close config file handle in case of error

When updating an existing configuration file, we did not always close the filehandle that is reading from the current configuration file when we encountered an error (e.g. when unsetting a variable that does not exist).

Signed-off-by: Sven Strickroth <email@cs-ware.de>
Signed-off-by: Sup Yut Sum <ch3cooli@gmail.com>
Reviewed-by: Eric Sunshine <sunshine@sunshineco.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 config.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
Show changes to config.c +4 −1
diff --git a/config.c b/config.c
index 9fd275f..83caa25 100644
--- a/config.c
+++ b/config.c
@@ -1935,7 +1935,7 @@ int git_config_set_multivar_in_file(const char *config_filename,
 				const char *key, const char *value,
 				const char *value_regex, int multi_replace)
 {
-	int fd = -1, in_fd;
+	int fd = -1, in_fd = -1;
 	int ret;
 	struct lock_file *lock = NULL;
 	char *filename_buf = NULL;
@@ -2065,6 +2065,7 @@ int git_config_set_multivar_in_file(const char *config_filename,
 			goto out_free;
 		}
 		close(in_fd);
+		in_fd = -1;
 
 		if (chmod(lock->filename.buf, st.st_mode & 07777) < 0) {
 			error("chmod on %s failed: %s",
@@ -2148,6 +2149,8 @@ out_free:
 	free(filename_buf);
 	if (contents)
 		munmap(contents, contents_sz);
+	if (in_fd >= 0)
+		close(in_fd);
 	return ret;
 
 write_err_out:
-- 
2.5.0-487-g2edca40

← back to recent threads