Fix circular_queue pop_front(count) debug assert and empty() (#141)
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.Pull Request resolved: https://github.com/facebook/fatal/pull/141
Test Plan:
- Added regression tests:
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().- Full
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.Reviewed By: ilvokhin
Differential Revision: D118651261
Pulled By: yfeldblum
fbshipit-source-id: c49659b62c3dc0c2a082323500314feb00d84282
版权所有:中国计算机学会技术支持:开源发展技术委员会
京ICP备13000930号-9
京公网安备 11010802047560号
Facebook Template Library
Fatal (Facebook Template Library) is a library for fast prototyping software in modern C++.
It provides facilities heavily based on template meta-programming, while keeping most of the complexity under-the-hood, to enhance the expressive power of C++.
Fatal also provides lessons on how to write meta-programs, as well as on how to make the best use of the library, starting at beginner levels.
The goal is make its benefits available to a wider audience other than just hard-core library writers.
Documentation and Examples
A learn-by-example guide is available under the
lesson/directory.Demo programs are also available under the
demo/directory.Extensive documentation with examples can be found inline in the source header files.
Building Fatal
Fatal is a header only library, therefore no building is required.
Requirements
A compliant C++14 compiler. Currently tested under Clang {3.5, 3.6, 3.7, 3.8, 3.9, 5.0} and GCC {5, 6}.
There are no other external dependencies.
Links
Discussions group on Facebook
Try it online using Metashell.
Philosophy
Fatal moves fast, therefore it uses the latest and greatest in C++ standards. It aims to adopt new standard features as soon as they’re officially out, as long as they provide benefits like performance, productivity and usability improvements.
Stable versions are tagged and can be found under the list of releases. Look out for breaking changes whenever a new release is made (see below) - they should be listed on the release notes.
The
masterbranch is considered the development version of the library and experiences frequent updates. If you want the bleeding edge, that’s the branch for you. Be advised, though, that it is under heavy development: breaking changes might be introduced without notice. They’ll be tagged with the string[break]in the first line of the commit message.Installation
There’s no need to install Fatal, as long as you add its base directory to the include directories list.
For Clang and GCC, it suffices to either:
-I path/to/fatalflagCPLUS_INCLUDE_PATH=path/to/fatalBuilding Benchmarks and Unit Tests
or
Join the Fatal community
See the CONTRIBUTING file for how to help out.
License
Fatal is BSD-licensed. We also provide an additional patent grant.