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.
diff --git a/src/attrcache.c b/src/attrcache.c
index 52a8830..c2cf697 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;