1
unsigned char* Data::getAddress(unsigned char* address)
{
    strcpy((char*)address, (char*)this->_address);
    return (unsigned char*)address;
}

int main()
{
    Data d;
    d.makealinkedlisthere();
    while (d)
    {
       unsigned char address[256];
       printf("0x%08x \r\n",d.getAddress(address));
       d = d.getNext();
    }
    return 0;
}

它返回前两个(相同,并且应该不同[可以从调试器中看出] ...)然后崩溃。

它只是制作一个链表。受保护的成员Data* _next......他们的链条。

unsigned char* 来自它返回的数据结构的 Windows 函数VirtualQueryEx部分。MEMORY_BASIC_INFORMATION

this->_address = (unsigned char*)meminfo->BaseAddress; // casted from void*

它是 void*,但我看到它在其他代码中转换为 unsigned char*。在调试器中,我可以看到它表示为十六进制数字。

D1: +    _address   0x7ffd5000 <Bad Ptr>    unsigned char * 
D1->_next:+  _address   0x7f6f0000 "áå•ú`©" unsigned char * 
D1->_next->_next+    _address   0x7ffb0000 " "  unsigned char *
4

1 回答 1

2

MEMORY_BASIC_INFORMATION.Base Address 是页面区域的位置,而不是字符串,并且不能保证它会以空值终止。从 MSDN 站点您可以看到MEMORY_BASIC_INFORMATION 结构

typedef struct _MEMORY_BASIC_INFORMATION {
  PVOID  BaseAddress;
  PVOID  AllocationBase;
  DWORD  AllocationProtect;
  SIZE_T RegionSize;
  DWORD  State;
  DWORD  Protect;
  DWORD  Type;
} MEMORY_BASIC_INFORMATION, *PMEMORY_BASIC_INFORMATION;

要复制数据,您应该使用memcpysize = 255`。

回顾您的代码,只要没有其他问题,将其更改为

PVOID Data::getAddress(PVOID address,size) {    
    memcpy((address, (void *)this->_address, size);
    address[size]=NULL;
    return address;
}
int main() {
    Data d;
    d.makealinkedlisthere();
    while (d) {
       unsigned char address[256];
       printf("Address: 0x%08x \n",d.getAddress((PVOID)address),sizeof(address));
       printf("Data: %s\n",(LPSTR)d.getAddress((PVOID)address),sizeof(address));
       d = d.getNext();
    }
    return 0;
}

应该管用

另请注意,在存储RegionSize之前进行边界检查是安全的memcpy

于 2012-04-15T19:21:58.540 回答