【问题标题】:Boost::Regex Segfault (I think)Boost::Regex Segfault(我认为)
【发布时间】:2016-12-06 15:30:40
【问题描述】:

我正在运行的一些 C++ 代码存在问题。基本上,它适用于大多数输入,但对于某些输入,它会在我的主函数返回后出现段错误。这一直……令人费解。我在段错误处停止运行以获取堆栈跟踪,它返回了这个:

#0 malloc_consolidate() at /build/eglibc-oGUzwX/eglibc-2.19/malloc/malloc.c:4151
#1 _int_free() at /build/eglibc-oGUzwX/eglibc-2.19/malloc/malloc.c:4057
#2 boost::re_detail::mem_block_cache::~mem_block_cache()() at /usr/lib/x86_64-linux-gnu/libboost_regex.so.1.54.0
#3 __cxa_finalize() at /build/eglibc-oGUzwX/eglibc-2.19/stdlib/cxa_finalize.c:56
#4 ??() at /usr/lib/x86_64-linux-gnu/libboost_regex.so.1.54.0
#5 ??() at 
#6 _dl_fini() at /build/eglibc-oGUzwX/eglibc-2.19/elf/dl-fini.c:252

这让我认为我一定是在使用 boost 正则表达式时做错了,但我终其一生都无法弄清楚。我使用正则表达式的方式是用户可以输入一堆字符串。这些字符串可能只是普通文本,也可能是正则表达式。因此,我基本上将所有输入作为正则表达式进行交互。但是,如果用户给出的字符串本来是纯文本,但有一个字符可以被不同地解释为正则表达式怎么办?我所做的是遍历所有纯文本输入字符串并转义所有这些字符。

这是我正在使用的代码。这是我的主要内容:

int
main(int argc, char * argv[])
{

    // Process input arguments
    // The desired input is numVertices (int), graph density (double between 0 and 1), halfLoss (double), total loss (double),
    // position expanse (double, m), velocity expanse (double, m/s)
    int num_vertices;
    double graph_density ;
    double half_loss;
    double total_loss;
    double position_expanse;
    double velocity_expanse;

    if (argc == 1)
    {
        num_vertices = 48;
        graph_density = 1;
        half_loss = 200000;
        total_loss = 400000;
        position_expanse = 400000;
        velocity_expanse = 10000;
    }
    else
    {
        if (argc != 7)
        {
            std::cerr << "Need 6 input arguments" << std::endl;
            return 1;
        }

        std::istringstream ss(argv[1]);
        num_vertices;
        if (!(ss >> num_vertices))
            std::cerr << "First input must be an integer" << std::endl;

        graph_density = read_double_input(argv[2]);
        half_loss = read_double_input(argv[3]);
        total_loss = read_double_input(argv[4]);
        position_expanse = read_double_input(argv[5]);
        velocity_expanse = read_double_input(argv[6]);
    }

    // Determine how many edges to create
    int num_edges = (int) ( (graph_density * num_vertices * (num_vertices - 1)) + 0.5 );

    // Create the edges
    int edges_created = 0;
    std::set<std::pair<int, int> > edge_set;
    while (edge_set.size() < num_edges)
    {

        // Pick a random start vertex and end vertex
        int start_vertex = rand() % num_vertices;
        int end_vertex = rand() % num_vertices;

        // Make sure the start and end vertices are not equal
        while (start_vertex == end_vertex)
        {
            end_vertex = rand() % num_vertices;
        }

        // Insert the new edge into our set of edges
        edge_set.insert(std::pair<int, int>(start_vertex, end_vertex));

    }

    // Create connection handler
    ConnectionHandler conn_handler;

    // Create lists for from and to vertices
    std::vector<std::string> from_list;
    std::vector<std::string> to_list;

    // Add connections to from and to lists
    for (std::set<std::pair<int, int> >::const_iterator edge_it = edge_set.begin(), end_it = edge_set.end(); edge_it != end_it; ++edge_it)
    {
        int start_vertex = edge_it->first;
        int end_vertex = edge_it->second;
        from_list.push_back("Radio" + int_to_string(start_vertex));
        to_list.push_back("Radio" + int_to_string(end_vertex));
    }

    // Read the list into the connection handler
    conn_handler.read_connection_list(true, from_list, to_list);    
    return 0;

}

这段代码有我创建的这个 ConnectionHandler 对象。这是标题:

#ifndef CLCSIM_CONNECTIONHANDLER_HPP_
#define CLCSIM_CONNECTIONHANDLER_HPP_

