Skip to content

undefined behavior in do_fill_uninitialized_memory() #39

Done

Description

@usefulcat

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.

Activity

  1. changed the title [-]UB sanitizer error in do_fill_uninitialized_memory()[/-] [+]undefined behavior in do_fill_uninitialized_memory()[/+] on Feb 9, 2021
  2. added a commit that references this issue on Sep 17, 2024
    598d346
  3. usefulcat commented on Mar 10, 2026

    @usefulcat
    Author

    This issue makes it difficult or impossible to use ubsan with boost::circular_buffer (impossible with -fsanitize-undefined-trap-on-error). It has been open for 5 years, even though it is trivial to fix. Probably the only way it could be more trivial is if it were a change to comments or documentation.

    It seems like this project is no longer maintained. Anyone in a position to know care to comment?

  4. nigels-com commented on Nov 28, 2026

    @nigels-com
    Collaborator

    I'm stepping up to be a maintainer for boost::circular_buffer.
    Just getting familiar at this stage, and triaging the tickets.
    Also made a note on PR #50.
    Thanks for the report.

  5. nigels-com commented on Nov 29, 2026

    @nigels-com
    Collaborator

    See also PR #11

  6. added a commit that references this issue on Nov 29, 2026
    ff35f82
  7. usefulcat commented on Nov 29, 2026

    @usefulcat
    Author

    I'm stepping up to be a maintainer for boost::circular_buffer.

    This is excellent news, thanks for taking this on!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions