提交 a23baf58 编写于 作者: R Rich Felker

fix simple_malloc size restrictions

do not allow allocations that overflow ptrdiff_t; fix some overflow
checks that were not quite right but didn't matter due to address
layout implementation.
上级 96f21974
......@@ -15,16 +15,16 @@ void *__simple_malloc(size_t n)
static int lock;
size_t align=1;
if (n < SIZE_MAX - ALIGN)
while (align<n && align<ALIGN)
align += align;
if (n > SIZE_MAX/2) goto toobig;
while (align<n && align<ALIGN)
align += align;
n = n + align - 1 & -align;
LOCK(&lock);
if (!cur) cur = brk = __brk(0)+16;
if (n > SIZE_MAX - brk) goto fail;
base = cur + align-1 & -align;
if (n > SIZE_MAX - PAGE_SIZE - base) goto fail;
if (base+n > brk) {
new = base+n + PAGE_SIZE-1 & -PAGE_SIZE;
if (__brk(new) != new) goto fail;
......@@ -37,6 +37,7 @@ void *__simple_malloc(size_t n)
fail:
UNLOCK(&lock);
toobig:
errno = ENOMEM;
return 0;
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册