Commit d103f008 by Edward Thomson

pool: use `size_t` for sizes

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