Bug 61059 - regex throws regex_error for valid extended regular expression
Summary: regex throws regex_error for valid extended regular expression
Status: RESOLVED INVALID
Alias: None
Product: gcc
Classification: Unclassified
Component: libstdc++ (show other bugs)
Version: 4.9.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks: std::regex
  Show dependency treegraph
 
Reported: 2014-05-04 19:43 UTC by Roger Leigh
Modified: 2023-07-20 11:29 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments
Preprocessed source for testcase (186.43 KB, application/x-xz)
2014-05-04 19:43 UTC, Roger Leigh
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Roger Leigh 2014-05-04 19:43:51 UTC
Created attachment 32732 [details]
Preprocessed source for testcase

Source:

--------[regextest.cpp]---------------------------------------------------------
#include <regex>

int main()
{
  std::regex description_keys("^description\\[.*\\]$", std::regex::extended);
}
--------------------------------------------------------------------------------

This RE has been used by code using boost::regex for around a decade.  The failure is caused by escaping the "[" and "]" regex metacharacters with a backslash "\" to match the literal character, so nothing too complex really.  I'm fairly sure this is completely correct.  This compiles with GCC 4.9, but fails at runtime in the regex constructor, shown below:


% gdb regextest 
GNU gdb (GDB) 7.6.2 (Debian 7.6.2-1)
This GDB was configured as "x86_64-linux-gnu".

(gdb) run
Starting program: /tmp/regextest 
warning: no loadable sections found in added symbol-file system-supplied DSO at 0x7ffff7ffd000
warning: Could not load shared library symbols for linux-vdso.so.1.
Do you need "set solib-search-path" or "set sysroot"?
Traceback (most recent call last):
  File "/usr/lib/debug/usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.20-gdb.py", line 59, in <module>
    from libstdcxx.v6.printers import register_libstdcxx_printers
ImportError: No module named libstdcxx.v6.printers
terminate called after throwing an instance of 'std::regex_error'
  what():  regex_error

Program received signal SIGABRT, Aborted.
0x00000033248353a9 in __GI_raise (sig=sig@entry=6) at ../nptl/sysdeps/unix/sysv/linux/raise.c:56
56      ../nptl/sysdeps/unix/sysv/linux/raise.c: No such file or directory.
(gdb) bt
#0  0x00000033248353a9 in __GI_raise (sig=sig@entry=6) at ../nptl/sysdeps/unix/sysv/linux/raise.c:56
#1  0x00000033248384c8 in __GI_abort () at abort.c:89
#2  0x00007ffff7d1fd55 in __gnu_cxx::__verbose_terminate_handler () at ../../../../src/libstdc++-v3/libsupc++/vterminate.cc:95
#3  0x00007ffff7d1ddd6 in __cxxabiv1::__terminate (handler=<optimized out>) at ../../../../src/libstdc++-v3/libsupc++/eh_terminate.cc:47
#4  0x00007ffff7d1de21 in std::terminate () at ../../../../src/libstdc++-v3/libsupc++/eh_terminate.cc:57
#5  0x00007ffff7d1e038 in __cxxabiv1::__cxa_throw (obj=0x62b550, tinfo=0x7ffff7fb13d0 <typeinfo for std::regex_error>, dest=
    0x7ffff7d74f70 <std::regex_error::~regex_error()>) at ../../../../src/libstdc++-v3/libsupc++/eh_throw.cc:87
#6  0x00007ffff7d74325 in std::__throw_regex_error (__ecode=std::regex_constants::_S_error_escape) at ../../../../../src/libstdc++-v3/src/c++11/functexcept.cc:141
#7  0x0000000000404f56 in std::__detail::_Scanner<char>::_M_eat_escape_posix (this=0x7fffffffe5b8) at /usr/include/c++/4.9/bits/regex_scanner.tcc:395
#8  0x0000000000405c59 in std::__detail::_Scanner<char>::_M_scan_normal (this=0x7fffffffe5b8) at /usr/include/c++/4.9/bits/regex_scanner.tcc:110
#9  0x0000000000404fc3 in std::__detail::_Scanner<char>::_M_advance (this=0x7fffffffe5b8) at /usr/include/c++/4.9/bits/regex_scanner.tcc:80
#10 0x000000000040426a in std::__detail::_Compiler<std::regex_traits<char> >::_M_match_token (this=0x7fffffffe5a0, token=std::__detail::_ScannerBase::_S_token_closure0)
    at /usr/include/c++/4.9/bits/regex_compiler.tcc:487
