Skip to content

Commit d103f00

Browse files
committed
pool: use size_t for sizes
1 parent c4a64b1 commit d103f00

File tree

8 files changed

+44
-60
lines changed

8 files changed

+44
-60
lines changed

src/attrcache.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ int git_attr_cache__alloc_file_entry(
5454
cachesize++;
5555
}
5656

57-
ce = git_pool_mallocz(pool, (uint32_t)cachesize);
57+
ce = git_pool_mallocz(pool, cachesize);
5858
GIT_ERROR_CHECK_ALLOC(ce);
5959

6060
if (baselen) {

src/commit_list.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ static git_commit_list_node **alloc_parents(
7373
return (git_commit_list_node **)((char *)commit + sizeof(git_commit_list_node));
7474

7575
return (git_commit_list_node **)git_pool_malloc(
76-
&walk->commit_pool, (uint32_t)(n_parents * sizeof(git_commit_list_node *)));
76+
&walk->commit_pool, (n_parents * sizeof(git_commit_list_node *)));
7777
}
7878

7979

src/fileops.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -636,9 +636,7 @@ int git_futils_mkdir_relative(
636636
size_t alloc_size;
637637

638638
GIT_ERROR_CHECK_ALLOC_ADD(&alloc_size, make_path.size, 1);
639-
if (!git__is_uint32(alloc_size))
640-
return -1;
641-
cache_path = git_pool_malloc(opts->pool, (uint32_t)alloc_size);
639+
cache_path = git_pool_malloc(opts->pool, alloc_size);
642640
GIT_ERROR_CHECK_ALLOC(cache_path);
643641

644642
memcpy(cache_path, make_path.ptr, make_path.size + 1);

src/iterator.c

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1303,12 +1303,7 @@ static int filesystem_iterator_entry_init(
13031303
sizeof(filesystem_iterator_entry), path_len);
13041304
GIT_ERROR_CHECK_ALLOC_ADD(&entry_size, entry_size, 2);
13051305

1306-
if (entry_size > UINT32_MAX) {
1307-
git_error_set(GIT_ERROR_REPOSITORY, "file path too long");
1308-
return -1;
1309-
}
1310-
1311-
entry = git_pool_malloc(&frame->entry_pool, (uint32_t)entry_size);
1306+
entry = git_pool_malloc(&frame->entry_pool, entry_size);
13121307
GIT_ERROR_CHECK_ALLOC(entry);
13131308

13141309
entry->path_len = path_len;

src/pool.c

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,30 +14,30 @@
1414

1515
struct git_pool_page {
1616
git_pool_page *next;
17-
uint32_t size;
18-
uint32_t avail;
17+
size_t size;
18+
size_t avail;
1919
GIT_ALIGN(char data[GIT_FLEX_ARRAY], 8);
2020
};
2121

22-
static void *pool_alloc_page(git_pool *pool, uint32_t size);
22+
static void *pool_alloc_page(git_pool *pool, size_t size);
2323

24-
uint32_t git_pool__system_page_size(void)
24+
size_t git_pool__system_page_size(void)
2525
{
26-
static uint32_t size = 0;
26+
static size_t size = 0;
2727

2828
if (!size) {
2929
size_t page_size;
3030
if (git__page_size(&page_size) < 0)
3131
page_size = 4096;
3232
/* allow space for malloc overhead */
33-
size = (uint32_t)(page_size - (2 * sizeof(void *)) - sizeof(git_pool_page));
33+
size = (page_size - (2 * sizeof(void *)) - sizeof(git_pool_page));
3434
}
3535

3636
return size;
3737
}
3838

3939
#ifndef GIT_DEBUG_POOL
40-
void git_pool_init(git_pool *pool, uint32_t item_size)
40+
void git_pool_init(git_pool *pool, size_t item_size)
4141
{
4242
assert(pool);
4343
assert(item_size >= 1);
@@ -59,10 +59,10 @@ void git_pool_clear(git_pool *pool)
5959
pool->pages = NULL;
6060
}
6161

62-
static void *pool_alloc_page(git_pool *pool, uint32_t size)
62+
static void *pool_alloc_page(git_pool *pool, size_t size)
6363
{
6464
git_pool_page *page;
65-
const uint32_t new_page_size = (size <= pool->page_size) ? pool->page_size : size;
65+
const size_t new_page_size = (size <= pool->page_size) ? pool->page_size : size;
6666
size_t alloc_size;
6767

6868
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)
7878
return page->data;
7979
}
8080

