Skip to content

Instantly share code, notes, and snippets.

@directionless
Created February 16, 2019 06:49
Show Gist options
  • Save directionless/c38eb7dc216bcd93b40c3ecaf99f326e to your computer and use it in GitHub Desktop.
Save directionless/c38eb7dc216bcd93b40c3ecaf99f326e to your computer and use it in GitHub Desktop.
boost::algorithm::split_regex hangs and consumes CPU on dangling alternatives
#include <stdlib.h>
#include <iostream>
#include <boost/regex.hpp>
#include <boost/algorithm/string/regex.hpp>
int main() {
// Works
{
std::cout << "Using ;|," << std::endl;
const char *input = "foo;bar,qux";
std::vector<std::string> result;
boost::regex pattern(";|,", boost::regbase::normal);
boost::algorithm::split_regex(result, input, pattern);
for( int i = 0; i < result.size(); i = i + 1) {
std::cout << "value at " << i << ": " << result[i] << std::endl;
}
std::cout << std::endl;
}
// Consumes CPU due to unbounded |
{
std::cout << "Using ;|" << std::endl;
const char *input = "foo;bar,qux";
std::vector<std::string> result;
boost::regex pattern(";|", boost::regbase::normal);
boost::algorithm::split_regex(result, input, pattern);
for( int i = 0; i < result.size(); i = i + 1) {
std::cout << "value at " << i << ": " << result[i] << std::endl;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment