Zero memory for major objects before freeing By zeroing out the memory when we free larger objects (i.e. those that serve as collections of other data, such as repos, odb, refdb), I'm hoping that it will be easier for libgit2 bindings to find errors in their object management code.
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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
diff --git a/src/cache.c b/src/cache.c
index dc3af06..6205ef8 100644
--- a/src/cache.c
+++ b/src/cache.c
@@ -66,7 +66,7 @@ void git_cache_dump_stats(git_cache *cache)
int git_cache_init(git_cache *cache)
{
- cache->used_memory = 0;
+ memset(cache, 0, sizeof(*cache));
cache->map = git_oidmap_alloc();
git_mutex_init(&cache->lock);
return 0;
@@ -102,9 +102,9 @@ void git_cache_clear(git_cache *cache)
void git_cache_free(git_cache *cache)
{
git_cache_clear(cache);
-
git_oidmap_free(cache->map);
git_mutex_free(&cache->lock);
+ memset(cache, 0, sizeof(*cache));
}
/* Called with lock */
diff --git a/src/config.c b/src/config.c
index 9491d26..2c4b155 100644
--- a/src/config.c
+++ b/src/config.c
@@ -40,12 +40,14 @@ static void config_free(git_config *cfg)
size_t i;
file_internal *internal;
- for(i = 0; i < cfg->files.length; ++i){
+ for (i = 0; i < cfg->files.length; ++i) {
internal = git_vector_get(&cfg->files, i);
GIT_REFCOUNT_DEC(internal, file_internal_free);
}
git_vector_free(&cfg->files);
+
+ memset(cfg, 0, sizeof(*cfg));
git__free(cfg);
}
diff --git a/src/diff.c b/src/diff.c
index b96ff47..f1d1010 100644
--- a/src/diff.c
+++ b/src/diff.c
@@ -463,6 +463,8 @@ static void diff_list_free(git_diff_list *diff)
git_pathspec_free(&diff->pathspec);
git_pool_clear(&diff->pool);
+
+ memset(diff, 0, sizeof(*diff));
git__free(diff);
}
diff --git a/src/index.c b/src/index.c
index 45ce2f3..abc9495 100644
--- a/src/index.c
+++ b/src/index.c
@@ -348,6 +348,8 @@ static void index_free(git_index *index)
git_vector_free(&index->reuc);
git__free(index->index_file_path);
+
+ memset(index, 0, sizeof(*index));
git__free(index);
}
diff --git a/src/odb.c b/src/odb.c
index 9f18b51..246f7d1 100644
--- a/src/odb.c
+++ b/src/odb.c
@@ -589,6 +589,8 @@ static void odb_free(git_odb *db)
git_vector_free(&db->backends);
git_cache_free(&db->own_cache);
+
+ memset(db, 0, sizeof(*db));
git__free(db);
}
diff --git a/src/pack.c b/src/pack.c
index 417d225..a9b835f 100644
--- a/src/pack.c
+++ b/src/pack.c
@@ -85,13 +85,17 @@ static void cache_free(git_pack_cache *cache)
git_offmap_free(cache->entries);
git_mutex_free(&cache->lock);
}
+
+ memset(cache, 0, sizeof(*cache));
}
static int cache_init(git_pack_cache *cache)
{
- memset(cache, 0, sizeof(git_pack_cache));
+ memset(cache, 0, sizeof(*cache));
+
cache->entries = git_offmap_alloc();
GITERR_CHECK_ALLOC(cache->entries);
+
cache->memory_limit = GIT_PACK_CACHE_MEMORY_LIMIT;
git_mutex_init(&cache->lock);
diff --git a/src/refdb.c b/src/refdb.c
index 9f9037c..02244c9 100644
--- a/src/refdb.c
+++ b/src/refdb.c
@@ -89,6 +89,7 @@ int git_refdb_compress(git_refdb *db)
static void refdb_free(git_refdb *db)
{
refdb_free_backend(db);
+ memset(db, 0, sizeof(*db));
git__free(db);
}
diff --git a/src/repository.c b/src/repository.c
index 28505e8..8b16f00 100644
--- a/src/repository.c
+++ b/src/repository.c
@@ -113,6 +113,7 @@ void git_repository_free(git_repository *repo)
git__free(repo->workdir);
git__free(repo->namespace);
+ memset(repo, 0, sizeof(*repo));
git__free(repo);
}