Commit 3e9e6cda by Russell Belfer

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.
parent 1a42dd17
......@@ -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 */
......
......@@ -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);
}
......
......@@ -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);
}
......
......@@ -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);
}
......
......@@ -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);
}
......
......@@ -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);
}
......
......@@ -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;
......
......@@ -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;
}
......@@ -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__ */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment