提交 d889cc34 编写于 作者: A Alexander Monakov 提交者: Rich Felker

malloc: fix an over-allocation bug

Fix an instance where realloc code would overallocate by OVERHEAD bytes
amount. Manually arrange for reuse of memcpy-free-return exit sequence.
上级 b9410061
...@@ -414,10 +414,9 @@ void *realloc(void *p, size_t n) ...@@ -414,10 +414,9 @@ void *realloc(void *p, size_t n)
size_t newlen = n + extra; size_t newlen = n + extra;
/* Crash on realloc of freed chunk */ /* Crash on realloc of freed chunk */
if (extra & 1) a_crash(); if (extra & 1) a_crash();
if (newlen < PAGE_SIZE && (new = malloc(n))) { if (newlen < PAGE_SIZE && (new = malloc(n-OVERHEAD))) {
memcpy(new, p, n-OVERHEAD); n0 = n;
free(p); goto copy_free_ret;
return new;
} }
newlen = (newlen + PAGE_SIZE-1) & -PAGE_SIZE; newlen = (newlen + PAGE_SIZE-1) & -PAGE_SIZE;
if (oldlen == newlen) return p; if (oldlen == newlen) return p;
...@@ -460,6 +459,7 @@ copy_realloc: ...@@ -460,6 +459,7 @@ copy_realloc:
/* As a last resort, allocate a new chunk and copy to it. */ /* As a last resort, allocate a new chunk and copy to it. */
new = malloc(n-OVERHEAD); new = malloc(n-OVERHEAD);
if (!new) return 0; if (!new) return 0;
copy_free_ret:
memcpy(new, p, n0-OVERHEAD); memcpy(new, p, n0-OVERHEAD);
free(CHUNK_TO_MEM(self)); free(CHUNK_TO_MEM(self));
return new; return new;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册