bad_backref.pass.cpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. // UNSUPPORTED: libcpp-no-exceptions
  9. // <regex>
  10. // template <class charT, class traits = regex_traits<charT>> class basic_regex;
  11. // template <class ST, class SA>
  12. // basic_regex(const basic_string<charT, ST, SA>& s);
  13. #include <regex>
  14. #include <cassert>
  15. #include "test_macros.h"
  16. static bool error_badbackref_thrown(const char *pat)
  17. {
  18. bool result = false;
  19. try {
  20. std::regex re(pat);
  21. } catch (const std::regex_error &ex) {
  22. result = (ex.code() == std::regex_constants::error_backref);
  23. }
  24. return result;
  25. }
  26. int main(int, char**)
  27. {
  28. assert(error_badbackref_thrown("\\1abc")); // no references
  29. assert(error_badbackref_thrown("ab(c)\\2def")); // only one reference
  30. assert(error_badbackref_thrown("\\800000000000000000000000000000")); // overflows
  31. // this should NOT throw, because we only should look at the '1'
  32. // See https://bugs.llvm.org/show_bug.cgi?id=31387
  33. {
  34. const char *pat1 = "a(b)c\\1234";
  35. std::regex re(pat1, pat1 + 7); // extra chars after the end.
  36. }
  37. return 0;
  38. }