Commit d103f0088745327e052f425b1166fe3f6ef503ed

Edward Thomson 2019-05-21T13:44:47

pool: use `size_t` for sizes

diff --git a/src/attrcache.c b/src/attrcache.c
index 00e4596..b88bc09 100644
--- a/src/attrcache.c
+++ b/src/attrcache.c
@@ -54,7 +54,7 @@ int git_attr_cache__alloc_file_entry(
 			cachesize++;
 	}
 
-	ce = git_pool_mallocz(pool, (uint32_t)cachesize);
+	ce = git_pool_mallocz(pool, cachesize);
 	GIT_ERROR_CHECK_ALLOC(ce);
 
 	if (baselen) {
diff --git a/src/commit_list.c b/src/commit_list.c
index 5496dca..44673d9 100644
--- a/src/commit_list.c
+++ b/src/commit_list.c
@@ -73,7 +73,7 @@ static git_commit_list_node **alloc_parents(
 		return (git_commit_list_node **)((char *)commit + sizeof(git_commit_list_node));
 
 	return (git_commit_list_node **)git_pool_malloc(
-		&walk->commit_pool, (uint32_t)(n_parents * sizeof(git_commit_list_node *)));
+		&walk->commit_pool, (n_parents * sizeof(git_commit_list_node *)));
 }
 
 
diff --git a/src/fileops.c b/src/fileops.c
index a4d5cc6..0b732aa 100644
--- a/src/fileops.c
+++ b/src/fileops.c
@@ -636,9 +636,7 @@ retry_lstat:
 			size_t alloc_size;
 
 			GIT_ERROR_CHECK_ALLOC_ADD(&alloc_size, make_path.size, 1);
-			if (!git__is_uint32(alloc_size))
-				return -1;
-			cache_path = git_pool_malloc(opts->pool, (uint32_t)alloc_size);
+			cache_path = git_pool_malloc(opts->pool, alloc_size);
 			GIT_ERROR_CHECK_ALLOC(cache_path);
 
 			memcpy(cache_path, make_path.ptr, make_path.size + 1);
diff --git a/src/iterator.c b/src/iterator.c
index 12b2822..d00b8aa 100644
--- a/src/iterator.c
+++ b/src/iterator.c
@@ -1303,12 +1303,7 @@ static int filesystem_iterator_entry_init(
 		sizeof(filesystem_iterator_entry), path_len);
 	GIT_ERROR_CHECK_ALLOC_ADD(&entry_size, entry_size, 2);
 
-	if (entry_size > UINT32_MAX) {
-		git_error_set(GIT_ERROR_REPOSITORY, "file path too long");
-		return -1;
-	}
-
-	entry = git_pool_malloc(&frame->entry_pool, (uint32_t)entry_size);
+	entry = git_pool_malloc(&frame->entry_pool, entry_size);
 	GIT_ERROR_CHECK_ALLOC(entry);
 
 	entry->path_len = path_len;
diff --git a/src/pool.c b/src/pool.c
index 49f2a53..b3bc8d4 100644
--- a/src/pool.c
+++ b/src/pool.c
@@ -14,30 +14,30 @@
 
 struct git_pool_page {
 	git_pool_page *next;
-	uint32_t size;
-	uint32_t avail;
+	size_t size;
+	size_t avail;
 	GIT_ALIGN(char data[GIT_FLEX_ARRAY], 8);
 };
 
-static void *pool_alloc_page(git_pool *pool, uint32_t size);
+static void *pool_alloc_page(git_pool *pool, size_t size);
 
-uint32_t git_pool__system_page_size(void)
+size_t git_pool__system_page_size(void)
 {
-	static uint32_t size = 0;
+	static size_t size = 0;
 
 	if (!size) {
 		size_t page_size;
 		if (git__page_size(&page_size) < 0)
 			page_size = 4096;
 		/* allow space for malloc overhead */
-		size = (uint32_t)(page_size - (2 * sizeof(void *)) - sizeof(git_pool_page));
+		size = (page_size - (2 * sizeof(void *)) - sizeof(git_pool_page));
 	}
 
 	return size;
 }
 
 #ifndef GIT_DEBUG_POOL
-void git_pool_init(git_pool *pool, uint32_t item_size)
+void git_pool_init(git_pool *pool, size_t item_size)
 {
 	assert(pool);
 	assert(item_size >= 1);
@@ -59,10 +59,10 @@ void git_pool_clear(git_pool *pool)
 	pool->pages = NULL;
 }
 
