c - 我该如何解决 munmap_chunk(): regfree(®ex) 上的无效指针
问题描述
您好,我munmap_chunk(): invalid pointer
正在
regfree(®exCompiled);
分配 regexCompiled。
问题是只有在找到匹配时才会发生这种情况,否则它可以正常工作
之后:
这是函数代码:
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", ..
您会远远超出这个字符,因此您会破坏内存。
然后在以后的任何时候,当堆检测到它已损坏时,您可能会遇到问题。
分配多个字符。
推荐阅读
- sed - sed 在两个括号之间的倒数第二行插入行
- node.js - 将数据导出到 csv 时出现“发送到客户端后无法设置标题”
- php - 如何在foreach循环中获得唯一值
- html - CSS 边框:Firefox/Chrome 的区别
- ansible - 在 Ansible 中从多个变量源导入用户变量
- python-3.x - 虽然找到当前和上一个得到错误“列表”对象的两个熊猫元素的最大值,但没有属性“最大值”
- docker - 运行类似于 Kubernetes 仪表盘的网页
- sql - 用于 abc 分析的 Postgresql for 循环
- excel - 查找存在空白单元格的第 2 和第 3 最常见文本字符串的函数
- unity3d - 将 Unity 应用程序部署到 Hololens 模拟器时出现“错误 DEP6957 无法使用通用身份验证连接到设备”