2

I am working in Codes::blocks 17.12 and installed boost 1.66.0 using GNU.

I am using boost and boost regex for first time, so, to start with i copied and pasted code from this site and tried compiling it, but failed.

the error which it is showing is following:

required by substitution of 'template boost::shared_ptr::shared_ptr(const boost::shared_ptr&, typename boost::detail::sp_enable_if_convertible::type) [with Y = const boost::re_detail_106600::cpp_regex_traits_implementation]'|

however, i even tried various other examples, listed below:

i even wrote some of them by myself
In short, in all the samples i had tried, i got exactly the same error.

How do remove this error and why in the first place it is there ?

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Sanmveg saini
  • 744
  • 1
  • 7
  • 22

1 Answers1

0

I think there is a bug in the implementation when you define BOOST_REGEX_MATCH_EXTRA. For me, it does compile (with both boost 1.64 and 1.66) however, it fails at runtime with UBSAN diagnostics (meaning there is undefined behaviour). The diagnostics are, e.g.:

/home/sehe/custom/boost_1_66_0/boost/smart_ptr/detail/shared_count.hpp:426:36: runtime error: member call on misaligned address 0x000000000001 for type 'struct sp_counted_base', which requires 8 byte alignment
0x000000000001: note: pointer points here
<memory cannot be printed>
/home/sehe/custom/boost_1_66_0/boost/smart_ptr/detail/shared_count.hpp:426:36: runtime error: member access within misaligned address 0x000000000001 for type 'struct sp_counted_base', which requires 8 byte alignment
0x000000000001: note: pointer points here
<memory cannot be printed>
ASAN:DEADLYSIGNAL
=================================================================
==24391==ERROR: AddressSanitizer: SEGV on unknown address 0x000000000001 (pc 0x000000406f73 bp 0x7ffe19c54a80 sp 0x7ffe19c54a60 T0)
==24391==The signal is caused by a READ memory access.
==24391==Hint: address points to the zero page.
    #0 0x406f72 in boost::detail::shared_count::~shared_count() /home/sehe/custom/boost_1_66_0/boost/smart_ptr/detail/shared_count.hpp:426
    #1 0x407800 in boost::shared_ptr<boost::re_detail_106600::named_subexpressions>::~shared_ptr() /home/sehe/custom/boost_1_66_0/boost/smart_ptr/shared_ptr.hpp:341
    #2 0x407a26 in boost::match_results<__gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > >::~match_results() /home/sehe/custom/boost_1_66_0/boost/regex/v4/match_results.hpp:110
    #3 0x404c17 in print_captures(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) /home/sehe/Projects/stackoverflow/test.cpp:7
    #4 0x404f81 in main /home/sehe/Projects/stackoverflow/test.cpp:37
    #5 0x7f70051e482f in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2082f)
    #6 0x404718 in _start (/home/sehe/Projects/stackoverflow/sotest+0x404718)

AddressSanitizer can not provide additional info.
SUMMARY: AddressSanitizer: SEGV /home/sehe/custom/boost_1_66_0/boost/smart_ptr/detail/shared_count.hpp:426 in boost::detail::shared_count::~shared_count()
==24391==ABORTING

In fact the compiler define BOOST_REGEX_MATCH_EXTRA is only required for the extra capture information (the first linked sample). If you don't need that, just compile without the define, and all the other stuff should work.

Live On Coliru

#include <boost/regex.hpp>
#include <iostream>

void print_captures(const std::string &regx, const std::string &text) {
    boost::regex e(regx);
    boost::smatch what;
    std::cout << "Expression: \"" << regx << "\"\n";
    std::cout << "Text:       \"" << text << "\"\n";

    if (boost::regex_match(text, what, e, boost::match_extra)) {
        std::cout << "** Match found **\n   Sub-Expressions:\n";
        for (unsigned i = 0; i < what.size(); ++i)
            std::cout << "      $" << i << " = \"" << what[i] << "\"\n";

#ifdef BOOST_REGEX_MATCH_EXTRA
        std::cout << "   Captures:\n";
        for (unsigned i = 0; i < what.size(); ++i) {
            std::cout << "      $" << i << " = {";
            for (unsigned j = 0; j < what.captures(i).size(); ++j) {
                if (j)
                    std::cout << ", ";
                else
                    std::cout << " ";
                std::cout << "\"" << what.captures(i)[j] << "\"";
            }
            std::cout << " }\n";
        }
#endif
    } else {
        std::cout << "** No Match found **\n";
    }
}

int main(int, char *[]) {
    print_captures(R"((([[:lower:]]+)|([[:upper:]]+))+)", "aBBcccDDDDDeeeeeeee");
    print_captures(R"((.*)bar|(.*)bah)", "abcbar");
    print_captures(R"((.*)bar|(.*)bah)", "abcbah");
    print_captures(R"(^(?:(\w+)|(?>\W+))*$)", "now is the time for all good men to come to the aid of the party");
}

Prints

Expression: "(([[:lower:]]+)|([[:upper:]]+))+"
Text:       "aBBcccDDDDDeeeeeeee"
** Match found **
   Sub-Expressions:
      $0 = "aBBcccDDDDDeeeeeeee"
      $1 = "eeeeeeee"
      $2 = "eeeeeeee"
      $3 = "DDDDD"
Expression: "(.*)bar|(.*)bah"
Text:       "abcbar"
** Match found **
   Sub-Expressions:
      $0 = "abcbar"
      $1 = "abc"
      $2 = ""
Expression: "(.*)bar|(.*)bah"
Text:       "abcbah"
** Match found **
   Sub-Expressions:
      $0 = "abcbah"
      $1 = ""
      $2 = "abc"
Expression: "^(?:(\w+)|(?>\W+))*$"
Text:       "now is the time for all good men to come to the aid of the party"
** Match found **
   Sub-Expressions:
      $0 = "now is the time for all good men to come to the aid of the party"
      $1 = "party"
sehe
  • 374,641
  • 47
  • 450
  • 633