Skip to content

Fix pointer overflow in test fancy pointer - #62

Merged
nigels-com merged 1 commit into
boostorg:developfrom
nigels-com:testing-fancy-pointer-fix
Nov 30, 2026
Merged

nigels-com merged 1 commit into
boostorg:developfrom
nigels-com:testing-fancy-pointer-fix

Conversation

@nigels-com

Copy link
Copy Markdown
Collaborator

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() and sub() (base.hpp) pass a negative difference_type to the pointer's operator+ / operator-. That is valid for any random-access pointer.

The test allocator my_allocator uses its own pointer types (pointer_ and const_pointer_), and these only accepted size_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- and operator+= on my_allocator's pointer_ and const_pointer_ now take difference_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

  • Sanitizers: base_test and space_optimized_test built with clang and gcc, at C++11 and C++20, with BOOST_CB_ENABLE_DEBUG=0 and 1, under -fsanitize=address,undefined -fno-sanitize-recover=all. All pass.

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.
@nigels-com nigels-com added this to the boost 1.94.0 milestone Nov 30, 2026
@nigels-com nigels-com added the bug label Nov 30, 2026
@nigels-com

Copy link
Copy Markdown
Collaborator Author

@Flamefire Still shipping away at failing tests, if you can spare any bandwidth.

@codecov

codecov Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 50.00000% with 2 lines in your changes missing coverage. Please review.
⚠️ Please upload report for BASE (develop@c3faa18). Learn more about missing BASE report.

Files with missing lines Patch % Lines
test/common.ipp 50.00% 2 Missing ⚠️
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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@Flamefire

Copy link
Copy Markdown
Contributor

@Flamefire Still shipping away at failing tests, if you can spare any bandwidth.

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:

./boost/bind.hpp:36:1: warning: The practice of declaring the Bind placeholders (_1, _2, ...) in the global namespace is deprecated. Please use <boost/bind/bind.hpp> + using namespace boost::placeholders, or define BOOST_BIND_GLOBAL_PLACEHOLDERS to retain the current behavior. [-W#pragma-messages]

see
https://github.com/boostorg/circular_buffer/actions/runs/46381408533/job/141721497716#step:15:3767
https://github.com/boostorg/circular_buffer/blob/develop/test/bounded_buffer_comparison.cpp#L16

@nigels-com

Copy link
Copy Markdown
Collaborator Author

BTW: You may want to fix the deprecated Boost.Bind usage

Yeah. I think boost::bind goes away with C++11 migration, so it's a question of a timeframe for that.

@nigels-com
nigels-com merged commit ff8107c into boostorg:develop Nov 30, 2026
75 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants