mymalloc
撞了缓冲区的溢出,因为while循环允许
current
走过堆的末端。 4096-Byte跳入
current->size
意味着它正在阅读垃圾,这可能是由于过度付出的。
为什么它发生循环检查
heap.bytes + MEMLENGTH
((char *)current + current->size) <= end
,但是如果current->size
是巨大的(例如4096,来自非初始化的内存),则逐渐远。
堆中没有清晰的“末端”标记,让
current
逃脱。
while (current < end && current->size > 0) {
if (current->allocated == 0 && current->size >= bytesToAlloc) {
if (current->size > bytesToAlloc) {
split_chunk(current, bytesToAlloc);
}
current->allocated = 1;
return (void *)((char *)current + sizeof(header));
}
current = (header *)((char *)current + current->size);
if ((char *)current > (char *)end) break; // Stop if we overshoot
}
检查
确保正确设置了块(例如,一大块initialize_heap()
如果是4096,则崩溃是从读取到末端的读数。