Fix double destructor call in channel iterators#339
Open
a2Fsa2k wants to merge 1 commit into
Open
Conversation
… iterators Commit 37b5f77 (boostorg#209) added an explicit destructor call in operator++() for channel iterators. Later, commit e440623 (boostorg#258) added the same destructor call inside increment_() which operator++() calls. This results in the destructor running twice on every iterator increment, causing undefined behavior (double-free) for non-trivially-destructible types. This bug affects all users of range-for loops over buffered_channel and unbuffered_channel with types like std::string, shared_ptr, etc. Fix by removing the redundant destructor call in operator++() since increment_() already handles it correctly. Regression test uses a type with an atomic destruction counter to verify objects are destroyed exactly once.
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
buffered_channelandunbuffered_channeliterators that causes undefined behavior (double-free) with non-trivially-destructible types.operator++()that was already handled insideincrement_().Problem
When iterating over a
buffered_channel<T>orunbuffered_channel<T>with range-for whereThas a non-trivial destructor (e.g.,std::string,shared_ptr), the destructor is called twice on every increment. This is a double-free — undefined behavior that manifests as crashes or corruption.Root cause
Two overlapping commits created the double-destroy:
~value_type()inoperator++()~value_type()insideincrement_()guarded by!initialSince
operator++()callsincrement_(), the destructor runs twice on the same dead object.Fix
Remove the explicit destructor call in
operator++()from bothbuffered_channel.hpp:412andunbuffered_channel.hpp:447. The call inincrement_()handles it correctly.Testing
int, which masked the bug — trivially destructible).test_rangefor_non_trivial_dtortests in both channel test files usecounted_type, a type with an atomic destruction counter, verifying each object is destroyed exactly once.-fsyntax-only.Files changed
include/boost/fiber/buffered_channel.hpp~value_type()include/boost/fiber/unbuffered_channel.hpp~value_type()test/test_buffered_channel_post.cpptest/test_unbuffered_channel_post.cpp