首页 > 解决方案 > 我该如何解决 munmap_chunk(): regfree(®ex) 上的无效指针

问题描述

您好,我munmap_chunk(): invalid pointer 正在 regfree(&regexCompiled); 分配 regexCompiled。

问题是只有在找到匹配时才会发生这种情况,否则它可以正常工作

正则表达式1

之后:

正则表达式1

这是函数代码:

char * WebServer::get_db_query(char * line) {
  const char * regex = "<sql\\s+db=(.+?)\\s+query=(.+;)\\s*\\\\>";
  size_t maxGroups = 3;
  char * ret = (char * ) malloc(sizeof(char));
  regex_t regexCompiled;
  regmatch_t groupArray[maxGroups];

  if (regcomp( & regexCompiled, regex, REG_EXTENDED)) {
    printf("Could not compile regular expression.\n");
    fflush(stdout);
    return NULL;
  };

  if (!regexec( & regexCompiled, line, maxGroups, groupArray, 0)) {
    char copy[strlen(line) + 1];
    strcpy(copy, line);
    copy[groupArray[1].rm_eo] = '\0';
    copy[groupArray[2].rm_eo] = '\0';
    matched = true;
    sprintf(ret, "%s;%s", copy+ groupArray[1].rm_so, copy+ groupArray[2].rm_so);
    printf("Match %s\n",
      copy+ groupArray[1].rm_so);
    printf("Match %s\n",
      copy+ groupArray[2].rm_so);
    fflush(stdout);
  }

  regfree( & regexCompiled);
  return ret;
}

标签: cregex

解决方案


char * ret = (char * ) malloc(sizeof(char));你只分配一个字符。

稍后您执行此操作时,sprintf(ret, "%s;%s", ..您会远远超出这个字符,因此您会破坏内存。

然后在以后的任何时候,当堆检测到它已损坏时,您可能会遇到问题。

分配多个字符。


推荐阅读