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

オープン
#284 コメント 0 件 リアクション 0 件 担当者 1 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

@jbcoe がすでに取り組んでいます。

2026年9月4日 から。

  • #305 @jbcoe による — マージされずにクローズ

評価

この issue はまだ評価されていません。

説明

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.

主要言語
C++
スター
12
フォーク
4
平均マージ
1日 11時間
マージ済み PR(30日)
15

環境構築

Codespaces で開く

このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

jbcoe/cc-protocol のほかの issue

jbcoe/cc-protocol の issue をすべて見る

似ている issue

C++ の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。