|
|
999efc |
commit 632183ddcc8f3aead8b4fc63c4ab59a42ef9ad00
|
|
|
999efc |
Author: Jonathan Wakely <jwakely@redhat.com>
|
|
|
999efc |
Date: Wed Jun 17 22:49:06 2020 +0100
|
|
|
999efc |
|
|
|
999efc |
libstdc++: Avoid stack overflow in std::vector (PR 94540)
|
|
|
999efc |
|
|
|
999efc |
The std::__uninitialized_default_n algorithm used by std::vector creates
|
|
|
999efc |
an initial object as a local variable then copies that into the
|
|
|
999efc |
destination range. If the object is too large for the stack this
|
|
|
999efc |
crashes. We should create the first object directly into the
|
|
|
999efc |
destination and then copy it from there.
|
|
|
999efc |
|
|
|
999efc |
This doesn't fix the bug for C++98, because in that case the initial
|
|
|
999efc |
value is created as a default argument of the vector constructor i.e. in
|
|
|
999efc |
the user's code, not inside libstdc++. We can't prevent that.
|
|
|
999efc |
|
|
|
999efc |
PR libstdc++/94540
|
|
|
999efc |
* include/bits/stl_uninitialized.h (__uninitialized_default_1<true>):
|
|
|
999efc |
Construct the first value at *__first instead of on the stack.
|
|
|
999efc |
(__uninitialized_default_n_1<true>): Likewise.
|
|
|
999efc |
Improve comments on several of the non-standard algorithms.
|
|
|
999efc |
* testsuite/20_util/specialized_algorithms/uninitialized_default/94540.cc:
|
|
|
999efc |
New test.
|
|
|
999efc |
* testsuite/20_util/specialized_algorithms/uninitialized_default_n/94540.cc:
|
|
|
999efc |
New test.
|
|
|
999efc |
* testsuite/20_util/specialized_algorithms/uninitialized_value_construct/94540.cc:
|
|
|
999efc |
New test.
|
|
|
999efc |
* testsuite/20_util/specialized_algorithms/uninitialized_value_construct_n/94540.cc:
|
|
|
999efc |
New test.
|
|
|
999efc |
* testsuite/23_containers/vector/cons/94540.cc: New test.
|
|
|
999efc |
|
|
|
999efc |
--- libstdc++-v3/include/bits/stl_uninitialized.h
|
|
|
999efc |
+++ libstdc++-v3/include/bits/stl_uninitialized.h
|
|
|
999efc |
@@ -556,7 +556,14 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
|
|
|
999efc |
typedef typename iterator_traits<_ForwardIterator>::value_type
|
|
|
999efc |
_ValueType;
|
|
|
999efc |
|
|
|
999efc |
- std::fill(__first, __last, _ValueType());
|
|
|
999efc |
+ if (__first == __last)
|
|
|
999efc |
+ return;
|
|
|
999efc |
+
|
|
|
999efc |
+ typename iterator_traits<_ForwardIterator>::value_type* __val
|
|
|
999efc |
+ = std::__addressof(*__first);
|
|
|
999efc |
+ std::_Construct(__val);
|
|
|
999efc |
+ if (++__first != __last)
|
|
|
999efc |
+ std::fill(__first, __last, *__val);
|
|
|
999efc |
}
|
|
|
999efc |
};
|
|
|
999efc |
|
|
|
999efc |
@@ -589,16 +596,20 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
|
|
|
999efc |
static _ForwardIterator
|
|
|
999efc |
__uninit_default_n(_ForwardIterator __first, _Size __n)
|
|
|
999efc |
{
|
|
|
999efc |
- typedef typename iterator_traits<_ForwardIterator>::value_type
|
|
|
999efc |
- _ValueType;
|
|
|
999efc |
-
|
|
|
999efc |
- return std::fill_n(__first, __n, _ValueType());
|
|
|
999efc |
+ if (__n > 0)
|
|
|
999efc |
+ {
|
|
|
999efc |
+ typename iterator_traits<_ForwardIterator>::value_type* __val
|
|
|
999efc |
+ = std::__addressof(*__first);
|
|
|
999efc |
+ std::_Construct(__val);
|
|
|
999efc |
+ ++__first;
|
|
|
999efc |
+ __first = std::fill_n(__first, __n - 1, *__val);
|
|
|
999efc |
+ }
|
|
|
999efc |
+ return __first;
|
|
|
999efc |
}
|
|
|
999efc |
};
|
|
|
999efc |
|
|
|
999efc |
// __uninitialized_default
|
|
|
999efc |
- // Fills [first, last) with std::distance(first, last) default
|
|
|
999efc |
- // constructed value_types(s).
|
|
|
999efc |
+ // Fills [first, last) with value-initialized value_types.
|
|
|
999efc |
template<typename _ForwardIterator>
|
|
|
999efc |
inline void
|
|
|
999efc |
__uninitialized_default(_ForwardIterator __first,
|
|
|
999efc |
@@ -615,7 +626,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
|
|
|
999efc |
}
|
|
|
999efc |
|
|
|
999efc |
// __uninitialized_default_n
|
|
|
999efc |
- // Fills [first, first + n) with n default constructed value_type(s).
|
|
|
999efc |
+ // Fills [first, first + n) with value-initialized value_types.
|
|
|
999efc |
template<typename _ForwardIterator, typename _Size>
|
|
|
999efc |
inline _ForwardIterator
|
|
|
999efc |
__uninitialized_default_n(_ForwardIterator __first, _Size __n)
|
|
|
999efc |
@@ -633,8 +644,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
|
|
|
999efc |
|
|
|
999efc |
|
|
|
999efc |
// __uninitialized_default_a
|
|
|
999efc |
- // Fills [first, last) with std::distance(first, last) default
|
|
|
999efc |
- // constructed value_types(s), constructed with the allocator alloc.
|
|
|
999efc |
+ // Fills [first, last) with value_types constructed by the allocator
|
|
|
999efc |
+ // alloc, with no arguments passed to the construct call.
|
|
|
999efc |
template<typename _ForwardIterator, typename _Allocator>
|
|
|
999efc |
void
|
|
|
999efc |
__uninitialized_default_a(_ForwardIterator __first,
|
|
|
999efc |
@@ -664,8 +675,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
|
|
|
999efc |
|
|
|
999efc |
|
|
|
999efc |
// __uninitialized_default_n_a
|
|
|
999efc |
- // Fills [first, first + n) with n default constructed value_types(s),
|
|
|
999efc |
- // constructed with the allocator alloc.
|
|
|
999efc |
+ // Fills [first, first + n) with value_types constructed by the allocator
|
|
|
999efc |
+ // alloc, with no arguments passed to the construct call.
|
|
|
999efc |
template<typename _ForwardIterator, typename _Size, typename _Allocator>
|
|
|
999efc |
_ForwardIterator
|
|
|
999efc |
__uninitialized_default_n_a(_ForwardIterator __first, _Size __n,
|
|
|
999efc |
@@ -686,6 +697,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
|
|
|
999efc |
}
|
|
|
999efc |
}
|
|
|
999efc |
|
|
|
999efc |
+ // __uninitialized_default_n_a specialization for std::allocator,
|
|
|
999efc |
+ // which ignores the allocator and value-initializes the elements.
|
|
|
999efc |
template<typename _ForwardIterator, typename _Size, typename _Tp>
|
|
|
999efc |
inline _ForwardIterator
|
|
|
999efc |
__uninitialized_default_n_a(_ForwardIterator __first, _Size __n,
|
|
|
999efc |
@@ -757,8 +770,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
|
|
|
999efc |
};
|
|
|
999efc |
|
|
|
999efc |
// __uninitialized_default_novalue
|
|
|
999efc |
- // Fills [first, last) with std::distance(first, last) default-initialized
|
|
|
999efc |
- // value_types(s).
|
|
|
999efc |
+ // Fills [first, last) with default-initialized value_types.
|
|
|
999efc |
template<typename _ForwardIterator>
|
|
|
999efc |
inline void
|
|
|
999efc |
__uninitialized_default_novalue(_ForwardIterator __first,
|
|
|
999efc |
@@ -772,8 +784,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
|
|
|
999efc |
__uninit_default_novalue(__first, __last);
|
|
|
999efc |
}
|
|
|
999efc |
|
|
|
999efc |
- // __uninitialized_default_n
|
|
|
999efc |
- // Fills [first, first + n) with n default-initialized value_type(s).
|
|
|
999efc |
+ // __uninitialized_default_novalue_n
|
|
|
999efc |
+ // Fills [first, first + n) with default-initialized value_types.
|
|
|
999efc |
template<typename _ForwardIterator, typename _Size>
|
|
|
999efc |
inline _ForwardIterator
|
|
|
999efc |
__uninitialized_default_novalue_n(_ForwardIterator __first, _Size __n)
|
|
|
999efc |
--- /dev/null
|
|
|
999efc |
+++ libstdc++-v3/testsuite/20_util/specialized_algorithms/uninitialized_default/94540.cc
|
|
|
999efc |
@@ -0,0 +1,34 @@
|
|
|
999efc |
+// Copyright (C) 2020 Free Software Foundation, Inc.
|
|
|
999efc |
+//
|
|
|
999efc |
+// This file is part of the GNU ISO C++ Library. This library is free
|
|
|
999efc |
+// software; you can redistribute it and/or modify it under the
|
|
|
999efc |
+// terms of the GNU General Public License as published by the
|
|
|
999efc |
+// Free Software Foundation; either version 3, or (at your option)
|
|
|
999efc |
+// any later version.
|
|
|
999efc |
+
|
|
|
999efc |
+// This library is distributed in the hope that it will be useful,
|
|
|
999efc |
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
999efc |
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
999efc |
+// GNU General Public License for more details.
|
|
|
999efc |
+
|
|
|
999efc |
+// You should have received a copy of the GNU General Public License along
|
|
|
999efc |
+// with this library; see the file COPYING3. If not see
|
|
|
999efc |
+// <http://www.gnu.org/licenses/>.
|
|
|
999efc |
+
|
|
|
999efc |
+// { dg-do run { target { c++11 && { ! simulator } } } }
|
|
|
999efc |
+
|
|
|
999efc |
+#include <memory>
|
|
|
999efc |
+#include <testsuite_hooks.h>
|
|
|
999efc |
+
|
|
|
999efc |
+// Assume that 9MB is larger than the stack limit.
|
|
|
999efc |
+struct X { char data[9*1024*1024]; };
|
|
|
999efc |
+
|
|
|
999efc |
+static_assert( std::is_trivial<X>::value, "" );
|
|
|
999efc |
+
|
|
|
999efc |
+int main()
|
|
|
999efc |
+{
|
|
|
999efc |
+ auto mem = new char[sizeof(X) * 2];
|
|
|
999efc |
+ auto p = reinterpret_cast<X*>(mem);
|
|
|
999efc |
+ std::__uninitialized_default(p, p + 2);
|
|
|
999efc |
+ delete[] mem;
|
|
|
999efc |
+}
|
|
|
999efc |
--- /dev/null
|
|
|
999efc |
+++ libstdc++-v3/testsuite/20_util/specialized_algorithms/uninitialized_default_n/94540.cc
|
|
|
999efc |
@@ -0,0 +1,34 @@
|
|
|
999efc |
+// Copyright (C) 2020 Free Software Foundation, Inc.
|
|
|
999efc |
+//
|
|
|
999efc |
+// This file is part of the GNU ISO C++ Library. This library is free
|
|
|
999efc |
+// software; you can redistribute it and/or modify it under the
|
|
|
999efc |
+// terms of the GNU General Public License as published by the
|
|
|
999efc |
+// Free Software Foundation; either version 3, or (at your option)
|
|
|
999efc |
+// any later version.
|
|
|
999efc |
+
|
|
|
999efc |
+// This library is distributed in the hope that it will be useful,
|
|
|
999efc |
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
999efc |
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
999efc |
+// GNU General Public License for more details.
|
|
|
999efc |
+
|
|
|
999efc |
+// You should have received a copy of the GNU General Public License along
|
|
|
999efc |
+// with this library; see the file COPYING3. If not see
|
|
|
999efc |
+// <http://www.gnu.org/licenses/>.
|
|
|
999efc |
+
|
|
|
999efc |
+// { dg-do run { target { c++11 && { ! simulator } } } }
|
|
|
999efc |
+
|
|
|
999efc |
+#include <memory>
|
|
|
999efc |
+#include <testsuite_hooks.h>
|
|
|
999efc |
+
|
|
|
999efc |
+// Assume that 9MB is larger than the stack limit.
|
|
|
999efc |
+struct X { char data[9*1024*1024]; };
|
|
|
999efc |
+
|
|
|
999efc |
+static_assert( std::is_trivial<X>::value, "" );
|
|
|
999efc |
+
|
|
|
999efc |
+int main()
|
|
|
999efc |
+{
|
|
|
999efc |
+ auto mem = new char[sizeof(X) * 2];
|
|
|
999efc |
+ auto p = reinterpret_cast<X*>(mem);
|
|
|
999efc |
+ std::__uninitialized_default_n(p, 2);
|
|
|
999efc |
+ delete[] mem;
|
|
|
999efc |
+}
|
|
|
999efc |
--- /dev/null
|
|
|
999efc |
+++ libstdc++-v3/testsuite/20_util/specialized_algorithms/uninitialized_value_construct/94540.cc
|
|
|
999efc |
@@ -0,0 +1,35 @@
|
|
|
999efc |
+// Copyright (C) 2020 Free Software Foundation, Inc.
|
|
|
999efc |
+//
|
|
|
999efc |
+// This file is part of the GNU ISO C++ Library. This library is free
|
|
|
999efc |
+// software; you can redistribute it and/or modify it under the
|
|
|
999efc |
+// terms of the GNU General Public License as published by the
|
|
|
999efc |
+// Free Software Foundation; either version 3, or (at your option)
|
|
|
999efc |
+// any later version.
|
|
|
999efc |
+
|
|
|
999efc |
+// This library is distributed in the hope that it will be useful,
|
|
|
999efc |
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
999efc |
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
999efc |
+// GNU General Public License for more details.
|
|
|
999efc |
+
|
|
|
999efc |
+// You should have received a copy of the GNU General Public License along
|
|
|
999efc |
+// with this library; see the file COPYING3. If not see
|
|
|
999efc |
+// <http://www.gnu.org/licenses/>.
|
|
|
999efc |
+
|
|
|
999efc |
+// { dg-options "-std=gnu++17" }
|
|
|
999efc |
+// { dg-do run { target { c++17 && { ! simulator } } } }
|
|
|
999efc |
+
|
|
|
999efc |
+#include <memory>
|
|
|
999efc |
+#include <testsuite_hooks.h>
|
|
|
999efc |
+
|
|
|
999efc |
+// Assume that 9MB is larger than the stack limit.
|
|
|
999efc |
+struct X { char data[9*1024*1024]; };
|
|
|
999efc |
+
|
|
|
999efc |
+static_assert( std::is_trivial_v<X> );
|
|
|
999efc |
+
|
|
|
999efc |
+int main()
|
|
|
999efc |
+{
|
|
|
999efc |
+ auto mem = new char[sizeof(X) * 2];
|
|
|
999efc |
+ auto p = reinterpret_cast<X*>(mem);
|
|
|
999efc |
+ std::uninitialized_value_construct(p, p + 2);
|
|
|
999efc |
+ delete[] mem;
|
|
|
999efc |
+}
|
|
|
999efc |
--- /dev/null
|
|
|
999efc |
+++ libstdc++-v3/testsuite/20_util/specialized_algorithms/uninitialized_value_construct_n/94540.cc
|
|
|
999efc |
@@ -0,0 +1,34 @@
|
|
|
999efc |
+// Copyright (C) 2020 Free Software Foundation, Inc.
|
|
|
999efc |
+//
|
|
|
999efc |
+// This file is part of the GNU ISO C++ Library. This library is free
|
|
|
999efc |
+// software; you can redistribute it and/or modify it under the
|
|
|
999efc |
+// terms of the GNU General Public License as published by the
|
|
|
999efc |
+// Free Software Foundation; either version 3, or (at your option)
|
|
|
999efc |
+// any later version.
|
|
|
999efc |
+
|
|
|
999efc |
+// This library is distributed in the hope that it will be useful,
|
|
|
999efc |
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
999efc |
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
999efc |
+// GNU General Public License for more details.
|
|
|
999efc |
+
|
|
|
999efc |
+// You should have received a copy of the GNU General Public License along
|
|
|
999efc |
+// with this library; see the file COPYING3. If not see
|
|
|
999efc |
+// <http://www.gnu.org/licenses/>.
|
|
|
999efc |
+
|
|
|
999efc |
+// { dg-options "-std=gnu++17" }
|
|
|
999efc |
+// { dg-do run { target { c++17 && { ! simulator } } } }
|
|
|
999efc |
+
|
|
|
999efc |
+#include <memory>
|
|
|
999efc |
+#include <testsuite_hooks.h>
|
|
|
999efc |
+
|
|
|
999efc |
+// Assume that 9MB is larger than the stack limit.
|
|
|
999efc |
+struct X { char data[9*1024*1024]; };
|
|
|
999efc |
+
|
|
|
999efc |
+static_assert( std::is_trivial_v<X> );
|
|
|
999efc |
+
|
|
|
999efc |
+int main()
|
|
|
999efc |
+{
|
|
|
999efc |
+ auto mem = new char[sizeof(X) * 2];
|
|
|
999efc |
+ std::uninitialized_value_construct_n(reinterpret_cast<X*>(mem), 2);
|
|
|
999efc |
+ delete[] mem;
|
|
|
999efc |
+}
|
|
|
999efc |
--- /dev/null
|
|
|
999efc |
+++ libstdc++-v3/testsuite/23_containers/vector/cons/94540.cc
|
|
|
999efc |
@@ -0,0 +1,35 @@
|
|
|
999efc |
+// Copyright (C) 2020 Free Software Foundation, Inc.
|
|
|
999efc |
+//
|
|
|
999efc |
+// This file is part of the GNU ISO C++ Library. This library is free
|
|
|
999efc |
+// software; you can redistribute it and/or modify it under the
|
|
|
999efc |
+// terms of the GNU General Public License as published by the
|
|
|
999efc |
+// Free Software Foundation; either version 3, or (at your option)
|
|
|
999efc |
+// any later version.
|
|
|
999efc |
+
|
|
|
999efc |
+// This library is distributed in the hope that it will be useful,
|
|
|
999efc |
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
999efc |
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
999efc |
+// GNU General Public License for more details.
|
|
|
999efc |
+
|
|
|
999efc |
+// You should have received a copy of the GNU General Public License along
|
|
|
999efc |
+// with this library; see the file COPYING3. If not see
|
|
|
999efc |
+// <http://www.gnu.org/licenses/>.
|
|
|
999efc |
+
|
|
|
999efc |
+// { dg-do run { target { c++11 && { ! simulator } } } }
|
|
|
999efc |
+
|
|
|
999efc |
+#include <vector>
|
|
|
999efc |
+#include <testsuite_hooks.h>
|
|
|
999efc |
+
|
|
|
999efc |
+// Assume that 9MB is larger than the stack limit.
|
|
|
999efc |
+struct X { char data[9*1024*1024]; };
|
|
|
999efc |
+
|
|
|
999efc |
+static_assert( std::is_trivial<X>::value, "" );
|
|
|
999efc |
+
|
|
|
999efc |
+int main()
|
|
|
999efc |
+{
|
|
|
999efc |
+ std::vector<X> v(1);
|
|
|
999efc |
+ VERIFY( v.size() == 1 );
|
|
|
999efc |
+ v.clear();
|
|
|
999efc |
+ v.resize(2);
|
|
|
999efc |
+ VERIFY( v.size() == 2 );
|
|
|
999efc |
+}
|