Repository navigation
Avoid UB in debug code - #50
Conversation
|
There was a similar fix merged this year: 5d8bebb7614157768d49372618ebbe3049375820 My initial thought is that there always ought to be a
|
|
See also: PR #11 |
|
Claude here had more to say about it. Bug: debug mode calls
|
| Component | boost/circular_buffer/debug.hpp: cb_details::do_fill_uninitialized_memory(); called from base.hpp: allocate() |
| Severity | Low: undefined behaviour by the letter of the standard, harmless on mainstream implementations. Breaks UBSan runs. |
| Affects | Debug mode only (BOOST_CB_ENABLE_DEBUG=1). Present since the debug fill was introduced (fc1d341, 2013). Reproduced on develop at 5d8bebb and on the C++11 working tree. |
| Status | Open |
Summary
In debug mode, newly allocated storage is filled with 0xcc so that uninitialized elements
are easy to spot. When the requested capacity is 0, allocate() returns a null pointer and
still calls memset(nullptr, 0xcc, 0).
Passing a null pointer to memset is undefined behaviour even when the size is 0.
UndefinedBehaviorSanitizer reports it, so anyone running their tests with
-fsanitize=undefined and BOOST_CB_ENABLE_DEBUG=1 gets a failure from inside the library.
The library's own test suite trips over it.
Reproduction
#define BOOST_CB_ENABLE_DEBUG 1
#include <boost/circular_buffer.hpp>
int main() {
boost::circular_buffer<int> cb(0);
}$ g++ -std=c++11 -fsanitize=undefined repro.cpp && ./a.out
include/boost/circular_buffer/debug.hpp:34:16: runtime error: null pointer passed as argument 1, which is declared to never be null
Measured triggers, with debug mode on:
| Code | UBSan reports |
|---|---|
circular_buffer<int> cb; (default constructor, no allocation) |
0 |
circular_buffer<int> cb(0); |
1 |
circular_buffer<int> cb(4); cb.set_capacity(0); |
1 |
circular_buffer_space_optimized<int> cb(10); (starts with internal capacity 0) |
1 |
| any of the above with debug mode off | 0 |
The space-optimized buffer hits this routinely, because it often starts with internal
capacity 0, and shrinking can reach 0 again.
Root cause
base.hpp, allocate():
#if BOOST_CB_ENABLE_DEBUG
pointer p = (n == 0) ? nullptr : alloc().allocate(n);
cb_details::do_fill_uninitialized_memory(p, sizeof(value_type) * n); // p may be null, n may be 0
return p;
#elsedebug.hpp:
template <class T>
inline void do_fill_uninitialized_memory(T* data, std::size_t size_in_bytes) noexcept {
#if defined(__GNUC__) && __GNUC__ < 5
// Avoid warning for calling memset with a constant size of zero
if( size_in_bytes )
#endif
std::memset(static_cast<void*>(data), UNINITIALIZED, size_in_bytes);
}The if (size_in_bytes) guard already exists, but only for GCC older than 5, where it was
added in 5d8bebb to silence a warning. On every other compiler the call goes through with a
null pointer.
Fix
Make the guard unconditional, and test the pointer instead of the size. That covers both
the original GCC 4.x warning and this bug:
template <class T>
inline void do_fill_uninitialized_memory(T* data, std::size_t size_in_bytes) noexcept {
if (data != nullptr)
std::memset(static_cast<void*>(data), UNINITIALIZED, size_in_bytes);
}Alternatively, guard on size_in_bytes != 0, or on both. A null data with a non-zero size
never happens, because allocate() returns null only for n == 0. Testing the pointer
states the actual precondition of memset.
Check that GCC 4.8/4.9 (still in the CI matrix) don't bring back the warning 5d8bebb fixed.
With a run-time check on data they shouldn't, since the size is no longer a constant zero
at the call.
The other overload, do_fill_uninitialized_memory(T&, std::size_t) for fancy pointers, does
nothing, so it is not affected.
Tests to add
- Run the existing test suite under UBSan in CI with
BOOST_CB_ENABLE_DEBUG=1and without
-fno-sanitize=nonnull-attribute. Today that would fail inbase_testand
space_optimized_test. - No new test code is needed:
circular_buffer<int> cb(0)and the space-optimized tests
already hit this path. It just needs a sanitizer build to observe it. - Once fixed, remove
-fno-sanitize=nonnull-attributefrom any local or CI sanitizer
commands that were added to work around this.
|
yes #11 is the exact same fix. Peters recent fix only avoids the compile-time warning, not the UBSan error which is correct as stated by the standard although likely not an issue in practice. On the other hand a sufficiently "smart" compiler may trace the nullptr, determine the code as unreachable and erase the whole program |
Can you clarify that particular point for me? |
UB gives the compiler the leeway to assume this does not happen and optimize accordingly. A classic example is something like: Dereferencing a nullptr is UB so the The This could kick in here: You
IMO only for the UB issue, not the intention. To me this code now reads: "If we have elements, reset them" |
|
I do prefer the more simplistic reasoning that |
`std::memset` calls with a nullptr argument are flagged as UB by GCC `-fsanitize=undefined`, so check the size first Fixes boostorg#39
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## develop #50 +/- ##
==========================================
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:
|
A nullptr implies zero elements, while the other way may not be true. So we should define the pre-condition of the function and have an assert. With the else-branch being logically unreachable It's your call as the maintainer, I'm fine either way. Shall I change to the data check? BTW: checking the call site we could fix it there too. It already has the check which means the check for size_in_bytes would be free: |
std::memsetcalls with a nullptr argument are flagged as UB by GCC-fsanitize=undefined, so check the size firstSee https://godbolt.org/z/Kjch8dd1d for a simple example showing the diagnostics
Fixes #39