-static void *pool_alloc_page(git_pool *pool, uint32_t size)
+static void *pool_alloc_page(git_pool *pool, size_t size)
 {
 	git_pool_page *page;
-	const uint32_t new_page_size = (size <= pool->page_size) ? pool->page_size : size;
+	const size_t new_page_size = (size <= pool->page_size) ? pool->page_size : size;
 	size_t alloc_size;
 
 	if (GIT_ADD_SIZET_OVERFLOW(&alloc_size, new_page_size, sizeof(git_pool_page)) ||
@@ -78,7 +78,7 @@ static void *pool_alloc_page(git_pool *pool, uint32_t size)
 	return page->data;
 }
 
-static void *pool_alloc(git_pool *pool, uint32_t size)
+static void *pool_alloc(git_pool *pool, size_t size)
 {
 	git_pool_page *page = pool->pages;
 	void *ptr = NULL;
@@ -125,7 +125,7 @@ static int git_pool__ptr_cmp(const void * a, const void * b)
 	}
 }
 
-void git_pool_init(git_pool *pool, uint32_t item_size)
+void git_pool_init(git_pool *pool, size_t item_size)
 {
 	assert(pool);
 	assert(item_size >= 1);
@@ -141,7 +141,7 @@ void git_pool_clear(git_pool *pool)
 	git_vector_free_deep(&pool->allocations);
 }
 
