Hacktoberfest 2026: những issue maintainer đã đánh dấu cho tháng Mười, đang mở và phù hợp người mới. Xem issue Hacktoberfest

undefined behavior in do_fill_uninitialized_memory()

Đang mở
#39 1 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
52/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Đình trệ
Công nghệ
cpp
Lĩnh vực
backend

Hướng nghiên cứu

Bắt đầu trong boost/circular_buffer/debug.h, tại do_fill_uninitialized_memory(), và xem xét đường đi của thao tác tạo bản sao đối với một circular_buffer rỗng. Tái hiện ví dụ với GCC 9.1, -fsanitize=undefined và -DBOOST_CB_ENABLE_DEBUG=1; công việc được hoàn tất khi trường hợp sao chép rỗng không còn báo lỗi con trỏ null, trong khi các lần fill có giá trị khác không vẫn được kiểm tra.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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.

Ngôn ngữ chính
C++
Star
65
Fork
64
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Hướng dẫn đóng góp

Chưa lập chỉ mục được hướng dẫn đóng góp cho kho mã nguồn này

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của boostorg/circular_buffer

Tất cả issue của boostorg/circular_buffer

Issue tương tự

Thêm issue về C++

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.