git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] Close config file handle if the entry to unset is not found
@ 2015-08-14 19:18 Sven Strickroth
  2015-08-14 19:35 ` Eric Sunshine
  0 siblings, 1 reply; 10+ messages in thread
From: Sven Strickroth @ 2015-08-14 19:18 UTC (permalink / raw)
  To: git, gitster

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(+)

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

^ permalink raw reply related	[flat|nested] 10+ messages in thread

* Re: [PATCH] Close config file handle if the entry to unset is not found
  2015-08-14 19:18 [PATCH] Close config file handle if the entry to unset is not found Sven Strickroth
@ 2015-08-14 19:35 ` Eric Sunshine
  2015-08-14 19:38   ` Sven Strickroth
  2015-08-14 19:44   ` [PATCH] Correctly close config file handle in case of error Sven Strickroth
  0 siblings, 2 replies; 10+ messages in thread
From: Eric Sunshine @ 2015-08-14 19:35 UTC (permalink / raw)
  To: Sven Strickroth; +Cc: Git List, Junio C Hamano

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...

> 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?

>                 }
>
> --

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] Close config file handle if the entry to unset is not found
  2015-08-14 19:35 ` Eric Sunshine
@ 2015-08-14 19:38   ` Sven Strickroth
  2015-08-14 19:44   ` [PATCH] Correctly close config file handle in case of error Sven Strickroth
  1 sibling, 0 replies; 10+ messages in thread
From: Sven Strickroth @ 2015-08-14 19:38 UTC (permalink / raw)
  To: Eric Sunshine; +Cc: Git List, Junio C Hamano

Am 14.08.2015 um 21:35 schrieb Eric Sunshine:
>> 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

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [PATCH] Correctly close config file handle in case of error
  2015-08-14 19:35 ` Eric Sunshine
  2015-08-14 19:38   ` Sven Strickroth
@ 2015-08-14 19:44   ` Sven Strickroth
  2015-08-14 19:54     ` Eric Sunshine
  1 sibling, 1 reply; 10+ messages in thread
From: Sven Strickroth @ 2015-08-14 19:44 UTC (permalink / raw)
  To: Eric Sunshine, Git List, Junio C Hamano

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(+)

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

^ permalink raw reply related	[flat|nested] 10+ messages in thread

* Re: [PATCH] Correctly close config file handle in case of error
  2015-08-14 19:44   ` [PATCH] Correctly close config file handle in case of error Sven Strickroth
@ 2015-08-14 19:54     ` Eric Sunshine
  2015-08-14 20:03       ` Sven Strickroth
  0 siblings, 1 reply; 10+ messages in thread
From: Eric Sunshine @ 2015-08-14 19:54 UTC (permalink / raw)
  To: Sven Strickroth; +Cc: Git List, Junio C Hamano

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...

> 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);

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [PATCH] Correctly close config file handle in case of error
  2015-08-14 19:54     ` Eric Sunshine
@ 2015-08-14 20:03       ` Sven Strickroth
  2015-08-14 20:14         ` Eric Sunshine
  0 siblings, 1 reply; 10+ messages in thread
From: Sven Strickroth @ 2015-08-14 20:03 UTC (permalink / raw)
  To: Eric Sunshine, Git List, Junio C Hamano

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(+)

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

^ permalink raw reply related	[flat|nested] 10+ messages in thread

* Re: [PATCH] Correctly close config file handle in case of error
  2015-08-14 20:03       ` Sven Strickroth
@ 2015-08-14 20:14         ` Eric Sunshine
  2015-08-14 20:21           ` Sven Strickroth
  0 siblings, 1 reply; 10+ messages in thread
From: Eric Sunshine @ 2015-08-14 20:14 UTC (permalink / raw)
  To: Sven Strickroth; +Cc: Git List, Junio C Hamano

On Fri, Aug 14, 2015 at 4:03 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>
> ---
> 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(...)'.

>                 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:
> --

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [PATCH] Correctly close config file handle in case of error
  2015-08-14 20:14         ` Eric Sunshine
@ 2015-08-14 20:21           ` Sven Strickroth
  2015-08-14 20:32             ` Eric Sunshine
  2015-08-14 21:00             ` Junio C Hamano
  0 siblings, 2 replies; 10+ messages in thread
From: Sven Strickroth @ 2015-08-14 20:21 UTC (permalink / raw)
  To: Eric Sunshine, Git List, Junio C Hamano

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(-)

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

^ permalink raw reply related	[flat|nested] 10+ messages in thread

* Re: [PATCH] Correctly close config file handle in case of error
  2015-08-14 20:21           ` Sven Strickroth
@ 2015-08-14 20:32             ` Eric Sunshine
  2015-08-14 21:00             ` Junio C Hamano
  1 sibling, 0 replies; 10+ messages in thread
From: Eric Sunshine @ 2015-08-14 20:32 UTC (permalink / raw)
  To: Sven Strickroth; +Cc: Git List, Junio C Hamano

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>

> ---
> 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

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] Correctly close config file handle in case of error
  2015-08-14 20:21           ` Sven Strickroth
  2015-08-14 20:32             ` Eric Sunshine
@ 2015-08-14 21:00             ` Junio C Hamano
  1 sibling, 0 replies; 10+ messages in thread
From: Junio C Hamano @ 2015-08-14 21:00 UTC (permalink / raw)
  To: Sven Strickroth; +Cc: Eric Sunshine, Git List

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(-)

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

^ permalink raw reply related	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2015-08-14 21:01 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-08-14 19:18 [PATCH] Close config file handle if the entry to unset is not found Sven Strickroth
2015-08-14 19:35 ` Eric Sunshine
2015-08-14 19:38   ` Sven Strickroth
2015-08-14 19:44   ` [PATCH] Correctly close config file handle in case of error Sven Strickroth
2015-08-14 19:54     ` Eric Sunshine
2015-08-14 20:03       ` Sven Strickroth
2015-08-14 20:14         ` Eric Sunshine
2015-08-14 20:21           ` Sven Strickroth
2015-08-14 20:32             ` Eric Sunshine
2015-08-14 21:00             ` Junio C Hamano

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).