regexec导致malloc错误大小与prev_size损坏

时间:2019-09-23 06:46:05

标签: regex gcc malloc

在多次调用regexec之后,我收到了regexec内部的malloc错误。

标签数组中通常只有一个标签(reg exp),因此在以下代码中,循环中tagidx的值为零。逐步执行代码将显示有效的正则表达式

^\"(.+)\",\"(.+)\"$

并且缓冲区包含一个0终止的char *(字符串)

0x555555756160 <buffer> "\"PrimaryKey\",\"https://example.com/news/story/samplepage\""

因此,此功能可以工作多次,直到最终失败:

大小损坏与prev_size

堆栈跟踪显示了regexec调用的malloc中的错误。

我的环境是Ubuntu 18.04,gcc 7.4.4。

void ProcessTags(FILE *infile, char *buffer, tag *tags)
{
  int tagcnt = sizeof(*tags)/sizeof(tag);
  int tagidx, errno;
  regex_t tagreg;
  regmatch_t matches[MAXMATCHES];

  for (tagidx = 0 ; tagidx < tagcnt ; tagidx++)
  {
    if((errno = regcomp(&tagreg, tags[tagidx].regexp, REG_EXTENDED)))
    {
      regerror(errno, &tagreg, errbuf, sizeof(errbuf));
      fprintf(stderr, "Regular expression problem with tag %s - %s : %s\n", tags[tagidx].tagname, tags[tagidx].regexp, errbuf);
    }

    if (regexec(&tagreg, buffer, MAXMATCHES, matches, 0) == 0) // SIGINT here!!!
    {
      switch (tags[tagidx].action)
      {
...
      }
    }

    regfree(&tagreg);
  }
}

0 个答案:

没有答案