Fixed up memory leaks
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
diff --git a/src/attr.c b/src/attr.c
index fac2fa4..f984458 100644
--- a/src/attr.c
+++ b/src/attr.c
@@ -207,10 +207,8 @@ int git_attr_add_macro(
if (error == GIT_SUCCESS)
error = git_attr_cache__insert_macro(repo, macro);
- if (error < GIT_SUCCESS) {
+ if (error < GIT_SUCCESS)
git_attr_rule__free(macro);
- git__free(macro);
- }
return error;
}
diff --git a/src/attr_file.c b/src/attr_file.c
index 0b1eb1f..a137905 100644
--- a/src/attr_file.c
+++ b/src/attr_file.c
@@ -8,6 +8,7 @@ const char *git_attr__false = "[internal]__FALSE__";
static int git_attr_fnmatch__parse(git_attr_fnmatch *spec, const char **base);
static int sort_by_hash_and_name(const void *a_raw, const void *b_raw);
+static void git_attr_rule__clear(git_attr_rule *rule);
int git_attr_cache__insert_macro(git_repository *repo, git_attr_rule *macro)
{
@@ -72,7 +73,7 @@ int git_attr_file__from_buffer(
/* if the rule wasn't a pattern, on to the next */
if (error != GIT_SUCCESS) {
- git_attr_rule__free(rule); /* free anything partially allocated */
+ git_attr_rule__clear(rule); /* reset rule contents */
if (error == GIT_ENOTFOUND)
error = GIT_SUCCESS;
} else {
@@ -82,9 +83,8 @@ int git_attr_file__from_buffer(
cleanup:
if (error != GIT_SUCCESS) {
- git__free(rule);
+ git_attr_rule__free(rule);
git_attr_file__free(attrs);
- git__free(attrs);
} else {
*out = attrs;
}
@@ -122,14 +122,15 @@ void git_attr_file__free(git_attr_file *file)
if (!file)
return;
- git_vector_foreach(&file->rules, i, rule) {
+ git_vector_foreach(&file->rules, i, rule)
git_attr_rule__free(rule);
- }
git_vector_free(&file->rules);
git__free(file->path);
file->path = NULL;
+
+ git__free(file);
}
unsigned long git_attr_file__name_hash(const char *name)
@@ -505,7 +506,7 @@ int git_attr_assignment__parse(
return error;
}
-void git_attr_rule__free(git_attr_rule *rule)
+static void git_attr_rule__clear(git_attr_rule *rule)
{
unsigned int i;
git_attr_assignment *assign;
@@ -525,3 +526,10 @@ void git_attr_rule__free(git_attr_rule *rule)
git_vector_free(&rule->assigns);
}
+
+void git_attr_rule__free(git_attr_rule *rule)
+{
+ git_attr_rule__clear(rule);
+ git__free(rule);
+}
+