为无符号整数赋值时出现运行时错误

发布于 2024-10-14 19:46:55 字数 1531 浏览 2 评论 0原文

我刚刚遇到一个非常奇特的问题。当我尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苏别ゝ 2024-10-21 19:46:55

我可以向您保证,分配给 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文