#11 0x00000000004080a5 in std::__detail::_Compiler<std::regex_traits<char> >::_M_quantifier (this=0x7fffffffe5a0) at /usr/include/c++/4.9/bits/regex_compiler.tcc:187
#12 0x0000000000406b85 in std::__detail::_Compiler<std::regex_traits<char> >::_M_term (this=0x7fffffffe5a0) at /usr/include/c++/4.9/bits/regex_compiler.tcc:138
#13 0x000000000040522e in std::__detail::_Compiler<std::regex_traits<char> >::_M_alternative (this=0x7fffffffe5a0) at /usr/include/c++/4.9/bits/regex_compiler.tcc:118
#14 0x0000000000405251 in std::__detail::_Compiler<std::regex_traits<char> >::_M_alternative (this=0x7fffffffe5a0) at /usr/include/c++/4.9/bits/regex_compiler.tcc:121
#15 0x0000000000405251 in std::__detail::_Compiler<std::regex_traits<char> >::_M_alternative (this=0x7fffffffe5a0) at /usr/include/c++/4.9/bits/regex_compiler.tcc:121
#16 0x0000000000405251 in std::__detail::_Compiler<std::regex_traits<char> >::_M_alternative (this=0x7fffffffe5a0) at /usr/include/c++/4.9/bits/regex_compiler.tcc:121
#17 0x0000000000405251 in std::__detail::_Compiler<std::regex_traits<char> >::_M_alternative (this=0x7fffffffe5a0) at /usr/include/c++/4.9/bits/regex_compiler.tcc:121
#18 0x0000000000405251 in std::__detail::_Compiler<std::regex_traits<char> >::_M_alternative (this=0x7fffffffe5a0) at /usr/include/c++/4.9/bits/regex_compiler.tcc:121
#19 0x0000000000405251 in std::__detail::_Compiler<std::regex_traits<char> >::_M_alternative (this=0x7fffffffe5a0) at /usr/include/c++/4.9/bits/regex_compiler.tcc:121
#20 0x0000000000405251 in std::__detail::_Compiler<std::regex_traits<char> >::_M_alternative (this=0x7fffffffe5a0) at /usr/include/c++/4.9/bits/regex_compiler.tcc:121
#21 0x0000000000405251 in std::__detail::_Compiler<std::regex_traits<char> >::_M_alternative (this=0x7fffffffe5a0) at /usr/include/c++/4.9/bits/regex_compiler.tcc:121
#22 0x0000000000405251 in std::__detail::_Compiler<std::regex_traits<char> >::_M_alternative (this=0x7fffffffe5a0) at /usr/include/c++/4.9/bits/regex_compiler.tcc:121
#23 0x0000000000405251 in std::__detail::_Compiler<std::regex_traits<char> >::_M_alternative (this=0x7fffffffe5a0) at /usr/include/c++/4.9/bits/regex_compiler.tcc:121
#24 0x0000000000405251 in std::__detail::_Compiler<std::regex_traits<char> >::_M_alternative (this=0x7fffffffe5a0) at /usr/include/c++/4.9/bits/regex_compiler.tcc:121
#25 0x0000000000405251 in std::__detail::_Compiler<std::regex_traits<char> >::_M_alternative (this=0x7fffffffe5a0) at /usr/include/c++/4.9/bits/regex_compiler.tcc:121
#26 0x0000000000405251 in std::__detail::_Compiler<std::regex_traits<char> >::_M_alternative (this=0x7fffffffe5a0) at /usr/include/c++/4.9/bits/regex_compiler.tcc:121
#27 0x0000000000404122 in std::__detail::_Compiler<std::regex_traits<char> >::_M_disjunction (this=0x7fffffffe5a0) at /usr/include/c++/4.9/bits/regex_compiler.tcc:97
#28 0x000000000040391c in std::__detail::_Compiler<std::regex_traits<char> >::_Compiler (this=0x7fffffffe5a0, __b=0x62b028 "^description\\[.*\\]$", __e=0x62b03b "", 
    __traits=..., __flags=std::regex_constants::extended) at /usr/include/c++/4.9/bits/regex_compiler.tcc:82
#29 0x00000000004034e4 in std::__detail::__compile_nfa<std::regex_traits<char> > (__first=0x62b028 "^description\\[.*\\]$", __last=0x62b03b "", __traits=..., 
    __flags=std::regex_constants::extended) at /usr/include/c++/4.9/bits/regex_compiler.h:155
#30 0x0000000000403257 in std::basic_regex<char, std::regex_traits<char> >::basic_regex<char const*> (this=0x7fffffffe800, __first=0x41bc6e "^description\\[.*\\]$", 
    __last=0x41bc81 "", __f=std::regex_constants::extended) at /usr/include/c++/4.9/bits/regex.h:528
#31 0x0000000000402fdf in std::basic_regex<char, std::regex_traits<char> >::basic_regex (this=0x7fffffffe800, __p=0x41bc6e "^description\\[.*\\]$", 
    __f=std::regex_constants::extended) at /usr/include/c++/4.9/bits/regex.h:452
#32 0x0000000000402588 in main () at regextest.cpp:5
Comment 1 Jonathan Wakely 2014-05-04 21:16:38 UTC
Reduced:

#include <regex>

int main()
{
  std::regex description_keys("\\]", std::regex::extended);
}

POSIX says this is undefined:
http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap09.html#tag_09_04_02

The closing brace is not a POSIX ERE special character so should not be escaped.

As a GNU extension we accept it with -std=gnu++11
Comment 2 Jonathan Wakely 2014-05-04 21:17:35 UTC
(Unsetting fields I accidentally filled in on the wrong browser tab)
Comment 3 Roger Leigh 2014-05-05 18:27:38 UTC
Thanks for the clarification.

Just for reference for anyone else running into this behaviour:

clang++3.3: compiles and runs
VS2012: compiles and throws regex_error
VS2013: compiles and throws regex_error