Skip to content

Commit

Permalink
buffered_channel with range-for syntax leaks #258
Browse files Browse the repository at this point in the history
- call destructor before take new value from channel via placement new
- skip calling desturctorfor the frist call of increment_()
  • Loading branch information
olk committed Sep 30, 2020
1 parent f84f1a6 commit e440623
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
7 changes: 5 additions & 2 deletions include/boost/fiber/buffered_channel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -547,9 +547,12 @@ class buffered_channel {
buffered_channel * chan_{ nullptr };
storage_type storage_;

void increment_() {
void increment_( bool initial = false) {
BOOST_ASSERT( nullptr != chan_);
try {
if ( ! initial) {
reinterpret_cast< value_type * >( std::addressof( storage_) )->~value_type();
}
::new ( static_cast< void * >( std::addressof( storage_) ) ) value_type{ chan_->value_pop() };
} catch ( fiber_error const&) {
chan_ = nullptr;
Expand All @@ -569,7 +572,7 @@ class buffered_channel {

explicit iterator( buffered_channel< T > * chan) noexcept :
chan_{ chan } {
increment_();
increment_( true);
}

iterator( iterator const& other) noexcept :
Expand Down
7 changes: 5 additions & 2 deletions include/boost/fiber/unbuffered_channel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -577,9 +577,12 @@ class unbuffered_channel {
unbuffered_channel * chan_{ nullptr };
storage_type storage_;

void increment_() {
void increment_( bool initial = false) {
BOOST_ASSERT( nullptr != chan_);
try {
if ( ! initial) {
reinterpret_cast< value_type * >( std::addressof( storage_) )->~value_type();
}
::new ( static_cast< void * >( std::addressof( storage_) ) ) value_type{ chan_->value_pop() };
} catch ( fiber_error const&) {
chan_ = nullptr;
Expand All @@ -599,7 +602,7 @@ class unbuffered_channel {

explicit iterator( unbuffered_channel< T > * chan) noexcept :
chan_{ chan } {
increment_();
increment_( true);
}

iterator( iterator const& other) noexcept :
Expand Down

0 comments on commit e440623

Please sign in to comment.