1

Here is the code I'm working with to test regular expressions:

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

int main(int argc, char** argv)
{
    auto isMatch = [](const std::string& text, const std::string& pattern)->bool
    {
        return boost::regex_search(text,
            boost::regex(pattern, boost::regex::icase | boost::regex::nosubs)
        );
    };

    std::cout << std::boolalpha << isMatch("Hello, world!",
        R"(((\%3D)|(=))[^\n]*((\%27)|(\')|(\-\-)|(\%3B)|(;)))") << std::endl;
    return 0;
}

The code compiles fine, but when I run it, sometimes I get an assertion failure on what I can safely assume is a null shared_ptr that isn't supposed to be null. It happens seemingly at random as demonstrated here:

[jacktrueborn@inpost regextest]$ ./regextest
false
[jacktrueborn@inpost regextest]$ ./regextest
regextest: /usr/include/boost/smart_ptr/shared_ptr.hpp:646: typename boost::detail::sp_dereference<T>::type boost::shared_ptr<T>::operator*() const [with T = boost::regex_traits_wrapper<boost::regex_traits<char> >; typename boost::detail::sp_dereference<T>::type = boost::regex_traits_wrapper<boost::regex_traits<char> >&]: Assertion `px != 0' failed.
Aborted (core dumped)
[jacktrueborn@inpost regextest]$ ./regextest
false
[jacktrueborn@inpost regextest]$ ./regextest
regextest: /usr/include/boost/smart_ptr/shared_ptr.hpp:646: typename boost::detail::sp_dereference<T>::type boost::shared_ptr<T>::operator*() const [with T = boost::regex_traits_wrapper<boost::regex_traits<char> >; typename boost::detail::sp_dereference<T>::type = boost::regex_traits_wrapper<boost::regex_traits<char> >&]: Assertion `px != 0' failed.
Aborted (core dumped)
[jacktrueborn@inpost regextest]$ ./regextest
regextest: /usr/include/boost/smart_ptr/shared_ptr.hpp:646: typename boost::detail::sp_dereference<T>::type boost::shared_ptr<T>::operator*() const [with T = boost::regex_traits_wrapper<boost::regex_traits<char> >; typename boost::detail::sp_dereference<T>::type = boost::regex_traits_wrapper<boost::regex_traits<char> >&]: Assertion `px != 0' failed.
Aborted (core dumped)
[jacktrueborn@inpost regextest]$ ./regextest
regextest: /usr/include/boost/smart_ptr/shared_ptr.hpp:646: typename boost::detail::sp_dereference<T>::type boost::shared_ptr<T>::operator*() const [with T = boost::regex_traits_wrapper<boost::regex_traits<char> >; typename boost::detail::sp_dereference<T>::type = boost::regex_traits_wrapper<boost::regex_traits<char> >&]: Assertion `px != 0' failed.
Aborted (core dumped)
[jacktrueborn@inpost regextest]$ ./regextest
regextest: /usr/include/boost/smart_ptr/shared_ptr.hpp:646: typename boost::detail::sp_dereference<T>::type boost::shared_ptr<T>::operator*() const [with T = boost::regex_traits_wrapper<boost::regex_traits<char> >; typename boost::detail::sp_dereference<T>::type = boost::regex_traits_wrapper<boost::regex_traits<char> >&]: Assertion `px != 0' failed.
Aborted (core dumped)
[jacktrueborn@inpost regextest]$ ./regextest
regextest: /usr/include/boost/smart_ptr/shared_ptr.hpp:646: typename boost::detail::sp_dereference<T>::type boost::shared_ptr<T>::operator*() const [with T = boost::regex_traits_wrapper<boost::regex_traits<char> >; typename boost::detail::sp_dereference<T>::type = boost::regex_traits_wrapper<boost::regex_traits<char> >&]: Assertion `px != 0' failed.
Aborted (core dumped)
[jacktrueborn@inpost regextest]$ ./regextest
false
[jacktrueborn@inpost regextest]$ ./regextest
false
[jacktrueborn@inpost regextest]$ ./regextest
regextest: /usr/include/boost/smart_ptr/shared_ptr.hpp:646: typename boost::detail::sp_dereference<T>::type boost::shared_ptr<T>::operator*() const [with T = boost::regex_traits_wrapper<boost::regex_traits<char> >; typename boost::detail::sp_dereference<T>::type = boost::regex_traits_wrapper<boost::regex_traits<char> >&]: Assertion `px != 0' failed.
Aborted (core dumped)
[jacktrueborn@inpost regextest]$ ./regextest
false
[jacktrueborn@inpost regextest]$ ./regextest
false
[jacktrueborn@inpost regextest]$ ./regextest
regextest: /usr/include/boost/smart_ptr/shared_ptr.hpp:646: typename boost::detail::sp_dereference<T>::type boost::shared_ptr<T>::operator*() const [with T = boost::regex_traits_wrapper<boost::regex_traits<char> >; typename boost::detail::sp_dereference<T>::type = boost::regex_traits_wrapper<boost::regex_traits<char> >&]: Assertion `px != 0' failed.
Aborted (core dumped)
[jacktrueborn@inpost regextest]$ ./regextest
false
[jacktrueborn@inpost regextest]$ ./regextest
regextest: /usr/include/boost/smart_ptr/shared_ptr.hpp:646: typename boost::detail::sp_dereference<T>::type boost::shared_ptr<T>::operator*() const [with T = boost::regex_traits_wrapper<boost::regex_traits<char> >; typename boost::detail::sp_dereference<T>::type = boost::regex_traits_wrapper<boost::regex_traits<char> >&]: Assertion `px != 0' failed.
Aborted (core dumped)
[jacktrueborn@inpost regextest]$ ./regextest
regextest: /usr/include/boost/smart_ptr/shared_ptr.hpp:646: typename boost::detail::sp_dereference<T>::type boost::shared_ptr<T>::operator*() const [with T = boost::regex_traits_wrapper<boost::regex_traits<char> >; typename boost::detail::sp_dereference<T>::type = boost::regex_traits_wrapper<boost::regex_traits<char> >&]: Assertion `px != 0' failed.
Aborted (core dumped)
[jacktrueborn@inpost regextest]$ ./regextest
false
[jacktrueborn@inpost regextest]$

