gpt4 book ai didi

c++ - g++正则表达式在(可能是非语法的)表达式上崩溃

转载 作者:太空宇宙 更新时间:2023-11-04 11:41:20 25 4
gpt4 key购买 nike

我认为下面的程序要么提示它不能编译正则表达式,要么将其视为合法的并编译正常(我没有标准所以我不能确定表达式是否严格合法;当然合理的解释是可能的)。无论如何,g++ (Ubuntu/Linaro 4.8.1-10ubuntu9) 4.8.1 发生的事情是,在运行时,它会严重崩溃

*** Error in `./a.out': free(): invalid next size (fast): 0x08b51248 ***

在图书馆的内部。

问题是:

a) 这是错误,对吧?我假设(可能是错误的)标准没有说 std::regex 如果不​​喜欢语法就会崩溃。 (msvc 吃得很好,fwiw)

b) 如果它是一个错误,是否有一些简单的方法可以查看它是否已被报告(我第一次在 gnu-land 错误系统中探索是令人生畏的)?

#include <iostream>
#include <regex>

int main(void)
{
const char* Pattern = "^(%%)|";
std::regex Machine;

try {
Machine = Pattern;
}
catch(std::regex_error e)
{
std::cerr << "regex could not compile pattern: "
<< Pattern << "\n"
<< e.what() << std::endl;
throw;
}

return 0;
}

最佳答案

我会把它放在评论中,但我不能,所以...

不知道大家知不知道,好像是管道|最后的字符导致你的问题。好像是 | 的字符表示作为最后一个字符(因为 "^(%%%)|a"对我来说很好用)当正则表达式试图调用 free() 时,g++ 会弄得一团糟;

该标准(或者至少是我正在阅读的在线草案)声称:

28.8
Class template basic_regex
[re.regex]

1 For a char-like type charT, specializations of class template basic_regex represent regular expressions
constructed from character sequences of charT characters. In the rest of 28.8, charT denotes a given char-
like type. Storage for a regular expression is allocated and freed as necessary by the member functions of
class basic_regex.

2 Objects of type specialization of basic_regex are responsible for converting the sequence of charT objects
to an internal representation. It is not specified what form this representation takes, nor how it is accessed by
algorithms that operate on regular expressions.
[ Note: Implementations will typically declare some function
templates as friends of basic_regex to achieve this — end note ]

及以后,

basic_regex& operator=(const charT* ptr);

3 Requires: ptr shall not be a null pointer.

4 Effects: returns assign(ptr).

所以除非 g++ 认为 const char* Pattern ="|";是一个 null ptr(我想不会……),我想这是一个错误?

编辑:顺便说一下,连续的 || (即使不是最后)似乎也会对我造成段错误。

关于c++ - g++正则表达式在(可能是非语法的)表达式上崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21324291/

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