Skip to content

Fix sparse FlatMap swap thumbnail reset - #3619

Merged
chenBright merged 1 commit into
apache:masterfrom
songgaoye:fix-sparse-flatmap-swap
Oct 18, 2026
Merged

chenBright merged 1 commit into
apache:masterfrom
songgaoye:fix-sparse-flatmap-swap

Conversation

@songgaoye

Copy link
Copy Markdown
Contributor

What problem does this PR solve?

Issue Number: resolve

Problem Summary:

FlatMap::swap() does not fully reset the moved-from object when swapping a
default-initialized SparseFlatMap with a dynamically initialized one.

After transferring the dynamic buckets to the default map, the moved-from map
resets _buckets but mistakenly assigns _thumbnail to itself. Its thumbnail
therefore still points to the transferred dynamic storage. Once the new owner
is destroyed, later operations on the moved-from map can access freed memory.

What is changed and the side effects?

Changed:

  • Reset the moved-from map's _thumbnail to _default_thumbnail together
    with _buckets.
  • Add regression tests for swapping default and dynamic SparseFlatMap
    instances, including use after the temporary owner is destroyed.

Side effects:

  • Performance effects:

  • Breaking backward compatibility:


Check List:

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The focused fix corrects storage ownership, and regression tests cover both reported failure scenarios.

No open issues 🎉

What changed in this PR

Fixes SparseFlatMap::swap() so the moved-from map no longer retains a thumbnail pointer to transferred storage.

Changes:

  • Reset the moved-from thumbnail to its default storage.
  • Add regression tests for map reuse and destruction of the transferred storage’s owner.
File Description
test/​flat_map_unittest.cpp Adds regression coverage for sparse-map swaps.
src/​butil/​containers/​flat_map_inl.h Corrects the moved-from thumbnail reset.

Review effort: Balanced

@wwbmmm

wwbmmm commented Oct 18, 2026

Copy link
Copy Markdown
Contributor

LGTM

@chenBright chenBright left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@chenBright
chenBright merged commit d1c3f7c into apache:master Oct 18, 2026
25 checks passed
@songgaoye
songgaoye deleted the fix-sparse-flatmap-swap branch October 18, 2026 02:57
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.

4 participants