https://gcc.gnu.org/bugzilla/show_bug.cgi?id=123895

--- Comment #3 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to Jonathan Wakely from comment #2)
> When a backref is too large to be represented in an int, we currently throw
> this:
> 
>         if (__builtin_mul_overflow(__v, __radix, &__v)
>             || __builtin_add_overflow(__v, _M_traits.value(__c, __radix),
> &__v))
>             std::__throw_regex_error(regex_constants::error_backref,
>                                      "invalid back reference");

When _CharT is char, we could even append the string _M_value to the exception.

For example:

--- a/libstdc++-v3/include/bits/regex_compiler.tcc
+++ b/libstdc++-v3/include/bits/regex_compiler.tcc
@@ -591,7 +591,8 @@ namespace __detail
        if (__builtin_mul_overflow(__v, __radix, &__v)
            || __builtin_add_overflow(__v, _M_traits.value(__c, __radix),
&__v))
            std::__throw_regex_error(regex_constants::error_backref,
-                                    "invalid back reference");
+                                    ("invalid back reference: " + _M_value
+                                       + " is larger than INT_MAX").c_str());
       return __v;
     }

terminate called after throwing an instance of 'std::regex_error'
  what():  invalid back reference: 999999999999 is larger than INT_MAX


The proper implementation should use if-constexpr to only do this when _CharT
is char and it would be better to add __throw_regex_error_fmt similar to
__throw_out_of_range_fmt so we don't create a string just to get a const char*
which we copy into another string.

Reply via email to