Make tree deserialisation exception-safe - #80
Merged
Amaury Chamayou (achamayou) merged 1 commit intoAug 25, 2026
Conversation
This was referenced Aug 24, 2026
Amaury Chamayou (achamayou)
force-pushed
the
achamayou-tree-deserialise-ownership
branch
2 times, most recently
from
August 24, 2026 20:31
2a49f78 to
5778593
Compare
Amaury Chamayou (achamayou)
marked this pull request as ready for review
August 25, 2026 12:12
Copilot started reviewing on behalf of
Amaury Chamayou (achamayou)
August 25, 2026 12:13
View session
Contributor
There was a problem hiding this comment.
Pull request overview
Makes tree deserialization exception-safe by using RAII during reconstruction.
Changes:
- Owns temporary nodes with
std::unique_ptr. - Publishes root and leaf metadata only after successful reconstruction.
- Adds truncated flushed-hash regression coverage.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
merklecpp.h |
Adds exception-safe ownership and final state commit. |
test/unit_tests.cpp |
Tests truncated flushed-edge hashes. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Amaury Chamayou (achamayou)
force-pushed
the
achamayou-tree-deserialise-ownership
branch
2 times, most recently
from
August 25, 2026 12:54
4a25c5b to
e3801e3
Compare
Own partially reconstructed nodes until the complete tree has been built, then transfer the final root and leaf metadata. Add a malformed flushed-tree regression that exercises cleanup after partial reconstruction. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Amaury Chamayou (achamayou)
force-pushed
the
achamayou-tree-deserialise-ownership
branch
from
August 25, 2026 15:09
e3801e3 to
048536d
Compare
Eddy Ashton (eddyashton)
approved these changes
Aug 25, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
std::unique_ptrownership instead of assembling a raw-pointer forestleaf_nodesandnum_flushedas the final commit stepRationale
The tree stores its completed structure as raw pointers owned recursively by the root. During deserialisation, however, there is no root owner until reconstruction finishes. The previous implementation placed newly allocated leaves and intermediate nodes directly in raw-pointer containers, so an exception during reconstruction could strand every node built so far.
The lower bounds-checking PR now rejects known malformed encodings before allocation, but reconstruction still contains throwing operations, including node allocation and vector growth, and must remain safe if later reconstruction work introduces another exception. This PR gives the in-progress forest explicit RAII ownership without changing the successful tree representation.
Ownership and cleanup guarantees
std::unique_ptrimmediately after allocation; the temporary leaf index contains non-owning aliases onlyNode::make(left, right)succeeds; ownership is released only after the new parent has taken responsibility for recursively deleting themleaf_nodesandnum_flushedare not published until the root is complete, so a failed deserialisation leaves no partially published tree state_root; its existing recursive destructor owns all descendants, whileleaf_nodesremains the tree's non-owning leaf indexMalformed input coverage
The lower PR validates platform-size limits and the full serialized hash count before reconstruction, including truncated resident leaves and missing flushed-edge hashes. This PR retains a regression with one resident leaf,
num_flushed == 3, and only one of the two required flushed-edge hashes. It must fail withnot enough bytes; combined with sanitizer execution, this covers the stacked malformed-input path while the ownership changes protect any exception that occurs after allocation begins.Testing
Clang 21.1.8 Debug build with AddressSanitizer, LeakSanitizer (
detect_leaks=1), and UndefinedBehaviorSanitizer:TreeT rejects invalid serialised leaf dataTreeT deserialises flushed counts beyond signed shift widthEmpty treeOne-node treeThree-node treeResult: 5 test cases passed, 65 assertions passed.