Commit 50dd4b89 authored by Nikolay Sivov's avatar Nikolay Sivov Committed by Alexandre Julliard

msvcrt: Handle overflow in calloc().

parent f3465ecb
......@@ -394,7 +394,15 @@ size_t CDECL _aligned_msize(void *p, MSVCRT_size_t alignment, MSVCRT_size_t offs
*/
void* CDECL MSVCRT_calloc(MSVCRT_size_t count, MSVCRT_size_t size)
{
return msvcrt_heap_alloc(HEAP_ZERO_MEMORY, count*size);
MSVCRT_size_t bytes = count*size;
if (size && bytes / size != count)
{
*MSVCRT__errno() = MSVCRT_ENOMEM;
return NULL;
}
return msvcrt_heap_alloc(HEAP_ZERO_MEMORY, bytes);
}
/*********************************************************************
......
......@@ -456,6 +456,28 @@ static void test_sbheap(void)
free(mem);
}
static void test_calloc(void)
{
void *ptr;
ptr = calloc(1, 0);
ok(ptr != NULL, "got %p\n", ptr);
free(ptr);
ptr = calloc(0, 0);
ok(ptr != NULL, "got %p\n", ptr);
free(ptr);
ptr = calloc(0, 1);
ok(ptr != NULL, "got %p\n", ptr);
free(ptr);
errno = 0;
ptr = calloc(~(size_t)0 / 2, ~(size_t)0 / 2);
ok(ptr == NULL, "got %p\n", ptr);
ok(errno == ENOMEM || broken(errno == 0) /* winxp, win2k3 */, "got errno %d\n", errno);
}
START_TEST(heap)
{
void *mem;
......@@ -480,4 +502,5 @@ START_TEST(heap)
test_aligned();
test_sbheap();
test_calloc();
}
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