81-
static void *pool_alloc(git_pool *pool, uint32_t size)
81+
static void *pool_alloc(git_pool *pool, size_t size)
8282
{
8383
git_pool_page *page = pool->pages;
8484
void *ptr = NULL;
@@ -125,7 +125,7 @@ static int git_pool__ptr_cmp(const void * a, const void * b)
125125
}
126126
}
127127

128-
void git_pool_init(git_pool *pool, uint32_t item_size)
128+
void git_pool_init(git_pool *pool, size_t item_size)
129129
{
130130
assert(pool);
131131
assert(item_size >= 1);
@@ -141,7 +141,7 @@ void git_pool_clear(git_pool *pool)
141141
git_vector_free_deep(&pool->allocations);
142142
}
143143

144-
static void *pool_alloc(git_pool *pool, uint32_t size) {
144+
static void *pool_alloc(git_pool *pool, size_t size) {
145145
void *ptr = NULL;
146146
if((ptr = git__malloc(size)) == NULL) {
147147
return NULL;
@@ -169,26 +169,26 @@ void git_pool_swap(git_pool *a, git_pool *b)
169169
memcpy(b, &temp, sizeof(temp));
170170
}
171171

172-
static uint32_t alloc_size(git_pool *pool, uint32_t count)
172+
static size_t alloc_size(git_pool *pool, size_t count)
173173
{
174-
const uint32_t align = sizeof(void *) - 1;
174+
const size_t align = sizeof(void *) - 1;
175175

176176
if (pool->item_size > 1) {
177-
const uint32_t item_size = (pool->item_size + align) & ~align;
177+
const size_t item_size = (pool->item_size + align) & ~align;
178178
return item_size * count;
179179
}
180180

181181
return (count + align) & ~align;
182182
}
183183

184-
void *git_pool_malloc(git_pool *pool, uint32_t items)
184+
void *git_pool_malloc(git_pool *pool, size_t items)
185185
{
186186
return pool_alloc(pool, alloc_size(pool, items));
187187
}
188188

189-
void *git_pool_mallocz(git_pool *pool, uint32_t items)
189+
void *git_pool_mallocz(git_pool *pool, size_t items)
190190
{
191-
const uint32_t size = alloc_size(pool, items);
191+
const size_t size = alloc_size(pool, items);
192192
void *ptr = pool_alloc(pool, size);
193193
if (ptr)
194194
memset(ptr, 0x0, size);
@@ -201,10 +201,10 @@ char *git_pool_strndup(git_pool *pool, const char *str, size_t n)
201201

202202
assert(pool && str && pool->item_size == sizeof(char));
203203

204-
if ((uint32_t)(n + 1) < n)
204+
if (n == SIZE_MAX)
205205
return NULL;
206206

207-
if ((ptr = git_pool_malloc(pool, (uint32_t)(n + 1))) != NULL) {
207+
if ((ptr = git_pool_malloc(pool, (n + 1))) != NULL) {
208208
memcpy(ptr, str, n);
209209
ptr[n] = '\0';
210210
}
@@ -226,14 +226,18 @@ char *git_pool_strdup_safe(git_pool *pool, const char *str)
226226
char *git_pool_strcat(git_pool *pool, const char *a, const char *b)
227227
{
228228
void *ptr;
229-
size_t len_a, len_b;
229+
size_t len_a, len_b, total;
230230

231231
assert(pool && pool->item_size == sizeof(char));
232232

233233
len_a = a ? strlen(a) : 0;
234234
len_b = b ? strlen(b) : 0;
235235

236-
if ((ptr = git_pool_malloc(pool, (uint32_t)(len_a + len_b + 1))) != NULL) {
236+
if (GIT_ADD_SIZET_OVERFLOW(&total, len_a, len_b) ||
237+
GIT_ADD_SIZET_OVERFLOW(&total, total, 1))
238+
return NULL;
239+
240+
if ((ptr = git_pool_malloc(pool, total)) != NULL) {
237241
if (len_a)
238242
memcpy(ptr, a, len_a);
239243
if (len_b)

src/pool.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ typedef struct git_pool_page git_pool_page;
3232
*/
3333
typedef struct {
3434
git_pool_page *pages; /* allocated pages */
35-
uint32_t item_size; /* size of single alloc unit in bytes */
36-
uint32_t page_size; /* size of page in bytes */
35+
size_t item_size; /* size of single alloc unit in bytes */
36+
size_t page_size; /* size of page in bytes */
3737
} git_pool;
3838

3939
#define GIT_POOL_INIT { NULL, 0, 0 }
@@ -57,8 +57,8 @@ typedef struct {
5757
*/
5858
typedef struct {
5959
git_vector allocations;
60-
uint32_t item_size;
61-
uint32_t page_size;
60+
size_t item_size;
61+
size_t page_size;
6262
} git_pool;
6363

6464
#define GIT_POOL_INIT { GIT_VECTOR_INIT, 0, 0 }
@@ -81,7 +81,7 @@ typedef struct {
8181
* Of course, you can use this in other ways, but those are the
8282
* two most common patterns.
8383
*/
84-
extern void git_pool_init(git_pool *pool, uint32_t item_size);
84+
extern void git_pool_init(git_pool *pool, size_t item_size);
8585

8686
/**
8787
* Free all items in pool
@@ -96,8 +96,8 @@ extern void git_pool_swap(git_pool *a, git_pool *b);
9696
/**
9797
* Allocate space for one or more items from a pool.
9898
*/
99-
extern void *git_pool_malloc(git_pool *pool, uint32_t items);
100-
extern void *git_pool_mallocz(git_pool *pool, uint32_t items);
99+
extern void *git_pool_malloc(git_pool *pool, size_t items);
100+
extern void *git_pool_mallocz(git_pool *pool, size_t items);
101101

102102
/**
103103
* Allocate space and duplicate string data into it.

src/sortedcache.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ int git_sortedcache_upsert(void **out, git_sortedcache *sc, const char *key)
282282
itemlen = sc->item_path_offset + keylen + 1;
283283
itemlen = (itemlen + 7) & ~7;
284284

285-
if ((item = git_pool_mallocz(&sc->pool, (uint32_t)itemlen)) == NULL) {
285+
if ((item = git_pool_mallocz(&sc->pool, itemlen)) == NULL) {
286286
/* don't use GIT_ERROR_CHECK_ALLOC b/c of lock */
287287
error = -1;
288288
goto done;

src/tree-cache.c

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -120,15 +120,10 @@ static int read_tree_internal(git_tree_cache **out,
120120

121121
/* Parse children: */
122122
if (tree->children_count > 0) {
123-
size_t i;
124-
uint32_t bufsize;
123+
size_t i, bufsize;
125124

126-
if (tree->children_count > UINT32_MAX / sizeof(git_tree_cache *)) {
127-
git_error_set_oom();
128-
return -1;
129-
}
125+
GIT_ERROR_CHECK_ALLOC_MULTIPLY(&bufsize, tree->children_count, sizeof(git_tree_cache*));
130126

131-
bufsize = (uint32_t)(tree->children_count * sizeof(git_tree_cache *));
132127
tree->children = git_pool_malloc(pool, bufsize);
133128
GIT_ERROR_CHECK_ALLOC(tree->children);
134129

@@ -167,7 +162,7 @@ int git_tree_cache_read(git_tree_cache **tree, const char *buffer, size_t buffer
167162
static int read_tree_recursive(git_tree_cache *cache, const git_tree *tree, git_pool *pool)
168163
{
169164
git_repository *repo;
170-
size_t i, j, nentries, ntrees;
165+
size_t i, j, nentries, ntrees, alloc_size;
171166
int error;
172167

173168
repo = git_tree_owner(tree);
@@ -189,13 +184,10 @@ static int read_tree_recursive(git_tree_cache *cache, const git_tree *tree, git_
189184
ntrees++;
190185
}
191186

192-
if (ntrees > UINT32_MAX / sizeof(git_tree_cache *)) {
193-
git_error_set_oom();
194-
return -1;
195-
}
187+
GIT_ERROR_CHECK_ALLOC_MULTIPLY(&alloc_size, ntrees, sizeof(git_tree_cache *));
196188

197189
cache->children_count = ntrees;
198-
cache->children = git_pool_mallocz(pool, (uint32_t)(ntrees * sizeof(git_tree_cache *)));
190+
cache->children = git_pool_mallocz(pool, alloc_size);
199191
GIT_ERROR_CHECK_ALLOC(cache->children);
200192

201193
j = 0;
@@ -251,12 +243,7 @@ int git_tree_cache_new(git_tree_cache **out, const char *name, git_pool *pool)
251243

252244
GIT_ERROR_CHECK_ALLOC_ADD3(&alloc_size, sizeof(git_tree_cache), name_len, 1);
253245

254-
if (alloc_size > UINT32_MAX) {
255-
git_error_set_oom();
256-
return -1;
257-
}
258-
259-
tree = git_pool_malloc(pool, (uint32_t)alloc_size);
246+
tree = git_pool_malloc(pool, alloc_size);
260247
GIT_ERROR_CHECK_ALLOC(tree);
261248

262249
memset(tree, 0x0, sizeof(git_tree_cache));

0 commit comments

Comments
 (0)