]> gcc.gnu.org Git - gcc.git/commit
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>
Fri, 23 Jun 2023 12:37:07 +0000 (13:37 +0100)
commit7dba69b5e9977408921c635681a0daa34b813e16
tree766b6fd3a96c7550af49389a55864b55166f07b8
parentfab3692a2cf961b7364d7f77dd976ba0e4f752b7
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.

(cherry picked from commit 632183ddcc8f3aead8b4fc63c4ab59a42ef9ad00)
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]
This page took 0.058982 seconds and 6 git commands to generate.