2014-01-24 21 views
1

我想下面的程序應該抱怨它不能編譯正則表達式,或者把它當作合法的並且編譯它很好(我沒有標準,所以我可以'不要說這個表達是嚴格合法的,當然可以有合理的解釋)。不管怎麼說,有g++ (Ubuntu/Linaro 4.8.1-10ubuntu9) 4.8.1情況是,在運行時,它在圖書館的膽量崩潰硬g ++正則表達式碰撞(可能是非句法)表達式

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

問題是:

a)它的錯誤,對吧?我假設(也許不正確)標準沒有說std :: regex可能崩潰,如果它不喜歡的語法。 (msvc吃得很好,fwiw)

b)如果它是一個bug,是否有一些簡單的方法來查看它是否已被報告(我第一次探討gnu-land bug系統是否嚇人)?

#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; 
    } 
+0

我會說你在正則表達式解析器中有一個錯誤,因爲它應該做的是匹配任何東西。管道字符指示正則表達式解析器匹配其任一側的表達式。因爲在你的情況下,右側是空的,它應該匹配任何東西。我想你會匹配管道字符,因此應該逃避它,它應該工作。 – ClasG

+0

順便說一句,這是正則表達式只是一個測試,或者你想要做什麼? – ClasG

+2

''在gcc-4.8.1的libstdC++中不起作用。如果您可以使用LLVM的libC++,請使用[您的程序運行](http://coliru.stacked-crooked.com/a/e061305dca0676ba)。否則 - boost.regex – Cubbi

回答

1

我會把這一個評論,但我做不到,所以...

我不知道,如果你已經知道了,但它似乎是管道|字符在最後導致你的問題。這似乎是|的字符表示作爲最後一個字符(因爲「^(%%)| a」適用於我)正如g ++所給出的,當正則表達式試圖調用free();

標準(或者至少是網上草案我讀)聲稱:

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 *模式= 「|」 ;是一個空ptr(我想不會...), 我想這是一個錯誤?

編輯:順便說一下,連續|| (即使不在最後)也會對我造成分段錯誤。

+0

這可能是一個很好的答案,因爲我會得到;它至少增強了我對正則表達式語法錯誤未被分類爲「未定義行爲」的信心。 –

+0

所以我也不能評論其他人的評論,但關於您對@Cubbi的評論,我昨天在通過將羣集安裝到沒有root訪問權的$ HOME/gcc目錄的gcc-4.8.2上進行了反擊,所以也許如果你仍然想要這樣做,我可以幫忙嗎? – chrisb2244