libstdc++: Avoid stack overflow in std::vector (PR 94540)
authorJonathan Wakely <jwakely@redhat.com>
Wed, 17 Jun 2020 21:49:06 +0000 (22:49 +0100)
committerJonathan Wakely <jwakely@redhat.com>
Wed, 17 Jun 2020 21:49:06 +0000 (22:49 +0100)
commit632183ddcc8f3aead8b4fc63c4ab59a42ef9ad00
treeddcacb50ebec5eaf0359ac2b8fb250d934f467ab
parent973bce0fb50bbfd91f47238b82b99935525716ad
libstdc++: Avoid stack overflow in std::vector (PR 94540)

The std::__uninitialized_default_n algorithm used by std::vector creates
an initial object as a local variable then copies that into the
destination range. If the object is too large for the stack this
crashes. We should create the first object directly into the
destination and then copy it from there.

This doesn't fix the bug for C++98, because in that case the initial
value is created as a default argument of the vector constructor i.e. in
the user's code, not inside libstdc++. We can't prevent that.

PR libstdc++/94540
* include/bits/stl_uninitialized.h (__uninitialized_default_1<true>):
Construct the first value at *__first instead of on the stack.
(__uninitialized_default_n_1<true>): Likewise.
Improve comments on several of the non-standard algorithms.
* testsuite/20_util/specialized_algorithms/uninitialized_default/94540.cc:
New test.
* testsuite/20_util/specialized_algorithms/uninitialized_default_n/94540.cc:
New test.
* testsuite/20_util/specialized_algorithms/uninitialized_value_construct/94540.cc:
New test.
* testsuite/20_util/specialized_algorithms/uninitialized_value_construct_n/94540.cc:
New test.
* testsuite/23_containers/vector/cons/94540.cc: New test.
libstdc++-v3/include/bits/stl_uninitialized.h
libstdc++-v3/testsuite/20_util/specialized_algorithms/uninitialized_default/94540.cc [new file with mode: 0644]
libstdc++-v3/testsuite/20_util/specialized_algorithms/uninitialized_default_n/94540.cc [new file with mode: 0644]
libstdc++-v3/testsuite/20_util/specialized_algorithms/uninitialized_value_construct/94540.cc [new file with mode: 0644]
libstdc++-v3/testsuite/20_util/specialized_algorithms/uninitialized_value_construct_n/94540.cc [new file with mode: 0644]
libstdc++-v3/testsuite/23_containers/vector/cons/94540.cc [new file with mode: 0644]