pool: use `size_t` for sizes
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
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));