File: issue232.cpp

package info (click to toggle)
boost1.90 1.90.0-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 593,120 kB
  • sloc: cpp: 4,190,908; xml: 196,648; python: 34,618; ansic: 23,145; asm: 5,468; sh: 3,774; makefile: 1,161; perl: 1,020; sql: 728; ruby: 676; yacc: 478; java: 77; lisp: 24; csh: 6
file content (71 lines) | stat: -rw-r--r-- 2,420 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <boost/core/lightweight_test.hpp>

#include <boost/regex.hpp>
#include <cstddef>
#include <vector>

template<std::size_t N0, std::size_t N = N0 - 1>
void tester( char const (&str)[ N0 ] )
{
   std::vector<char> s(N, '\0');
   std::memcpy(s.data(), str, N);
   boost::regex rx(s.begin(), s.end());

   std::vector<std::string> wheres;
   wheres.push_back(std::string(15, 'H'));
   wheres.push_back("");
   wheres.push_back("                         ");

   // Perl-style matching
   for (auto const& where : wheres) {
      boost::match_results<std::string::const_iterator> what;
      bool match = boost::regex_match(where, what, rx, boost::match_default | boost::match_partial | boost::match_any | boost::match_perl);
      (void) match;
   }

   // POSIX-style matching
   for (auto const& where : wheres) {
      try {
         boost::match_results<std::string::const_iterator> what;
         bool match = boost::regex_match(where, what, rx, boost::match_default | boost::match_partial | boost::match_any | boost::match_posix);
         (void) match;
      } catch(...) {}
   }

}

int main()
{
   // test strings derived from fuzzing
   // we keep a simple human-readable version
   char const str1[] = "(Y(*COMMIT)|\\K\\D|.)+";
   char const str2[] = "(Y(*COMMIT){||\\K\\D|||||||||\\K|||ss|||||.|\232*(?(50)\027\0204657|H)\020}\031\000.* 6.'?-i)+[L??.\000\000\000\004\000\000\000\000?..<[\000\024R]*+";
   char const str3[] = "(Y(*COMMIT)\xFF\x80|\\L\\K||||||||||.|||||\x84|||||\x00\x00\x10||||||.*  .'?-i)[L??...-i)[L??...[\x00\x14R]*+";
   char const str4[] = "(Y(*COMMIT)\x96||.*  .*  .\\K|||\x9F||||\x9C|.|||||\x84\x99|||\x01\x00\x00\x00|||'?-i#PL\x00\x01.\x86??OMMIT)?...[\x00\x14R]*+";

   tester(str1);
   tester(str2);
   tester(str3);
   tester(str4);

   // prove that we catch certain impossible scenarios

   {
      char const* str = "abcd";
      boost::regex rx(str);
      boost::match_results<std::string::const_iterator> what;
      std::string where(15, 'H');
      BOOST_TEST_THROWS(boost::regex_match(where, rx, boost::match_posix | boost::match_perl), std::logic_error);
   }

   {
      char const* str = "ab(*COMMIT)cd";
      boost::regex rx(str);
      boost::match_results<std::string::const_iterator> what;
      std::string where(15, 'H');
      BOOST_TEST_THROWS(boost::regex_match(where, rx, boost::match_posix), std::logic_error);
   }

   return boost::report_errors();
}