undefined behavior in do_fill_uninitialized_memory()
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 52/100
Research direction
Start in boost/circular_buffer/debug.h, at do_fill_uninitialized_memory(), and review the copy-construction path for an empty circular_buffer. Reproduce the example with GCC 9.1, -fsanitize=undefined, and -DBOOST_CB_ENABLE_DEBUG=1; done means the empty-copy case no longer reports the null-pointer error while nonzero fills remain checked.
Written by the indexing model from the issue text.
Description
Using gcc 9.1 with -fsanitize=undefined and -DBOOST_CB_ENABLE_DEBUG=1, the following code results in a ubsan error: "boost/circular_buffer/debug.hpp:37:16: runtime error: null pointer passed as argument 1, which is declared to never be null".
using Q = boost::circular_buffer<int>;
Q q;
const Q q2(q);
The ubsan error is a result of the following code in circular_buffer/debug.h:
template <class T>
inline void do_fill_uninitialized_memory(T* data, std::size_t size_in_bytes) BOOST_NOEXCEPT {
std::memset(static_cast<void*>(data), UNINITIALIZED, size_in_bytes);
}
During copy construction, the function gets called with data == nullptr and size_in_bytes == 0. I believe that passing a null pointer to memset is technically undefined behavior even if the size is 0.
Changing the above function as follows avoids the ubsan error:
template <class T>
inline void do_fill_uninitialized_memory(T* data, std::size_t size_in_bytes) BOOST_NOEXCEPT {
if (size_in_bytes != 0u) {
std::memset(static_cast<void*>(data), UNINITIALIZED, size_in_bytes);
}
}
I had originally written it to check for data != nullptr, but since this is debug code I thought it seemed desirable to know if the function is ever called with data == null and size_in_bytes != 0. In any case, either way will prevent the ubsan error.
- Dominant language
- C++
- Stars
- 65
- Forks
- 64
- PR merge metrics
- No merged PRs in 30d
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from boostorg/circular_buffer
-
Difficulty 3/5 1-2 days Newbie friendliness 25/100
boostorg/circular_buffer#53 ·
-
Difficulty 3/5 1-2 days Newbie friendliness 48/100
boostorg/circular_buffer#51 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 45/100
boostorg/circular_buffer#48 ·
-
Difficulty 4/5 3-5 days Newbie friendliness 25/100
boostorg/circular_buffer#47 ·
-
enhancement
Difficulty 2/5 1-3 hours Newbie friendliness 35/100
boostorg/circular_buffer#46 · 4 comments ·
All issues in boostorg/circular_buffer
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
google/libultrahdr#485 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
godotengine/godot#123776 ·
-
bug
Difficulty 1/5 Under an hour Newbie friendliness 60/100
-
good first issue
Difficulty 1/5 Under an hour Newbie friendliness 90/100
-
good first issue
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
ros2/common_interfaces#344 ·