Fix circular_queue pop_front(count) debug assert and empty() - #141
Open
rootkiller6788 wants to merge 1 commit into
Open
Fix circular_queue pop_front(count) debug assert and empty()#141rootkiller6788 wants to merge 1 commit into
rootkiller6788 wants to merge 1 commit into
Conversation
pop_front(count) used a wrong debug assumption: it asserted that offset_ wraps to zero exactly when count == chunk, but offset_ only wraps when the first chunk reaches the end of the buffer. Any valid pop_front(count) that does not wrap (or that pops more than one chunk) aborted the process in debug builds. The assertion now checks the actual safety precondition: when more elements remain beyond the first chunk, the buffer must have wrapped around. empty() reported the backing vector's emptiness instead of the queue's logical emptiness, so a drained queue (size() == 0) still reported empty() == false, breaking the while (!q.empty()) pop_front() idiom (and enabling front()/back() to read destroyed elements). It now returns size_ == 0. Add regression tests for pop_front(count) with and without wrap-around and for empty() after draining.
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
Two bugs in
circular_queue:pop_front(count)aborts in debug builds on valid input. The debug assumptionFATAL_ASSUME_EQ(offset_ == 0, count == chunk)is wrong:offset_wraps to zero exactly when the first chunk reaches the end of the backing buffer, which is unrelated tocount == chunk. Reproduced:q.push_back(0..2); q.pop_front(1);aborts at circular_queue.h:262 becauseoffset_ == 0is false whilecount == chunkis true. Similarly a wrap-around pop withcount > chunkaborts becauseoffset_ == 0is true whilecount == chunkis false. The only input that survived wascount == buffer_size - offset_exactly. The assertion now encodes the real safety precondition of the second loop: when more elements remain to be destroyed beyond the first chunk, the buffer must have wrapped around (sooffset_was reset to 0).empty()reports the wrong value. It returnedqueue_.empty()(emptiness of the backingstd::vector) instead ofsize_ == 0. After all elements are popped,size()is 0 butempty()stays false because the backing vector still holds storage. This breaks the canonicalwhile (!q.empty()) { q.pop_front(); }idiom (it never terminates) and letsfront()/back()read already-destroyed elements. It now returnssize_ == 0.Test plan
pop_front_count,pop_front_count_wrap,empty_after_drain.pop_front_countaborts against the old header (wrong assumption);empty_after_drainhangs forever against the oldempty().circular_queuesuite passes with the fix (13/13), plus a randomized model test (5000 mixed push/pop ops, std::deque oracle) and astd::stringwrap-around test.