Opened 5 years ago
Closed 4 years ago
#13533 closed Bugs (fixed)
Boost vector resize causes assert(false)
| Reported by: | Owned by: | Ion Gaztañaga | |
|---|---|---|---|
| Milestone: | To Be Determined | Component: | container | 
| Version: | Boost 1.67.0 | Severity: | Problem | 
| Keywords: | Cc: | 
Description
When using a boost vector with a custom allocator, a resize that results in memory growth in both directions causes an assert(false) in insert_value_initialized_n_proxy::copy_n_and_update(). The code in question is contained in boost/containers/detail/advanced_insert_int.hpp.
I have attached a test case that results in an assert. The test case uses a custom allocator, but the problem was originally encountered using the boost shared memory allocator.
The following patch appears to correct the behavior:
128,129c128,129
<    void copy_n_and_update(Allocator &, Iterator, size_type) const
<    {  BOOST_ASSERT(false); }
---
>    void copy_n_and_update(Allocator &a, Iterator p, size_type n) const
>    {  boost::container::uninitialized_value_init_alloc_n(a, n, p);  }
142,143c142,143
<    void copy_n_and_update(Allocator &, Iterator, size_type) const
<    {  BOOST_ASSERT(false); }
---
>    void copy_n_and_update(Allocator & a, Iterator n, size_type p) const
>    {  boost::container::uninitialized_default_init_alloc_n(a, n, p);  }
      Attachments (1)
Change History (4)
by , 5 years ago
| Attachment: | alloc_test.cpp added | 
|---|
comment:1 by , 4 years ago
comment:2 by , 4 years ago
The solution is to value-initialize an object and move it to the target. For default-initialization, this can be elided for POD-like types. Fixed in:
https://github.com/boostorg/container/commit/42c6be5887b34b67be4d54041acab12157d080f9
Thanks for the report.
comment:3 by , 4 years ago
| Resolution: | → fixed | 
|---|---|
| Status: | new → closed | 

Many thanks for the report and the test case. I'm afraied the patch is not correct as it constructs objects over already constructed ones (copy_n_and_update means that there are n objects already constructed in the range starting at the Iterator).
I need to think about what is the expected output by the user in this case and implement a solution based on a copy, instead of a "uninitialized" copy.