【问题标题】:Double free() occurrence where it should not在不应该出现的地方出现两次 free()
【发布时间】:2015-07-28 05:57:37
【问题描述】:

我有一个令人沮丧的问题,我找不到答案。

我有这个功能:

// Append character to the end of a string
void str_AppendChar(char* s, const char* ch)
{
  // +2 because of 2x '\0'
  char* buff = malloc(strlen(s)+strlen(ch)+2);
  memset(buff, 0, sizeof(buff));

  // Copy the whole string in buff
  strcpy(buff, s);

  // Append ch at the end of buff
  int len = strlen(buff);
  strcpy(buff+len, ch);

  // Set end of the string
  *(buff+strlen(buff)-3) = '\0';

  strcpy(s, buff);

  free(buff);
}

由于某种原因,我的程序最后尝试执行两次 free 。

我使用 AppendChar() 的代码是:(有点难看但请忍耐)

void it_GatherCmd(cmd_Details* lineptr[], char* cmd)
{
  // Used to count number of rows in lineptr
  int nlines;

  Detailptr p;
  char ch;
  char* word = (char*)malloc(sizeof(char)+256);
  memset(word, 0, sizeof(word));

  nlines = 0;
  while ((ch = *cmd++) != '\n')
  {
      if (ch != ' ' && ch != '\0' )
          str_AppendChar(word, &ch);
      else
      {
          int type = dict_CheckWord(word);

          if (type != -1)
          {
              p = it_CopyInfo(word, type);
              lineptr[nlines++] = p;
          }
          memset(word, 0, sizeof(word));
      }
   }
   //EDIT*
   free(word);
}

还有我的主要:

int main()
{
   cmd_Details* arrCmd[MAXLINES];
   char* str = "just some string";

   it_GatherCmd(arrCmd, str);

   printf("%s", str);
   return 0;
}

AppendChar() 在我创建它_GetCharCmd() 并在那里使用它之前一直没有问题。我在这上面花了大约 3 个小时,但我找不到问题所在。在互联网上进行了一些搜索,但我发现的内容与我的问题并不完全相关。

【问题讨论】:

  • 我很确定 memset 在这两个函数中的调用不会像你期望的那样工作; sizeof(buf) 这里将是sizeof(char*),而不是数组的长度。
  • 对于strcpy(s, buff);,我认为不会有问题,因为要使用的数据很可能永远不会到达sizeof(char)+256。但我会记住稍后对其进行优化。 memset 用于清除分配的内存中的数据,以便以后可以重用。工作完成后内存被释放。
  • 整个过程非常无效。当您所做的只是将一个字符附加到一个单词时,为什么还要在本地分配和释放内存?我认为(除了将所需的数据存储在命令结构中),您根本不需要动态分配。例如,最好使用char buf[256] 在 steck 上创建一个 256 字节的缓冲区作为自动内存,然后您就完成了。
  • 不一定是具体问题的原因,但在str_AppendChar()中,第二个参数被视为字符串(与strlen()strcpy()一起使用),而它实际上只是指向一个字符,而不是以空字符结尾的字符序列。所以它会尝试访问字符后面的随机内存,直到它碰巧找到的下一个 null。
  • 这里strcpy(buff+len, ch); 一个简单的strcat(buff, ch); 就可以了。

标签: c pointers char free double-free


【解决方案1】:

这段代码有一些问题。

首先,如果str_AppendChar 确实如其名称所暗示的那样附加了一个字符,那么为什么要给它一个暗示C 字符串的const char*?在这里传递指针而不是实际对象的增益为零,就像某些结构的情况一样;实际上,您仍然需要将 4 个字节压入堆栈。

其次,正如我在 cmets 中指出的那样,问题在于您没有正确初始化分配的缓冲区 - sizeof(buff) 返回良好,buff 的大小和 buff 是 char* 这很可能4. 虽然只是将sizeof(buff) 更改为strlen(s)+strlen(ch)+2,这是您实际分配的内存量解决了问题(并且由于sizeof(buff) 可能超过了您实际分配的内存量,您正在写超出该内存),我建议像这样简化函数:

// Append character to the end of a string
void str_AppendChar(char* s, char ch)
{
    size_t sLen = strlen(s);
    char* buff = (char*)malloc(sLen + 2);       // 1 for the appended char, 1 for \0
    //memset(buff, 0, sLen + 2);   //not necessary, we'll overwrite the memory anyway

    // Copy the whole string in buff
    strcpy(buff, s);

    // append our char and null-terminate
    buff[sLen] = ch;
    buff[sLen + 1] = '\0';

    strcpy(s, buff);

    free(buff);
}

请注意,这段代码仍然很糟糕;它很高兴地假设 s 将大到足以容纳一个额外的字符,但情况并非总是如此。

还有关于你的 it_gatherCmd 函数;它应该采用const char*,因为它不会以任何方式修改它(实际上,你调用它的方式是给它一个 const char*;修改字符串文字是未定义的行为,在 Windows 上你'd 可能由于违反页面权限而崩溃)。

【讨论】:

  • 不用memset
  • @KarolyHorvath 没错,Windows 会为你归零,但我觉得这样做更安全。
  • @szczurcio:这不是原因:你会在设置为零后立即覆盖它,所以没有必要。
  • @MOehm 是的,你是对的。在答案中更改了它。
  • 感谢您的宝贵时间。确实我做的不对,你的版本可以。从现在开始我会更加小心。
【解决方案2】:

据我所知,您在扫描命令时连续构建了一个字符串。附加字符时,绝对不需要复制要附加到两次的字符串。您实际上在做的是:

void str_AppendChar(char* s, char ch)
{
    int len = strlen(buff);

    s[len++] = ch;
    s[loen] = '\0';
}

请注意,您每次都使用strlen 确定字符串长度,这将遍历整个字符串。更糟糕的是,您没有关于最大可用缓冲区大小的任何信息,因此尽管您进行了所有分配、复制和释放,但原始字符串可能会溢出。

代替

str_AppendChar(word, &ch);

在自动内存中使用本地缓冲区:

char word[20];
int wlen = 0;

并像这样追加:

if (wlen + 1 < sizeof(word)) word[wlen++] = *cmd;

这将使单词缓冲区未终止,因此在使用它之前,附加它:

word[wlen] = '\0';
printf("next word: '%s'\n", word);

(当然,您也可以确保字符串始终以空值结尾。)

当您为新单词重置缓冲区时,无需memset 整个缓冲区;只需将wlen 重置为零即可。

【讨论】:

  • 我明白了。谢谢你的提示!
猜你喜欢
  • 2015-09-27
  • 2011-09-23
  • 1970-01-01
  • 2014-04-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多