Commit be8f9bb1 by Patrick Steinhardt

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.
parent 7277bf83
......@@ -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;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment