copy fill value before reallocation in array::insert/resize#1167
Conversation
|
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 |
|
GCOVR code coverage report https://1167.json.prtest2.cppalliance.org/gcovr/index.html Build time: 2026-07-07 07:51:37 UTC |
|
|
|
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. |

Repro: on a full array,
a.insert(a.begin(), n, a[0])ora.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.