On 25/07/2015 21:45, Roger Leigh wrote:

OK, some further investigation has shown what the exact error is.  It
looks like a GCC bug.  Please see the attached source file testcase.
This regex is failing:

   std::regex("^[a-z0-9][a-z0-9-]*$", std::regex::extended);

however this one works:

   std::regex("^[a-z0-9][-a-z0-9]*$", std::regex::extended);

In the same vein, the attached sample using basic rather than extended expressions fails in the opposite way. In this case both compile but the latter expression fails to match correctly. Since the expression should be valid and behave the same in both cases, it looks like there are two bugs here, the first being unable to compile a valid extended regex, the second here being unable to match (which is likely also a compile failure, but not a fatal one).


Regards,
Roger

#include <cassert>
#include <regex>
#include <iostream>
#include <stdexcept>

int main()
{
  try
    {
      // These three are OK:
      static std::regex lanana_namespace("^[a-z0-9]+$");
      static std::regex lsb_namespace("^_?([a-z0-9_.]+-)+[a-z0-9]+$");
      static std::regex debian_dpkg_conffile_cruft("dpkg-(old|dist|new|tmp)$");
    }
  catch(const std::regex_error& e)
    {
      std::cout <<"E0: " << e.what() << std::endl;
    }

  try
    {
      // This fails with regex_error:
      static std::regex debian_cron_namespace("^[a-z0-9][a-z0-9-]*$");
      assert(std::regex_match("test", debian_cron_namespace));
      assert(!std::regex_match("-a", debian_cron_namespace));
      assert(std::regex_match("a-", debian_cron_namespace));
    }
  catch(const std::regex_error& e)
    {
      std::cout <<"E1: " << e.what() << std::endl;
    }

  try
    {
      // This modified version works by moving the hyphen to the start of
      // the square bracket, even though the meaning is the same:
      static std::regex debian_cron_namespace_ok("^[a-z0-9][-a-z0-9]*$");
      assert(std::regex_match("test", debian_cron_namespace_ok));
      assert(!std::regex_match("-a", debian_cron_namespace_ok));
      assert(std::regex_match("a-", debian_cron_namespace_ok));
    }
  catch(const std::regex_error& e)
    {
      std::cout <<"E2: " << e.what() << std::endl;
    }
}

Reply via email to