mirror of
https://git.proxmox.com/git/libgit2
synced 2025-08-12 12:00:29 +00:00
pool: Correct overflow checks
Ok, scrap the previous commit. This is the right overflow check that takes care of 64 bit overflow **and** 32-bit overflow, which needs to be considered because the pool malloc can only allocate 32-bit elements in one go.
This commit is contained in:
parent
ce33645ff3
commit
437f7d69b2
@ -194,6 +194,9 @@ 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)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
if ((ptr = git_pool_malloc(pool, (uint32_t)(n + 1))) != NULL) {
|
if ((ptr = git_pool_malloc(pool, (uint32_t)(n + 1))) != NULL) {
|
||||||
memcpy(ptr, str, n);
|
memcpy(ptr, str, n);
|
||||||
ptr[n] = '\0';
|
ptr[n] = '\0';
|
||||||
|
@ -139,7 +139,11 @@ void test_core_pool__strndup_limit(void)
|
|||||||
git_pool p;
|
git_pool p;
|
||||||
|
|
||||||
cl_git_pass(git_pool_init(&p, 1, 100));
|
cl_git_pass(git_pool_init(&p, 1, 100));
|
||||||
cl_assert(git_pool_strndup(&p, "foo", -1) == NULL);
|
/* ensure 64 bit doesn't overflow */
|
||||||
|
cl_assert(git_pool_strndup(&p, "foo", (size_t)-1) == NULL);
|
||||||
|
|
||||||
|
/* ensure 32 bit doesn't overflow */
|
||||||
|
cl_assert(git_pool_strndup(&p, "bar", 0xfffffffful + 32) == NULL);
|
||||||
git_pool_clear(&p);
|
git_pool_clear(&p);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user