为无符号整数赋值时出现运行时错误
我刚刚遇到一个非常奇特的问题。当我尝试使用 memcpy 或 = 运算符将值分配给特定的 unsigned int 变量(其他 unsigned int 变量没有问题)时,程序会因运行时错误而崩溃。它并不总是在一个程序运行时发生,但当我运行第二个实例时它总是发生。我通过注释不同的内容彻底检查了代码,所以我很确定值分配是问题所在。
非常感谢。
DWORD WINAPI RecvFunc(void* lpParameter)
{
BYTE header[5];
short size, datasize; // must be unsigned, changed to signed for testing
int num;
BYTE* data;
BYTE opcode;
while(true)
{
size = 0;
while(size < 5)
{
num = recv(sock, (char*)(header + size), 5 - size, 0);
if(num <= 0)
{
size = 0;
break;
}
//MessageBox(g_hwnd, "Header piece ok", "", MB_OK);
size += num;
}
if(size == 0)
{
//MessageBox(g_hwnd, "Header error", "", MB_OK);
continue;
}
opcode = header[0];
memcpy(&datasize, header + 1, sizeof(datasize));
if(datasize > 5)
{
data = new BYTE[datasize - 5];
}else{
data = NULL;
}
while(size < datasize)
{
num = recv(sock, (char*)(data + size - 5), datasize - size, 0);
//MessageBox(g_hwnd, "Packet post-recv", "", MB_OK);
if(num <= 0)
{
size = 0;
break;
}
size += num;
}
if(size == 0)
{
//MessageBox(g_hwnd, "Packet error", "", MB_OK);
delete[] data;
continue;
}
size -= 5;
//MessageBox(g_hwnd, "Received a command", "New cmd", MB_OK);
switch(opcode)
{
...
}
if(data != NULL)
{
delete[] data;
}
}
}
I've just come across a very peculiar problem. When I try to assign a value to a particular unsigned int variable (no problem with other unsigned int variables), either with memcpy or the = operator, the program crashes with a runtime error. It does not always happen with one program running, but it always does when I run a second instance. I thoroughly checked the code by commenting different things out, so I'm pretty certain that the value assignment is the problem.
Many thanks in advance.
DWORD WINAPI RecvFunc(void* lpParameter)
{
BYTE header[5];
short size, datasize; // must be unsigned, changed to signed for testing
int num;
BYTE* data;
BYTE opcode;
while(true)
{
size = 0;
while(size < 5)
{
num = recv(sock, (char*)(header + size), 5 - size, 0);
if(num <= 0)
{
size = 0;
break;
}
//MessageBox(g_hwnd, "Header piece ok", "", MB_OK);
size += num;
}
if(size == 0)
{
//MessageBox(g_hwnd, "Header error", "", MB_OK);
continue;
}
opcode = header[0];
memcpy(&datasize, header + 1, sizeof(datasize));
if(datasize > 5)
{
data = new BYTE[datasize - 5];
}else{
data = NULL;
}
while(size < datasize)
{
num = recv(sock, (char*)(data + size - 5), datasize - size, 0);
//MessageBox(g_hwnd, "Packet post-recv", "", MB_OK);
if(num <= 0)
{
size = 0;
break;
}
size += num;
}
if(size == 0)
{
//MessageBox(g_hwnd, "Packet error", "", MB_OK);
delete[] data;
continue;
}
size -= 5;
//MessageBox(g_hwnd, "Received a command", "New cmd", MB_OK);
switch(opcode)
{
...
}
if(data != NULL)
{
delete[] data;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我可以向您保证,分配给 unsigned int 变量并不是问题的真正原因:) 最有可能的是,您的程序中其他地方存在缓冲区溢出。
Assigning to an unsigned int variable is not the real cause of your problem, I can assure you :) Most likely, you have a buffer overflow somewhere else in your program.