]> gcc.gnu.org Git - gcc.git/commitdiff
libstdc++: Add valid range assertions to std::basic_regex [PR89927]
authorJonathan Wakely <jwakely@redhat.com>
Mon, 11 Oct 2021 11:08:59 +0000 (12:08 +0100)
committerJonathan Wakely <jwakely@redhat.com>
Mon, 11 Oct 2021 19:34:16 +0000 (20:34 +0100)
This adds some debug assertions to basic_regex. They don't actually
diagnose the error in the PR yet, but I have another patch to make them
more effective.

Also change the __glibcxx_assert(false) consistency checks to include a
string literal that tells the user a bit more about why the process
aborted. We could consider adding a __glibcxx_bug or
__glibcxx_internal_error macro for this purpose, but ideally we'll never
hit such bugs anyway so it shouldn't be needed.

libstdc++-v3/ChangeLog:

PR libstdc++/89927
* include/bits/regex.h (basic_regex(const _Ch_type*, size_t)):
Add __glibcxx_requires_string_len assertion.
(basic_regex::assign(InputIterator, InputIterator)): Add
__glibcxx_requires_valid_range assertion.
* include/bits/regex_scanner.tcc (_Scanner::_M_advance())
(_Scanner::_M_scan_normal()): Use string literal in assertions.

libstdc++-v3/include/bits/regex.h
libstdc++-v3/include/bits/regex_scanner.tcc

index bf02bff7c4921bd66a94a8174f37e45bddbf2d76..3c44bcd7e33f86843040beaaddcf9e7d251011a2 100644 (file)
@@ -467,7 +467,10 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
        */
       basic_regex(const _Ch_type* __p, std::size_t __len,
                  flag_type __f = ECMAScript)
-      { _M_compile(__p, __p + __len, __f); }
+      {
+       __glibcxx_requires_string_len(__p, __len);
+       _M_compile(__p, __p + __len, __f);
+      }
 
       /**
        * @brief Copy-constructs a basic regular expression.
@@ -684,6 +687,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
          if constexpr (__detail::__is_contiguous_iter<_InputIterator>::value
                        && is_same_v<_ValT, value_type>)
            {
+             __glibcxx_requires_valid_range(__first, __last);
              const auto __len = __last - __first;
              const _Ch_type* __p = std::__to_address(__first);
              _M_compile(__p, __p + __len, __flags);
index d81627dc3e9dee705e94b98cb6dfba8c05b0c7cc..2fa2303f27bf14c7f465c039f03e8b94d20cc17a 100644 (file)
@@ -83,7 +83,7 @@ namespace __detail
        _M_scan_in_brace();
       else
        {
-         __glibcxx_assert(false);
+         __glibcxx_assert(!"unexpected state while processing regex");
        }
     }
 
@@ -195,7 +195,7 @@ namespace __detail
                _M_token = __it->second;
                return;
              }
-         __glibcxx_assert(false);
+         __glibcxx_assert(!"unexpected special character in regex");
        }
       else
        {
This page took 0.064651 seconds and 5 git commands to generate.