Constructing a protocol_view from a valueless protocol is undiagnosed, and calls through it hit a null function pointer

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

Maintainer thường phản hồi trong vòng 1 ngày

@jbcoe đang làm issue này rồi.

Từ ngày 4/9/2026.

  • #305 của @jbcoe — đã đóng, không merge

Đánh giá

Issue này chưa được đánh giá.

Mô tả

A moved-from protocol has object_ == nullptr and vtable_ == &null_vtable,
whose member entries are null function pointers. Calling a member function on
it directly is caught in debug builds: method_thunk::operator() asserts
!valueless_after_move() before dispatching.

Since #244 and #272 a protocol_view<I> or protocol_view<const I> constructs
implicitly from a protocol<I, A>&, copying object_ and vtable_. Nothing
checks the source, so a valueless protocol converts silently, and the thunk
assert is if constexpr (is_protocol_v<ProtocolType>), so it is compiled out
for views. A member call through such a view calls a null function pointer with
no diagnostic, even in a debug build.

protocol<Shape> a{Circle{}};
protocol<Shape> b = std::move(a);   // a is valueless
protocol_view<const Shape> v = a;   // accepted; comment says "Precondition: p is not valueless"
v.area();                           // release: null call; debug: also null call

The from-protocol constructors document the precondition in a comment
(protocol.hh, both protocol_view specializations) but do not enforce it.

Suggested fix, small enough for one PR:

  • assert(!p.valueless_after_move()) in the two from-protocol constructors.
  • Drop the is_protocol_v gate in method_thunk and assert object_ != nullptr
    instead, which covers protocol and view alike. A view's object_ is otherwise
    never null.
  • A death test for each path.

#208 settled the direct-call case as a precondition violation diagnosed by an
assert in debug builds; this extends the same contract to views. Raised in the
review of #272, where it was agreed to be out of scope there but worth fixing.

Ngôn ngữ chính
C++
Star
12
Fork
4
Merge trung bình
1 ngày 11 giờ
Pull request đã merge (30 ngày)
15

Chuẩn bị môi trường

Mở trong Codespaces

Khởi chạy dev container của dự án ngay trên trình duyệt, bằng tài khoản GitHub của bạn.

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 jbcoe/cc-protocol

Tất cả issue của jbcoe/cc-protocol

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.