summaryrefslogtreecommitdiff
path: root/src/attrcache.c
diff options
context:
space:
mode:
authorPatrick Steinhardt <ps@pks.im>2019-07-05 13:33:10 +0200
committerPatrick Steinhardt <ps@pks.im>2019-07-12 09:26:22 +0200
commitbe8f9bb188668cb1f1976f28f2aa4fef9e74890a (patch)
tree5a319762da69e651f3c77ac7274cddf12eef4f75 /src/attrcache.c
parent7277bf833d3d56723feee2c415d271ab933ca439 (diff)
downloadlibgit2-be8f9bb188668cb1f1976f28f2aa4fef9e74890a.tar.gz
attrcache: fix memory leak if inserting invalid macro to cache
A macro without any assignments is considered an invalid macro by the attributes cache and is thus not getting added to the macro map at all. But as `git_attr_cache__insert_macro` returns success with neither free'ing nor adopting the macro into its map, this will cause a memory leak. Fix this by freeing the macro in the function if it's not going to be added. This is perfectly fine to do, as callers assume that the attrcache will have the macro adopted on success anyway.
Diffstat (limited to 'src/attrcache.c')
-rw-r--r--src/attrcache.c13
1 files changed, 11 insertions, 2 deletions
diff --git a/src/attrcache.c b/src/attrcache.c
index 52a883065..c2cf697e0 100644
--- a/src/attrcache.c
+++ b/src/attrcache.c
@@ -428,9 +428,18 @@ int git_attr_cache__insert_macro(git_repository *repo, git_attr_rule *macro)
bool locked = false;
int error = 0;
- /* TODO: generate warning log if (macro->assigns.length == 0) */
- if (macro->assigns.length == 0)
+ /*
+ * Callers assume that if we return success, that the
+ * macro will have been adopted by the attributes cache.
+ * Thus, we have to free the macro here if it's not being
+ * added to the cache.
+ *
+ * TODO: generate warning log if (macro->assigns.length == 0)
+ */
+ if (macro->assigns.length == 0) {
+ git_attr_rule__free(macro);
goto out;
+ }
if ((error = attr_cache_lock(cache)) < 0)
goto out;