And this is how I installed boost:

./bootstrap.sh --prefix=/usr
sudo ./b2 install

I know this has been a problem in the past with this library and I haven't been able to find a satisfactory work-around; I'm wondering if one has surfaced and I just haven't seen it. However, I'm not sure if I'm even having the same problem given the flaky nature of the error. std::regex simply throws std::regex_errors while running so I can't use that.

(I'm aware that isMatch doesn't need to be a lambda or even a real function in this instance, but in my non-contrived code, there is a reason for doing it this way.)

jvstech
  • 844
  • 1
  • 9
  • 28
  • 1
    Do you have different versions of boost installed? That seems to be the problem, [see this question](http://stackoverflow.com/questions/5545689/boost-regex-runtime-error) which is the same problem. – Jesse Good Jun 26 '13 at 07:46
  • 1
    Have you tried using `valgrind`? Don't bother using `std::regex`, [it's not implemented in libstdc++](http://stackoverflow.com/a/12665408/981959) – Jonathan Wakely Jun 26 '13 at 13:03
  • @JesseGood: +1. That was the issue, indeed, but the question you posted didn't give me a fix. Fortunately, it was a fairly easy (if somewhat scary) fix. – jvstech Jun 27 '13 at 01:04

1 Answers1

0

The issue was, as pointed out, two different versions of Boost being installed. However, the steps for how to fix the problem were not addressed in the suggested question. Here is what I had to do:

  1. Remove the system packaged Boost library (I don't know if this step was actually required because of the following two steps I had to perform):

    sudo yum remove boost-devel

  2. Remove all the leftover Boost libs:

    sudo find /usr -name 'libboost_*' -delete

  3. Remove all the leftover Boost includes including the Boost folder itself:

    sudo rm -rf /usr/include/boost

  4. Reinstall Boost 1.53 (from above):

    ./bootstrap.sh --prefix=/usr

    sudo ./b2 install

In my case, I also had to update LD_LIBRARY_PATH to point to /usr/lib as the variable didn't even exist before.

At any rate, the code works now. Every time. No runtime errors at all.

jvstech
  • 844
  • 1
  • 9
  • 28