]> git.ipfire.org Git - thirdparty/gcc.git/commit
libstdc++: Simplify std::__throw_bad_variant_access
authorJonathan Wakely <jwakely@redhat.com>
Tue, 22 Oct 2024 15:06:12 +0000 (16:06 +0100)
committerJonathan Wakely <redi@gcc.gnu.org>
Thu, 24 Oct 2024 16:22:33 +0000 (17:22 +0100)
commit0dbc588acaa27a3a56bc9173bd577e1293f10046
treea415cc21c565754dfe60e87df952909f9e8f8183
parentbf43fe6aa966eaf397ea3b8ebd6408d3d124e285
libstdc++: Simplify std::__throw_bad_variant_access

This removes the overload of __throw_bad_variant_access that must be
called with a string literal. This avoids a potential source of
undefined behaviour if that function got misused. The other overload
that takes a bool parameter can be adjusted to take an integer index
selecting one of the four possible string literals to use, ensuring
that the std::bad_variant_access constructor is only called with those
literals.

Passing an index outside the range [0,3] is bogus, but will still select
a valid string literal and avoid undefined behaviour.

libstdc++-v3/ChangeLog:

* include/std/variant (__throw_bad_variant_access(unsigned)):
Define new function as inline friend, with namespace-scope
declaration using noreturn attribute.
(__throw_bad_variant_access(const char*)): Remove.
(__throw_bad_variant_access(bool)): Remove.
(visit, visit<R>): Adjust calls to __throw_bad_variant_access.

Reviewed-by: Patrick Palka <ppalka@redhat.com>
libstdc++-v3/include/std/variant