fix: properly free BeamState on error paths to prevent segfaults (closes #675)#676
Merged
anshalshukla merged 4 commits intomainfrom Mar 16, 2026
Merged
fix: properly free BeamState on error paths to prevent segfaults (closes #675)#676anshalshukla merged 4 commits intomainfrom
anshalshukla merged 4 commits intomainfrom
Conversation
added 3 commits
March 13, 2026 11:56
…Topic format signature (closes #594)
#675) Three memory-safety fixes in chain.zig: 1. onBlock — computedstate block: add errdefer for cpost_state - After allocator.create(): errdefer destroy (covers sszClone failure) - After sszClone(): errdefer deinit (covers verifySignatures / apply_transition failures, e.g. InvalidPostState). LIFO ordering ensures deinit runs before destroy, so interior ArrayList fields are freed cleanly. - Previously: a partially-mutated cloned state (historical_block_hashes already appended by process_slots) was silently leaked, causing UB / segfault. 2. onBlock — function level: add errdefer for post_state when we own it - If computedstate succeeds but a later step (forkChoice.onBlock, updateHead, InvalidSignatureGroups) returns an error, post_state was previously leaked. 3. BeamChain init — cloned_anchor_state: add errdefer before states.put - Same create+sszClone pattern without cleanup on states.put failure.
g11tech
approved these changes
Mar 16, 2026
anshalshukla
approved these changes
Mar 16, 2026
GrapeBaBa
pushed a commit
that referenced
this pull request
Apr 2, 2026
#675) (#676) * fix: replace {any} with {f} for types with format methods, fix GossipTopic format signature (closes #594) * fix: replace all {any} with {f} for types with custom format methods (closes #594) * fix: properly free BeamState on error paths to prevent segfaults (closes #675) Three memory-safety fixes in chain.zig: 1. onBlock — computedstate block: add errdefer for cpost_state - After allocator.create(): errdefer destroy (covers sszClone failure) - After sszClone(): errdefer deinit (covers verifySignatures / apply_transition failures, e.g. InvalidPostState). LIFO ordering ensures deinit runs before destroy, so interior ArrayList fields are freed cleanly. - Previously: a partially-mutated cloned state (historical_block_hashes already appended by process_slots) was silently leaked, causing UB / segfault. 2. onBlock — function level: add errdefer for post_state when we own it - If computedstate succeeds but a later step (forkChoice.onBlock, updateHead, InvalidSignatureGroups) returns an error, post_state was previously leaked. 3. BeamChain init — cloned_anchor_state: add errdefer before states.put - Same create+sszClone pattern without cleanup on states.put failure. --------- Co-authored-by: zclawz <zclawz@blockblaz.io> Co-authored-by: Anshal Shukla <53994948+anshalshukla@users.noreply.github.com>
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.
Problem
Fixes #675 — zeam segfaults on
InvalidPostStatewhen processing a cached block.Root cause: In
onBlock'scomputedstate:labeled block,cpost_stateis heap-allocated withallocator.createand then deep-cloned viasszClone(which allocates interiorArrayListfields likehistorical_block_hashes,justified_slots, etc.). IfverifySignaturesorapply_transitionsubsequently fails, there were noerrdefercleanup paths — the partially-mutated state leaked, causing UB and the observed segfault.Specifically:
apply_transitioncallsprocess_slotswhich appends tohistorical_block_hashesbefore the post-state root check. WhenInvalidPostStateis returned, those appended bytes are never freed → dangling allocations → segfault.Fix — three
errdeferadditions inchain.zig1.
onBlock— insidecomputedstate:blockLIFO
errdeferpair:destroyregistered first (coverssszClonefailure), thendeinitregistered after clone succeeds (coversverifySignatures/apply_transitionfailure). When both fire,deinitruns first freeing interior fields, thendestroyfrees the outer struct.2.
onBlock— function level (post-state ownership)If
computedstate:succeeds but a later step fails (forkChoice.onBlock,updateHead,InvalidSignatureGroups),post_statewas previously leaked. Addederrdeferguarded bypost_state_ownedflag.3.
BeamChain.init—cloned_anchor_stateSame create+sszClone pattern without cleanup if
states.putfails.