Skip to content

copy fill value before reallocation in array::insert/resize#1167

Merged
grisumbras merged 1 commit into
boostorg:developfrom
Ramya-9353:array-fill-self-alias
Jul 9, 2026
Merged

copy fill value before reallocation in array::insert/resize#1167
grisumbras merged 1 commit into
boostorg:developfrom
Ramya-9353:array-fill-self-alias

Conversation

@Ramya-9353

Copy link
Copy Markdown
Contributor

Repro: on a full array, a.insert(a.begin(), n, a[0]) or a.resize(n, a[1]) reads freed memory (ASAN heap-use-after-free in the value copy constructor).
Cause: revert_insert relocates the elements into a newly allocated table and frees the old one before the fill loop runs, so a value that aliases an existing element is left dangling.
Fix: copy the value into a local before constructing revert_insert, the same snapshot object::emplace_impl and array::emplace already take. std::vector defines these self-referential fills, so the copy overloads should honour it too.

@cppalliance-bot

Copy link
Copy Markdown

An automated preview of the documentation is available at https://1167.json.prtest2.cppalliance.org/libs/json/doc/html/index.html

If more commits are pushed to the pull request, the docs will rebuild at the same URL.

2026-07-07 07:42:49 UTC

@cppalliance-bot

Copy link
Copy Markdown

GCOVR code coverage report https://1167.json.prtest2.cppalliance.org/gcovr/index.html
LCOV code coverage report https://1167.json.prtest2.cppalliance.org/genhtml/index.html
Coverage Diff Report https://1167.json.prtest2.cppalliance.org/diff-report/index.html

Build time: 2026-07-07 07:51:37 UTC

@cppalliance-bot

Copy link
Copy Markdown

@grisumbras grisumbras merged commit d51db36 into boostorg:develop Jul 9, 2026
2 of 3 checks passed
@grisumbras

Copy link
Copy Markdown
Member

Thank you for catching this. I suspect the ideal solution is actually to change the API to accept by value and then eliminate the problem entirely. But since we're hours before the beta cutoff, I'm going to merge this now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants