Skip to content

Commit be8f9bb

Browse files
committed
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.
1 parent 7277bf8 commit be8f9bb

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

src/attrcache.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -428,9 +428,18 @@ int git_attr_cache__insert_macro(git_repository *repo, git_attr_rule *macro)
428428
bool locked = false;
429429
int error = 0;
430430

431-
/* TODO: generate warning log if (macro->assigns.length == 0) */
432-
if (macro->assigns.length == 0)
431+
/*
432+
* Callers assume that if we return success, that the
433+
* macro will have been adopted by the attributes cache.
434+
* Thus, we have to free the macro here if it's not being
435+
* added to the cache.
436+
*
437+
* TODO: generate warning log if (macro->assigns.length == 0)
438+
*/
439+
if (macro->assigns.length == 0) {
440+
git_attr_rule__free(macro);
433441
goto out;
442+
}
434443

435444
if ((error = attr_cache_lock(cache)) < 0)
436445
goto out;

0 commit comments

Comments
 (0)