git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* [PATCH] config: free resources of `struct config_store_data`
@ 2018-05-13  8:23 Martin Ågren
  2018-05-13  8:59 ` Eric Sunshine
  2018-05-13 18:40 ` [PATCH] " Martin Ågren
  0 siblings, 2 replies; 18+ messages in thread
From: Martin Ågren @ 2018-05-13  8:23 UTC (permalink / raw)
  To: git; +Cc: Johannes Schindelin

Commit fee8572c6d (config: avoid using the global variable `store`,
2018-04-09) dropped the staticness of a certain struct, instead letting
the users create an instance on the stack and pass around a pointer.

We do not free the memory that the struct tracks. When the struct was
static, the memory would always be reachable. Now that we keep the
struct on the stack, though, as soon as we return, it goes out of scope
and we leak the memory it points to.

Introduce and use a small helper function `config_store_data_clear()` to
plug these leaks. This should be safe. The memory tracked here is config
parser events. Once the users (`git_config_set_multivar_in_file_gently()`
and `git_config_copy_or_rename_section_in_file()` at the moment) are
done, no-one should be holding on to a pointer into this memory.

Signed-off-by: Martin Ågren <martin.agren@gmail.com>
---
 config.c | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/config.c b/config.c
index 6f8f1d8c11..83d7d0851a 100644
--- a/config.c
+++ b/config.c
@@ -2333,6 +2333,13 @@ struct config_store_data {
 	unsigned int key_seen:1, section_seen:1, is_keys_section:1;
 };
 
+void config_store_data_clear(struct config_store_data *store)
+{
+	free(store->parsed);
+	free(store->seen);
+	memset(store, 0, sizeof(*store));
+}
+
 static int matches(const char *key, const char *value,
 		   const struct config_store_data *store)
 {
@@ -2887,6 +2894,7 @@ int git_config_set_multivar_in_file_gently(const char *config_filename,
 		munmap(contents, contents_sz);
 	if (in_fd >= 0)
 		close(in_fd);
+	config_store_data_clear(&store);
 	return ret;
 
 write_err_out:
@@ -3127,6 +3135,7 @@ static int git_config_copy_or_rename_section_in_file(const char *config_filename
 	rollback_lock_file(&lock);
 out_no_rollback:
 	free(filename_buf);
+	config_store_data_clear(&store);
 	return ret;
 }
 
-- 
2.17.0.583.g9a75a153ac


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

end of thread, other threads:[~2018-05-23  7:03 UTC | newest]

Thread overview: 18+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-05-13  8:23 [PATCH] config: free resources of `struct config_store_data` Martin Ågren
2018-05-13  8:59 ` Eric Sunshine
2018-05-13  9:58   ` Martin Ågren
2018-05-13  9:58     ` [PATCH 2/1] config: let `config_store_data_clear()` handle `value_regex` Martin Ågren
2018-05-14  3:05       ` Eric Sunshine
2018-05-20 10:50         ` [PATCH] regex: do not call `regfree()` if compilation fails Martin Ågren
2018-05-21 18:43           ` Stefan Beller
2018-05-22  2:20             ` Eric Sunshine
2018-05-22 11:00               ` Martin Ågren
2018-05-13  9:58     ` [PATCH 3/1] config: let `config_store_data_clear()` handle `key` Martin Ågren
2018-05-14  3:03     ` [PATCH] config: free resources of `struct config_store_data` Eric Sunshine
2018-05-20 10:42       ` [PATCH v2 0/3] " Martin Ågren
2018-05-20 10:42         ` [PATCH v2 1/3] " Martin Ågren
2018-05-20 10:42         ` [PATCH v2 2/3] config: let `config_store_data_clear()` handle `value_regex` Martin Ågren
2018-05-20 10:42         ` [PATCH v2 3/3] config: let `config_store_data_clear()` handle `key` Martin Ågren
2018-05-23  7:03           ` Eric Sunshine
2018-05-23  7:01         ` [PATCH v2 0/3] config: free resources of `struct config_store_data` Eric Sunshine
2018-05-13 18:40 ` [PATCH] " Martin Ågren

Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

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 read-only IMAP folder(s) and NNTP newsgroup(s).