gpt4 book ai didi

c - 我该如何解决 munmap_chunk() : invalid pointer on regfree(®ex)

转载 作者:行者123 更新时间:2023-11-30 18:33:39 29 4
gpt4 key购买 nike

你好,我收到 munmap_chunk(): invalid pointerregfree(&regexCompiled);regexCompiled 已分配。

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

regex1

之后的步骤:

regex1

这是函数代码:

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;
}

最佳答案

使用char * ret = (char * ) malloc(sizeof(char));,您只分配一个个字符。

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

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

分配多个字符。

关于c - 我该如何解决 munmap_chunk() : invalid pointer on regfree(&regex),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55674175/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com