Boost logo

Boost Users :

Subject: [Boost-users] Boost.Regex 1.39.0: Crash in boost::sub_match::str()
From: Peter Klotz (Peter.Klotz_at_[hidden])
Date: 2009-05-15 10:34:03


The program below crashes when compiled and executed in Windows using MSVC8.

The crash happens in boost::sub_match::str(). It seems that the internal structure m_subs gets somehow corrupted.

The crash disappears when passing the const reference fileName instead of it->path().filename(). I have no idea why this should make any difference.

The program expects a directory on the command line. Just put an empty file called "AAA-0.txt" in this directory.

Any help is appreciated.

Regards, Peter.

#include <iostream>
#include <boost/filesystem/path.hpp>
#include <boost/filesystem/convenience.hpp>
#include <boost/regex.hpp>

int main(int argc, char** argv)
{
    try
    {
        if (argc==2)
        {
            const boost::filesystem::path p(argv[1]);
            const boost::regex regEx("^([A-Za-z0-9_]+)-(.*)\\.txt$");

            boost::smatch match;
            for (boost::filesystem::directory_iterator it(p);it!=boost::filesystem::directory_iterator();++it)
            {
                const std::string& fileName=it->path().filename();
                if (boost::regex_match(it->path().filename(),match,regEx)) // <- using fileName makes the crash disappear
                {
                    const std::string projectName=match.str(1); // <- here it crashes
                }
            } // for
            return 0;
        }
    }
    catch (const std::exception& e) {
        std::cout << e.what() << std::endl;
    }
    return 1;
}


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net