#include <models/network/NetworkTypes.hpp>
#include <generated/xsd/NetworkModelInterfaceConfig.hpp>

namespace clcsim
{

typedef std::map<std::string, std::set<std::string> > ConnectionFilter;

class ConnectionHandler
{

public:

    ConnectionHandler();
    ~ConnectionHandler();

    void read_connection_list(const bool is_white_list, const std::vector<std::string> &from_radios, const std::vector<std::string> &to_radios);


private:

    ConnectionFilter filter_;
    std::set<std::string> from_list_;
    std::set<std::string> to_list_;
    bool is_white_list_;

};

} // namespace clcsim

#endif // CLCSIM_CONNECTIONHANDLER_HPP_

这是来源:

#include <models/network/ConnectionHandler.hpp>
#include <oasis/framework/exceptions/ConfigurationException.h>
#include <boost/regex.hpp>

namespace clcsim 
{

ConnectionHandler::
ConnectionHandler()
{
}

ConnectionHandler::
~ConnectionHandler()
{
    std::cout << "Destructing conn handler" << std::endl;
}

void 
ConnectionHandler::
read_connection_list(
    const bool is_white_list, 
    const std::vector<std::string> &from_radios, 
    const std::vector<std::string> &to_radios)
{

    std::cout << "Reading the connection list" << std::endl;

    // Make sure the size of both the input vectors are the same
    std::size_t from_radio_size = from_radios.size();
    std::size_t to_radio_size = to_radios.size();
    if (from_radio_size != to_radio_size)
    {
        throw ofs::ConfigurationException("Error while initializing the "
                                          "Network model: "
                                          "Connections in from/to lists don't align"
                                         );
    }

    // Create a regular expression/replacement to find all characters in a non-regular expression
    // that would be interpreted as special characters in a regular expression. Replace them with
    // escape characters
    const boost::regex esc("[.$|()\\[\\]{}*+?\\\\]");
    const std::string rep("\\\\&");

    // Iterate through the specified connections
    for (int i = 0; i < from_radio_size; ++i)
    {

        std::string from_string = boost::regex_replace(from_radios[i], esc, rep, boost::match_default | boost::format_sed);
        std::string to_string = boost::regex_replace(to_radios[i], esc, rep, boost::match_default | boost::format_sed);
        //std::cout << "From " << from_string << " to " << to_string << std::endl;
        filter_[from_string].insert(to_string);
        //filter_[from_radios.at(i)].insert(to_radios.at(i));
    }

    std::cout << "Got here" << std::endl;

}


} // namespace clcsim

抱歉,写了这么多代码。

我看到了一些与 boost::regex 的段错误相关的类似线程。在这些示例中,用户的代码非常简单,只是创建了一个正则表达式并匹配它并遇到错误。原来这个问题与 Boost 版本控制有关。我试着看看我是否可以复制这些错误,但这些简单的例子对我来说效果很好。所以......我很困惑。非常感谢任何帮助!

【问题讨论】:

  • 我不确定您所说的“我在段错误处停止了运行”是什么意思。如果您有段错误,那么 system 将停止运行。如果您在调试器下运行,那么您确实应该能够获得显示故障位置的堆栈跟踪,但是如果 you 在系统停止程序之前停止程序,那么您将无法知道您的堆栈跟踪描述了尚未出现的段错误的位置。
  • 对不起,是的,感谢您的纠正。我在调试器下运行它,它在错误处停止并为我提供了堆栈跟踪。
  • 我在您的堆栈跟踪中看到 eglibc。您的软件堆栈(升压、C++ 编译器、C++ 运行时等)在为 eglibc 构建时是否一致?该 glibc 变体现已停产,如果您混合使用针对不同 C 库构建的软件和库,您可能会遇到麻烦。
  • 最近的 Debian 已经回到 glibc,所以如果有任何 C 库不兼容,那么为此类 Debian 构建的软件可能与 Ubuntu 14.04 不兼容。
  • 提供minimal reproducible example! 怎么样?

标签: c++ regex boost


【解决方案1】:

为了将其从“未答复”列表中删除,我将发布 cmets 中提供的答案,而不是此处。 OP 确定 Boost 链接到 eglibc 的建议确实与链接到 glibc 的其余代码冲突。因此,OP 发现升级他的操作系统以便不再使用 eglibc 链接库可以解决问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-24
    相关资源
    最近更新 更多