-static void *pool_alloc(git_pool *pool, uint32_t size) {
+static void *pool_alloc(git_pool *pool, size_t size) {
 	void *ptr = NULL;
 	if((ptr = git__malloc(size)) == NULL) {
 		return NULL;
@@ -169,26 +169,26 @@ void git_pool_swap(git_pool *a, git_pool *b)
 	memcpy(b, &temp, sizeof(temp));
 }
 
-static uint32_t alloc_size(git_pool *pool, uint32_t count)
+static size_t alloc_size(git_pool *pool, size_t count)
 {
-	const uint32_t align = sizeof(void *) - 1;
+	const size_t align = sizeof(void *) - 1;
 
 	if (pool->item_size > 1) {
-		const uint32_t item_size = (pool->item_size + align) & ~align;
+		const size_t item_size = (pool->item_size + align) & ~align;
 		return item_size * count;
 	}
 
 	return (count + align) & ~align;
 }
 
-void *git_pool_malloc(git_pool *pool, uint32_t items)
+void *git_pool_malloc(git_pool *pool, size_t items)
 {
 	return pool_alloc(pool, alloc_size(pool, items));
 }
 
-void *git_pool_mallocz(git_pool *pool, uint32_t items)
+void *git_pool_mallocz(git_pool *pool, size_t items)
 {
-	const uint32_t size = alloc_size(pool, items);
+	const size_t size = alloc_size(pool, items);
 	void *ptr = pool_alloc(pool, size);
 	if (ptr)
 		memset(ptr, 0x0, size);
@@ -201,10 +201,10 @@ char *git_pool_strndup(git_pool *pool, const char *str, size_t n)
 
 	assert(pool && str && pool->item_size == sizeof(char));
 
-	if ((uint32_t)(n + 1) < n)
+	if (n == SIZE_MAX)
 		return NULL;
 
-	if ((ptr = git_pool_malloc(pool, (uint32_t)(n + 1))) != NULL) {
+	if ((ptr = git_pool_malloc(pool, (n + 1))) != NULL) {
 		memcpy(ptr, str, n);
 		ptr[n] = '\0';
 	}
@@ -226,14 +226,18 @@ char *git_pool_strdup_safe(git_pool *pool, const char *str)
 char *git_pool_strcat(git_pool *pool, const char *a, const char *b)
 {
 	void *ptr;
-	size_t len_a, len_b;
+	size_t len_a, len_b, total;
 
 	assert(pool && pool->item_size == sizeof(char));
 
 	len_a = a ? strlen(a) : 0;
 	len_b = b ? strlen(b) : 0;
 
-	if ((ptr = git_pool_malloc(pool, (uint32_t)(len_a + len_b + 1))) != NULL) {
+	if (GIT_ADD_SIZET_OVERFLOW(&total, len_a, len_b) ||
+		GIT_ADD_SIZET_OVERFLOW(&total, total, 1))
+		return NULL;
+
+	if ((ptr = git_pool_malloc(pool, total)) != NULL) {
 		if (len_a)
 			memcpy(ptr, a, len_a);
 		if (len_b)
diff --git a/src/pool.h b/src/pool.h
index 92ddf99..23f6899 100644
--- a/src/pool.h
+++ b/src/pool.h
@@ -32,8 +32,8 @@ typedef struct git_pool_page git_pool_page;
  */
 typedef struct {
 	git_pool_page *pages; /* allocated pages */
-	uint32_t item_size;  /* size of single alloc unit in bytes */
-	uint32_t page_size;  /* size of page in bytes */
+	size_t item_size;  /* size of single alloc unit in bytes */
+	size_t page_size;  /* size of page in bytes */
 } git_pool;
 
 #define GIT_POOL_INIT { NULL, 0, 0 }
@@ -57,8 +57,8 @@ typedef struct {
  */
 typedef struct {
 	git_vector allocations;
-	uint32_t item_size;
-	uint32_t page_size;
+	size_t item_size;
+	size_t page_size;
 } git_pool;
 
 #define GIT_POOL_INIT { GIT_VECTOR_INIT, 0, 0 }
@@ -81,7 +81,7 @@ typedef struct {
  * Of course, you can use this in other ways, but those are the
  * two most common patterns.
  */
-extern void git_pool_init(git_pool *pool, uint32_t item_size);
+extern void git_pool_init(git_pool *pool, size_t item_size);
 
 /**
  * Free all items in pool
@@ -96,8 +96,8 @@ extern void git_pool_swap(git_pool *a, git_pool *b);
 /**
  * Allocate space for one or more items from a pool.
  */
-extern void *git_pool_malloc(git_pool *pool, uint32_t items);
-extern void *git_pool_mallocz(git_pool *pool, uint32_t items);
+extern void *git_pool_malloc(git_pool *pool, size_t items);
+extern void *git_pool_mallocz(git_pool *pool, size_t items);
 
 /**
  * Allocate space and duplicate string data into it.
diff --git a/src/sortedcache.c b/src/sortedcache.c
index 15ba6fd..8f7ea23 100644
--- a/src/sortedcache.c
+++ b/src/sortedcache.c
@@ -282,7 +282,7 @@ int git_sortedcache_upsert(void **out, git_sortedcache *sc, const char *key)
 	itemlen = sc->item_path_offset + keylen + 1;
 	itemlen = (itemlen + 7) & ~7;
 
-	if ((item = git_pool_mallocz(&sc->pool, (uint32_t)itemlen)) == NULL) {
+	if ((item = git_pool_mallocz(&sc->pool, itemlen)) == NULL) {
 		/* don't use GIT_ERROR_CHECK_ALLOC b/c of lock */
 		error = -1;
 		goto done;
diff --git a/src/tree-cache.c b/src/tree-cache.c
index 9d1c781..04d86fd 100644
--- a/src/tree-cache.c
+++ b/src/tree-cache.c
@@ -120,15 +120,10 @@ static int read_tree_internal(git_tree_cache **out,
 
 	/* Parse children: */
 	if (tree->children_count > 0) {
-		size_t i;
-		uint32_t bufsize;
+		size_t i, bufsize;
 
-		if (tree->children_count > UINT32_MAX / sizeof(git_tree_cache *)) {
-			git_error_set_oom();
-			return -1;
-		}
+		GIT_ERROR_CHECK_ALLOC_MULTIPLY(&bufsize, tree->children_count, sizeof(git_tree_cache*));
 
-		bufsize = (uint32_t)(tree->children_count * sizeof(git_tree_cache *));
 		tree->children = git_pool_malloc(pool, bufsize);
 		GIT_ERROR_CHECK_ALLOC(tree->children);
 
@@ -167,7 +162,7 @@ int git_tree_cache_read(git_tree_cache **tree, const char *buffer, size_t buffer
 static int read_tree_recursive(git_tree_cache *cache, const git_tree *tree, git_pool *pool)
 {
 	git_repository *repo;
-	size_t i, j, nentries, ntrees;
+	size_t i, j, nentries, ntrees, alloc_size;
 	int error;
 
 	repo = git_tree_owner(tree);
@@ -189,13 +184,10 @@ static int read_tree_recursive(git_tree_cache *cache, const git_tree *tree, git_
 			ntrees++;
 	}
 
-	if (ntrees > UINT32_MAX / sizeof(git_tree_cache *)) {
-		git_error_set_oom();
-		return -1;
-	}
+	GIT_ERROR_CHECK_ALLOC_MULTIPLY(&alloc_size, ntrees, sizeof(git_tree_cache *));
 
 	cache->children_count = ntrees;
-	cache->children = git_pool_mallocz(pool, (uint32_t)(ntrees * sizeof(git_tree_cache *)));
+	cache->children = git_pool_mallocz(pool, alloc_size);
 	GIT_ERROR_CHECK_ALLOC(cache->children);
 
 	j = 0;
@@ -251,12 +243,7 @@ int git_tree_cache_new(git_tree_cache **out, const char *name, git_pool *pool)
 
 	GIT_ERROR_CHECK_ALLOC_ADD3(&alloc_size, sizeof(git_tree_cache), name_len, 1);
 
-	if (alloc_size > UINT32_MAX) {
-		git_error_set_oom();
-		return -1;
-	}
-
-	tree = git_pool_malloc(pool, (uint32_t)alloc_size);
+	tree = git_pool_malloc(pool, alloc_size);
 	GIT_ERROR_CHECK_ALLOC(tree);
 
 	memset(tree, 0x0, sizeof(git_tree_cache));