Repository navigation
Fix pointer overflow in test fancy pointer - #62
Conversation
my_allocator's pointer types took size_type in operator+, operator- and operator+=. circular_buffer::add() and sub() pass a negative difference_type when an index wraps around the buffer end, which was converted to a huge unsigned value. The pointer arithmetic then overflowed, which UBSan reports as pointer-overflow at common.ipp:183 in the sanitizer CI jobs. Take difference_type, as a random-access pointer should. Also make const_pointer_::operator+= return *this. It returned the raw pointer and would not have compiled if it had been used.
|
@Flamefire Still shipping away at failing tests, if you can spare any bandwidth. |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## develop #62 +/- ##
==========================================
Coverage ? 99.43%
==========================================
Files ? 10
Lines ? 4578
Branches ? 3358
==========================================
Hits ? 4552
Misses ? 23
Partials ? 3 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
The only failing tests seem to be the UBSAN failures you are fixing here. Or which ones do you refer to? BTW: You may want to fix the deprecated Boost.Bind usage in one test to reduce the CI noise:
see |
Yeah. I think boost::bind goes away with C++11 migration, so it's a question of a timeframe for that. |
The sanitizer CI jobs (for example clang-12, ASan+UBSan, C++11,
base_test_dbg) fail with:libs/circular_buffer/test/common.ipp:183:67: runtime error: addition of unsigned offset to 0x607000000178 overflowed to 0x607000000170
SUMMARY: UndefinedBehaviorSanitizer: pointer-overflow libs/circular_buffer/test/common.ipp:183:67
Cause
When an index wraps past the end of the buffer,
circular_buffer::add()andsub()(base.hpp) pass a negativedifference_typeto the pointer'soperator+/operator-. That is valid for any random-access pointer.The test allocator
my_allocatoruses its own pointer types (pointer_andconst_pointer_), and these only acceptedsize_type. The negative offset was converted to a huge unsigned value, so the pointer arithmetic inside the test overflowed. It wrapped around to the correct address by accident, so the bug went unnoticed until UBSan ran in CI.Stack (local reproduction):
my_allocator::pointer_::operator+(unsigned long) test/common.ipp:183
circular_buffer<...>::add<pointer_>(pointer_, long) base.hpp:2365
circular_buffer<...>::operator[](unsigned long) base.hpp:388
generic_test<my_allocator> test/common.ipp:46
allocator_test test/common.ipp:257
Changes
Test-only; the library is unchanged.
operator+,operator-andoperator+=onmy_allocator'spointer_andconst_pointer_now takedifference_type, as a random-access pointer should.const_pointer_::operator+=now returns*this. It used to return the raw pointer and would not have compiled if anything had called it.Testing
base_testandspace_optimized_testbuilt with clang and gcc, at C++11 and C++20, withBOOST_CB_ENABLE_DEBUG=0and1, under-fsanitize=address,undefined -fno-sanitize-recover=all. All pass.