Regex error using MSVC 2013

◇◆丶佛笑我妖孽 提交于 2019-12-14 00:40:28

问题


I have a piece of xml like code to parse, using std::regex in MSVC 2013

<GLVertex>
#version 450 core
layout(location = 0) in vec3 pos;
in VertexInfo{
    vec2 uv;
}vertexInfo;
void main(){
    gl_Position = vec4(pos, 1.0);
    vertexInfo.uv = pos.xy;
}
<GLVertex/>
<GLFragment>
#version 450 core
layout(location = 0) uniform sampler2D g_map;
uniform Color {
    vec4 color;
};
layout(location = 0) out vec4 fragColor;
void main(){
    fragColor = texture(g_map, vertexInfo.uv);
}
<GLFragment/>

Here is the pattern:

<GLVertex>((.|\n)+)<GLVertex\/>

But the program always crash! Is there any bug in my regex? I've tested on regex101.

PS. when i delete the 5th line:

vec2 uv;

it works OK!


回答1:


You get a Stack overflow (parameters: 0x00000001, 0x00312FFC) exception as the pattern is not efficient. I think it is related to how std::regex processes repeated groups (you have define one with a +-quantifier group (.|\n)+). This pattern matches each and every character that is not a newline (.), or a newline (\n), and then stores the match inside a buffer. Then, an issue with the iterator debugging happens only in Debug mode. The std::_Orphan_Me is the place where the break occurs, and it is considered the most "expensive" method when matching strings. See performance killer -Debug Iterator Support in Visual studio

You should either switch to the Release mode, or test with a regex that does not require the use of the repeated groups, like any non-null character [^\x00] with lazy quantifier *?:

std::string str1 = "<GLVertex>\n#version 450 core\nlayout(location = 0) in vec3 pos;\nin VertexInfo{\n    vec2 uv;\n}vertexInfo;\nvoid main(){\n    gl_Position = vec4(pos, 1.0);\n    vertexInfo.uv = pos.xy;\n}\n<GLVertex/>\n<GLFragment>\n#version 450 core\nlayout(location = 0) uniform sampler2D g_map;\nuniform Color {\n    vec4 color;\n};\nlayout(location = 0) out vec4 fragColor;\nvoid main(){\n    fragColor = texture(g_map, vertexInfo.uv);\n}\n<GLFragment/>"; 
std::regex reg1("<GLVertex>([^\\x00]*?)<GLVertex/>");
std::smatch find1;
if (std::regex_search(str1, find1, reg1)){
    std::cout << find1[1].str();
}



来源:https://stackoverflow.com/questions/36686979/regex-error-using-msvc-2013

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!