Skip to content

REQ: keep the sketch valid when an allocation or an item copy throws - #587

Open
leerho wants to merge 7 commits into
masterfrom
req-update-safety
Open

leerho wants to merge 7 commits into
masterfrom
req-update-safety

Conversation

@leerho

@leerho leerho commented Nov 19, 2026 •

Copy link
Copy Markdown
Member

This fixes the REQ gaps found while reviewing #582, and the C++ side of the gaps that apache/datasketches-java#808 addresses in Java.

Compactor:

  • The copy constructor and the converting constructor now release their storage and the copies already made when a copy throws. The converting constructor does the same when it rejects the order of the items. Before, all of that leaked.
  • merge() first works out the sections and capacity it will need, then allocates once. Next it copies the other compactor's items, destroying the copies if one throws. Only after that does it change the state and the sections. Before, it changed the state and sections first, and it leaked the copies made before a copy that threw. It also now compares with the sketch's comparator instead of C().
  • compact() now allocates the space in the next level, and any growth that more sections need, before it flips or draws the coin or changes anything. Before, a failed compaction left the coin flipped or a random bit drawn, and it could change the sections and then fail to grow. A failed compaction now leaves both compactors as they were and draws no random bit.

Sketch:

  • update() compares the item with min and max and copies it first. It sets min and max only after the item is in level zero, so a failed append no longer leaves a min or max for an item that isn't retained.
  • merge() copies the new min and max before it sets either.

The docs of update() and merge() now say what a failure leaves behind. A comparator that throws while items are sorted or merged leaves the sketch undefined, as decided for Java in apache/datasketches-java#805.

One difference from Java: C++ never had the window in which #808's compress() could lose promoted items, because compact() already reserved space in the next level before moving any item.

Tests:

  • A compactor-level test fails each allocation of a compaction in turn. Both compactors must stay byte-identical and no random bit may be drawn. Repeating the compaction must then give exactly what it would have given without the failure.
  • Throwing item copies, made to throw at each copy in turn:
    • in updates: the sketch must be unchanged;
    • in const& merges: the target must stay consistent and usable;
    • in the copy constructor: nothing may leak.
  • A converting constructor whose comparator rejects the order must leak nothing.
  • A comparator that can't be default-constructed.

The five allocation tests fail on master and pass here. The comparator test needs this change to compile. Serialized results are byte-identical to master; I checked 600 seeded combinations of HRA and LRA, k = 4, 12 and 50, const& and rvalue merges, and double and string sketches. All tests pass under ASan and UBSan.

Added after review (a68f18e, a73df7c, 36789db, f2019a3):

  • REQ merge() of a sketch into itself merges a copy. Classic quantiles does the same; on master, merging a classic quantiles sketch into itself used freed memory.
  • REQ merge() merges all levels or none: it copies the items of other into every level, and min and max, before it changes anything, so a failed copy or allocation leaves the sketch unchanged.
  • REQ update() and merge() cap n at req_constants::MAX_N = 2^63 - 1, as KLL does since KLL: keep the sketch valid when an item copy throws; cap n at 2^63 - 1 #585.
  • When level zero must grow, the compactor copies the item first, since it may be one of the items that growing frees.
  • The sections stop doubling at 48; an empty compactor gets no storage and is not passed to the serde; the converting constructor releases its items if the comparator throws.
  • quantiles_sorted_view (common) checks split points with the view's comparator, so get_CDF() and get_PMF() work with comparators that can't be default-constructed, for KLL and classic quantiles too.

Serialized results are still byte-identical to master.

🤖 Generated with Claude Code

Compactor:
- The copy and converting constructors release their storage and the
  copies made when a copy throws, or when the converting constructor
  rejects the order of the items.
- merge() works out the sections and the capacity first and allocates
  once, then copies the other compactor's items and destroys the copies
  if one throws. Only then does it change the state and the sections.
  It used to change them first, and leaked the copies made before a
  copy that threw. It compares with the sketch's comparator, not C().
- compact() allocates the space in the next level, and the growth that
  more sections need, before it flips or draws the coin and changes
  anything. A failed compaction used to leave the coin flipped (or a
  random bit drawn), and could change the sections and then fail to grow.

Sketch:
- update() compares the item with min and max and copies it first, and
  sets min and max only after the item is in level zero.
- merge() copies the new min and max before it sets either.

The same gaps are addressed in datasketches-java by
apache/datasketches-java#808, except the item-loss window in compress(),
which C++ does not have: compact() reserved the next level's space before
moving any item.

Results are byte-identical to before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@leerho
leerho requested review from proost and tisonkun November 19, 2026 19:02
@coveralls

This comment was marked as outdated.

A compactor that has never held an item has no storage, so serialize()
passed a null pointer with a count of 0, and the default serde passed it
on to memcpy(). UBSan on Linux reports that as undefined behavior.
Serialize the items only when there are some; the bytes are unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Comment thread req/include/req_compactor_impl.hpp Resolved
Comment thread req/include/req_compactor_impl.hpp Resolved
Comment thread req/include/req_compactor_impl.hpp Resolved
Comment thread req/include/req_compactor_impl.hpp Resolved
Comment thread req/include/req_sketch_impl.hpp Resolved
Comment thread req/include/req_sketch_impl.hpp Resolved
Comment thread req/include/req_sketch_impl.hpp Resolved
Comment thread req/test/req_sketch_test.cpp Resolved
- merge(): merging a sketch into itself merges a copy, as merging read
  other while changing this sketch.
- merge(): merge the compactors from the highest level down, and set min
  and max with the first items of other that the sketch takes. A failed
  merge then leaves min and max unchanged unless items of other were
  added, and in that case a level above level zero holds items, so the
  sketch stays in estimation mode, where min and max are serialized.
- update() and merge(): cap n at req_constants::MAX_N = 2^63 - 1, as
  KLL does.
- Compactor append(): when level zero must grow, copy the item first,
  since it may be one of the items that growing moves and frees.
- Compactor: stop doubling the sections at 48, which keeps
  1 << (num_sections - 1) defined and stays within what deserialize
  accepts.
- Compactor converting constructor: release the items if the comparator
  throws while the order is checked.
- Compactor deserialize: an empty compactor gets no storage.
- quantiles_sorted_view: check split points with the view's comparator
  instead of a default-constructed one, so get_CDF() and get_PMF()
  compile with comparators that can't be default-constructed and use
  the right order with ones that have state.

Serialized results are byte-identical to master.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@leerho
leerho requested a review from tisonkun November 20, 2026 18:02
leerho and others added 2 commits November 20, 2026 10:08
merge() read other while it changed this sketch, so merging a sketch
into itself used memory it had freed. It now merges a copy, as REQ does.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Deserialize now checks that n is the total weight of the retained items (#595), so the test builds an image whose weight is 2^63 - 2 instead of overwriting n.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Comment thread req/include/req_compactor_impl.hpp Resolved
Comment thread req/include/req_sketch_impl.hpp Resolved
merge() now copies the items of other into every level, and the new min and max, before it changes anything. If an allocation or a copy fails, it destroys the copies and removes the levels it added, so this sketch is unchanged. Before, a failure could leave some levels of other merged. (Suggested by proost in review.)

The compactor merge is split into merge_prepare(), which allocates and copies, and merge_commit(), which adds the copies to the compactor before it compares any, so a comparator that throws while the items are sorted or merged leaks nothing.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@leerho
leerho requested a review from proost December 4, 2026 18:58

This branch has not been deployed

No deployments
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