Commit 3e9e6cdaff8acb11399736abbf793bf2d000d037

Russell Belfer 2013-06-07T09:54:33

Add safe memset and use it This adds a `git__memset` routine that will not be optimized away and updates the places where I memset() right before a free() call to use it.

diff --git a/src/cache.c b/src/cache.c
index afc7c5b..570838e 100644
--- a/src/cache.c
+++ b/src/cache.c
@@ -107,7 +107,7 @@ 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));
+	git__memset(cache, 0, sizeof(*cache));
 }
 
 /* Called with lock */
diff --git a/src/config.c b/src/config.c
index 2c4b155..75cbe34 100644
--- a/src/config.c
+++ b/src/config.c
@@ -47,7 +47,7 @@ static void config_free(git_config *cfg)
 
 	git_vector_free(&cfg->files);
 
-	memset(cfg, 0, sizeof(*cfg));
+	git__memset(cfg, 0, sizeof(*cfg));
 	git__free(cfg);
 }
 
diff --git a/src/diff.c b/src/diff.c
index f1d1010..982d640 100644
--- a/src/diff.c
+++ b/src/diff.c
@@ -464,7 +464,7 @@ 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__memset(diff, 0, sizeof(*diff));
 	git__free(diff);
 }
 
diff --git a/src/index.c b/src/index.c
index abc9495..2bb7d6e 100644
--- a/src/index.c
+++ b/src/index.c
@@ -349,7 +349,7 @@ static void index_free(git_index *index)
 
 	git__free(index->index_file_path);
 
-	memset(index, 0, sizeof(*index));
+	git__memset(index, 0, sizeof(*index));
 	git__free(index);
 }
 
diff --git a/src/odb.c b/src/odb.c
index 246f7d1..5e27eda 100644
--- a/src/odb.c
+++ b/src/odb.c
@@ -590,7 +590,7 @@ static void odb_free(git_odb *db)
 	git_vector_free(&db->backends);
 	git_cache_free(&db->own_cache);
 
-	memset(db, 0, sizeof(*db));
+	git__memset(db, 0, sizeof(*db));
 	git__free(db);
 }
 
diff --git a/src/refdb.c b/src/refdb.c
index 02244c9..4271b58 100644
--- a/src/refdb.c
+++ b/src/refdb.c
@@ -89,7 +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__memset(db, 0, sizeof(*db));
 	git__free(db);
 }
 
diff --git a/src/repository.c b/src/repository.c
index 8b16f00..ee6c5ba 100644
--- a/src/repository.c
+++ b/src/repository.c
@@ -113,7 +113,7 @@ void git_repository_free(git_repository *repo)
 	git__free(repo->workdir);
 	git__free(repo->namespace);
 
-	memset(repo, 0, sizeof(*repo));
+	git__memset(repo, 0, sizeof(*repo));
 	git__free(repo);
 }
 
@@ -140,12 +140,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/util.c b/src/util.c
index da15a03..248cf4c 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 5ae87ac..fd3ea22 100644
--- a/src/util.h
+++ b/src/util.h
@@ -293,8 +293,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'
@@ -309,7 +308,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.
@@ -320,4 +319,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__ */