TraceStack的构造和析构不平衡,导致TS_Len异常,从而出现野指针非法修改堆地址。

TS_Len不平衡变成了-1,_TS位于0x2000023c,赋值就变成了向0x20000238赋值,而g_Heap是0x20000228,238正是它的_First字段。故导致_First字段被错误修改。
This commit is contained in:
大石头 2017-03-14 15:56:24 +08:00
parent 8cf4912a49
commit 63b75a307c
3 changed files with 36 additions and 42 deletions

View File

@ -7,7 +7,7 @@
#define MEMORY_ALIGN 4
// 当前堆
Heap* Heap::Current = nullptr;
const Heap* Heap::Current = nullptr;
/*
@ -75,6 +75,7 @@ void* Heap::Alloc(int size)
#if DEBUG
// 检查头部完整性
auto head = (MemoryBlock*)Address;
//assert(_First >= head && head->Used <= Size && (byte*)head + head->Used <= (byte*)head->Next, "堆头被破坏!");
assert(head->Used <= Size && (byte*)head + head->Used <= (byte*)head->Next, "堆头被破坏!");
assert(_Used <= Size, "Heap::Used异常");
#endif

View File

@ -18,7 +18,7 @@ public:
void Free(void* ptr);
// 当前堆
static Heap* Current;
static const Heap* Current;
private:
int _Used;

View File

@ -141,36 +141,29 @@ bool Lock::Wait(int ms)
#if DEBUG
// 使用字符串指针的指针,因为引用的都是字符串常量,不需要拷贝和分配空间
static cstring* _TS = nullptr;
static cstring _TS[16];
static int _TS_Len = 0;
TraceStack::TraceStack(cstring name)
{
// 字符串指针的数组
static cstring __ts[16];
_TS = __ts;
//_TS->Push(name);
if (_TS_Len < 16) _TS[_TS_Len++] = name;
}
TraceStack::~TraceStack()
{
// 清空最后一个项目,避免误判
//if(_TS_Len > 0) _TS[--_TS_Len] = "";
if (_TS_Len > 0)
_TS_Len--;
else
debug_printf("_TS_Len \r\n");
}
void TraceStack::Show()
{
debug_printf("TraceStack::Show:\r\n");
if(_TS)
{
for (int i = _TS_Len - 1; i >= 0; i--)
{
debug_printf("\t<=%s \r\n", _TS[i]);
}
}
}
#endif