diff options
-rw-r--r-- | src/cache.c | 9 | ||||
-rw-r--r-- | src/config.c | 4 | ||||
-rw-r--r-- | src/diff.c | 2 | ||||
-rw-r--r-- | src/global.c | 6 | ||||
-rw-r--r-- | src/index.c | 2 | ||||
-rw-r--r-- | src/odb.c | 2 | ||||
-rw-r--r-- | src/pack-objects.c | 3 | ||||
-rw-r--r-- | src/pack.c | 22 | ||||
-rw-r--r-- | src/refdb.c | 1 | ||||
-rw-r--r-- | src/repository.c | 5 | ||||
-rw-r--r-- | src/thread-utils.h | 2 | ||||
-rw-r--r-- | src/util.c | 10 | ||||
-rw-r--r-- | src/util.h | 11 |
13 files changed, 63 insertions, 16 deletions
diff --git a/src/cache.c b/src/cache.c index dc3af063a..570838e44 100644 --- a/src/cache.c +++ b/src/cache.c @@ -66,9 +66,12 @@ 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); + if (git_mutex_init(&cache->lock)) { + giterr_set(GITERR_OS, "Failed to initialize cache mutex"); + return -1; + } return 0; } @@ -102,9 +105,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); + git__memset(cache, 0, sizeof(*cache)); } /* Called with lock */ diff --git a/src/config.c b/src/config.c index 4066cf741..006025903 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); + + git__memset(cfg, 0, sizeof(*cfg)); git__free(cfg); } diff --git a/src/diff.c b/src/diff.c index 97ccb3cbd..bd2b88167 100644 --- a/src/diff.c +++ b/src/diff.c @@ -465,6 +465,8 @@ static void diff_list_free(git_diff_list *diff) git_pathspec_free(&diff->pathspec); git_pool_clear(&diff->pool); + + git__memset(diff, 0, sizeof(*diff)); git__free(diff); } diff --git a/src/global.c b/src/global.c index a0571d127..2d40ca2fc 100644 --- a/src/global.c +++ b/src/global.c @@ -61,7 +61,8 @@ int git_threads_init(void) return 0; _tls_index = TlsAlloc(); - git_mutex_init(&git__mwindow_mutex); + if (git_mutex_init(&git__mwindow_mutex)) + return -1; /* Initialize any other subsystems that have global state */ if ((error = git_hash_global_init()) >= 0) @@ -121,7 +122,8 @@ int git_threads_init(void) if (_tls_init) return 0; - git_mutex_init(&git__mwindow_mutex); + if (git_mutex_init(&git__mwindow_mutex)) + return -1; pthread_key_create(&_tls_key, &cb__free_status); /* Initialize any other subsystems that have global state */ diff --git a/src/index.c b/src/index.c index fd55616b8..d519954c5 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); + + git__memset(index, 0, sizeof(*index)); git__free(index); } @@ -589,6 +589,8 @@ static void odb_free(git_odb *db) git_vector_free(&db->backends); git_cache_free(&db->own_cache); + + git__memset(db, 0, sizeof(*db)); git__free(db); } diff --git a/src/pack-objects.c b/src/pack-objects.c index 3d382026e..500104c55 100644 --- a/src/pack-objects.c +++ b/src/pack-objects.c @@ -132,7 +132,10 @@ int git_packbuilder_new(git_packbuilder **out, git_repository *repo) if (git_mutex_init(&pb->cache_mutex) || git_mutex_init(&pb->progress_mutex) || git_cond_init(&pb->progress_cond)) + { + giterr_set(GITERR_OS, "Failed to initialize packbuilder mutex"); goto on_error; + } #endif diff --git a/src/pack.c b/src/pack.c index 417d225f3..7ce7099e0 100644 --- a/src/pack.c +++ b/src/pack.c @@ -85,15 +85,27 @@ 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); + + if (git_mutex_init(&cache->lock)) { + giterr_set(GITERR_OS, "Failed to initialize pack cache mutex"); + + git__free(cache->entries); + cache->entries = NULL; + + return -1; + } return 0; } @@ -944,7 +956,11 @@ int git_packfile_alloc(struct git_pack_file **pack_out, const char *path) p->mtime = (git_time_t)st.st_mtime; p->index_version = -1; - git_mutex_init(&p->lock); + if (git_mutex_init(&p->lock)) { + giterr_set(GITERR_OS, "Failed to initialize packfile mutex"); + git__free(p); + return -1; + } /* see if we can parse the sha1 oid in the packfile name */ if (path_len < 40 || diff --git a/src/refdb.c b/src/refdb.c index 359842e44..6da409aac 100644 --- a/src/refdb.c +++ b/src/refdb.c @@ -89,6 +89,7 @@ int git_refdb_compress(git_refdb *db) void git_refdb__free(git_refdb *db) { refdb_free_backend(db); + git__memset(db, 0, sizeof(*db)); git__free(db); } diff --git a/src/repository.c b/src/repository.c index e4451499c..9cb093dd9 100644 --- a/src/repository.c +++ b/src/repository.c @@ -119,6 +119,7 @@ void git_repository_free(git_repository *repo) git__free(repo->workdir); git__free(repo->namespace); + git__memset(repo, 0, sizeof(*repo)); git__free(repo); } @@ -145,12 +146,10 @@ static bool valid_repository_path(git_buf *repository_path) static git_repository *repository_alloc(void) { - git_repository *repo = git__malloc(sizeof(git_repository)); + git_repository *repo = git__calloc(1, sizeof(git_repository)); if (!repo) return NULL; - memset(repo, 0x0, sizeof(git_repository)); - if (git_cache_init(&repo->objects) < 0) { git__free(repo); return NULL; diff --git a/src/thread-utils.h b/src/thread-utils.h index f56f61b50..83148188d 100644 --- a/src/thread-utils.h +++ b/src/thread-utils.h @@ -138,7 +138,7 @@ GIT_INLINE(int64_t) git_atomic64_add(git_atomic64 *a, int64_t addend) /* Pthreads Mutex */ #define git_mutex unsigned int -#define git_mutex_init(a) (void)0 +#define git_mutex_init(a) 0 #define git_mutex_lock(a) 0 #define git_mutex_unlock(a) (void)0 #define git_mutex_free(a) (void)0 diff --git a/src/util.c b/src/util.c index da15a039d..248cf4c42 100644 --- a/src/util.c +++ b/src/util.c @@ -722,3 +722,13 @@ void git__insertsort_r( if (freeswap) git__free(swapel); } + +void git__memset(void *data, int c, size_t size) +{ + volatile uint8_t *scan = data; + uint8_t *end = scan + size; + uint8_t val = (uint8_t)c; + + while (scan < end) + *scan++ = val; +} diff --git a/src/util.h b/src/util.h index 43ba79240..9417515a3 100644 --- a/src/util.h +++ b/src/util.h @@ -295,8 +295,7 @@ GIT_INLINE(bool) git__iswildcard(int c) } /* - * Parse a string value as a boolean, just like Core Git - * does. + * Parse a string value as a boolean, just like Core Git does. * * Valid values for true are: 'true', 'yes', 'on' * Valid values for false are: 'false', 'no', 'off' @@ -311,7 +310,7 @@ extern int git__parse_bool(int *out, const char *value); * - "July 17, 2003" * - "2003-7-17 08:23" */ -int git__date_parse(git_time_t *out, const char *date); +extern int git__date_parse(git_time_t *out, const char *date); /* * Unescapes a string in-place. @@ -322,4 +321,10 @@ int git__date_parse(git_time_t *out, const char *date); */ extern size_t git__unescape(char *str); +/* + * Memset that will not be optimized away by the compiler. + * You usually should just use regular `memset()`. + */ +extern void git__memset(void *data, int c, size_t size); + #endif /* INCLUDE_util_h__ */ |