Use of throwing methods in CETL core

オープン 初心者向け
#271 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
70/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
cpp
領域
embedded-iot

調査の方向性

include/cetl/pf17/variant.hpp:104 から始めてください。ここでは cetl::pf17::visit が lut.at(index) でルックアップテーブルをインデックスします。周囲の detail 失敗ヘルパー、または関連 issue の終了フックを読み、境界チェックの経路を選んでから、.at(index) をそのチェックと operator[] に置き換えてください。chronomorphize の doc コメントを更新し、-fno-exceptions でコンパイルし、std::__throw_* シンボルが参照されていないことを検証する CI ジョブを追加してください。再現プログラムが libstdc++ の throw ヘルパーなしでリンクし、既存のテストが通れば完了です。

索引モデルが issue の本文から書いたものです。

説明

accepted

cetl::pf17::visit (via chronomorphize) calls std::array::at(), which makes CETL depend on libstdc++'s out-of-line throw helpers even when the user builds with -fno-exceptions. On targets that link no C++ runtime library (for example NuttX-based flight controllers), this produces an unresolvable reference to std::__throw_out_of_range_fmt.

Where

include/cetl/pf17/variant.hpp:104 (main @ bf1e5f3, also v2.1.0):

return lut.at(index)(std::forward<Fun>(fun), std::forward<Args>(ar)...);

Why -fno-exceptions doesn't help

In a hosted libstdc++, std::array::at() calls std::__throw_out_of_range_fmt(), declared in bits/functexcept.h and defined in libstdc++ itself. The header calls it unconditionally; what it does (throw or abort) is decided by how libstdc++ was built, not by the user's flags. Only a freestanding build (_GLIBCXX_HOSTED == 0) makes these helpers inline std::__terminate() calls.

Minimal reproduction with Arm GNU Toolchain 13.3.rel1:

#include <array>
int f(unsigned i) { static constexpr std::array<int, 3> a{1, 2, 3}; return a.at(i); }
Flags Undefined reference
-std=c++23 -Os -mcpu=cortex-m7 -mthumb -fno-exceptions -fno-rtti std::__throw_out_of_range_fmt(char const*, ...)
same + -ffreestanding std::terminate()

Any use of cetl::pf17::visit therefore pulls in the helper at -Os as well as -O0.

Proposal

Replace .at() with an explicit bounds check that uses CETL's own failure path, then index with operator[]:

detail::bad_access_unless(index < sizeof...(Is));   // or the termination hook from the companion issue
return lut[index](std::forward<Fun>(fun), std::forward<Args>(ar)...);

This keeps the documented behaviour ("if index >= N, behaves like std::array<...,N>::at(index)") for exception-enabled builds if bad_access_unless throws, and removes the libstdc++ dependency for -fno-exceptions builds. The doc comment on chronomorphize would need updating to match.

It would also help to add a CI check that compiles the headers with -fno-exceptions and asserts the object files reference no std::__throw_* symbols, so this doesn't regress.

Patch

A patch implementing this against PR #266 (f4d8b79) is attached.

主要言語
C++
スター
18
フォーク
4
平均マージ
7日 16時間
マージ済み PR(30日)
7

環境構築

はじめの一歩

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

OpenCyphal/CETL のほかの issue

OpenCyphal/CETL の issue をすべて見る

似ている issue

C++ の issue をもっと見る

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

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