diff options
author | Jonathan Wakely <jwakely@redhat.com> | 2021-04-20 16:16:13 +0100 |
---|---|---|
committer | Jonathan Wakely <jwakely@redhat.com> | 2022-05-09 17:37:21 +0100 |
commit | b4a170bf3a2b0397a5d73f7cac24f6df4298569e (patch) | |
tree | 344350e8f326dd540453858b1b0f4b8847e34453 /libstdc++-v3 | |
parent | a5ae767c18ba7d78d11248604c5468e47b80f09b (diff) | |
download | gcc-b4a170bf3a2b0397a5d73f7cac24f6df4298569e.tar.gz |
libstdc++: Do not allocate a zero-size vector<bool> [PR 100153]
The vector<bool>::shrink_to_fit() implementation will allocate new
storage even if the vector is empty. That then leads to the
end-of-storage pointer being non-null and equal to the _M_start._M_p
pointer, which means that _M_end_addr() has undefined behaviour.
The fix is to stop doing a useless zero-sized allocation in
shrink_to_fit(), so that _M_start._M_p and _M_end_of_storage are both
null after an empty vector shrinks.
Signed-off-by: Jonathan Wakely <jwakely@redhat.com>
libstdc++-v3/ChangeLog:
PR libstdc++/100153
* include/bits/vector.tcc (vector<bool>::_M_shrink_to_fit()):
When size() is zero just deallocate and reset.
(cherry picked from commit 681707ec28d56494fa61a80c62500724d55f8586)
Diffstat (limited to 'libstdc++-v3')
-rw-r--r-- | libstdc++-v3/include/bits/vector.tcc | 8 |
1 files changed, 7 insertions, 1 deletions
diff --git a/libstdc++-v3/include/bits/vector.tcc b/libstdc++-v3/include/bits/vector.tcc index 497d9f72247..7d56c63134a 100644 --- a/libstdc++-v3/include/bits/vector.tcc +++ b/libstdc++-v3/include/bits/vector.tcc @@ -944,7 +944,13 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER return false; __try { - _M_reallocate(size()); + if (size_type __n = size()) + _M_reallocate(__n); + else + { + this->_M_deallocate(); + this->_M_impl._M_reset(); + } return true; } __catch(...) |