aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJonathan Wakely <jwakely@redhat.com>2024-08-21 12:29:32 +0100
committerJonathan Wakely <redi@gcc.gnu.org>2024-08-23 13:18:20 +0100
commita35dd276cbf6236e08bcf6e56e62c2be41cf6e3c (patch)
tree80d2faa9ee52dc53e49ae174312438608a493403
parent9115593d8517d210ffe7da2c8ec786342381e471 (diff)
downloadgcc-a35dd276cbf6236e08bcf6e56e62c2be41cf6e3c.zip
gcc-a35dd276cbf6236e08bcf6e56e62c2be41cf6e3c.tar.gz
gcc-a35dd276cbf6236e08bcf6e56e62c2be41cf6e3c.tar.bz2
libstdc++: Make debug sequence members mutable [PR116369]
We need to be able to attach debug mode iterators to const containers, so the safe iterator constructor uses const_cast to get a modifiable pointer to the container. If the container was defined as const, that const_cast to access its members results in undefined behaviour. PR 116369 shows a case where it results in a segfault because the container is in a rodata section (which shouldn't have happened, but the undefined behaviour in the library still exists in any case). This makes the _M_iterators and _M_const_iterators data members mutable, so that it's safe to modify them even if the declared type of the container is a const type. Ideally we would not need the const_cast at all. Instead, the _M_attach member (and everything it calls) should be const-qualified. That would work fine now, because the members that it ends up modifying are mutable. Making that change would require a number of new exports from the shared library, and would require retaining the old non-const member functions (maybe as symbol aliases) for backwards compatibility. That might be worth changing at some point, but isn't done here. libstdc++-v3/ChangeLog: PR c++/116369 * include/debug/safe_base.h (_Safe_sequence_base::_M_iterators): Add mutable specifier. (_Safe_sequence_base::_M_const_iterators): Likewise.
-rw-r--r--libstdc++-v3/include/debug/safe_base.h4
1 files changed, 2 insertions, 2 deletions
diff --git a/libstdc++-v3/include/debug/safe_base.h b/libstdc++-v3/include/debug/safe_base.h
index d5fbe4b..88d7f0b 100644
--- a/libstdc++-v3/include/debug/safe_base.h
+++ b/libstdc++-v3/include/debug/safe_base.h
@@ -205,10 +205,10 @@ namespace __gnu_debug
public:
/// The list of mutable iterators that reference this container
- _Safe_iterator_base* _M_iterators;
+ mutable _Safe_iterator_base* _M_iterators;
/// The list of constant iterators that reference this container
- _Safe_iterator_base* _M_const_iterators;
+ mutable _Safe_iterator_base* _M_const_iterators;
/// The container version number. This number may never be 0.
mutable unsigned int _M_version;