Use of throwing methods in CETL core

未关闭 适合新手
#271 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
70/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
cpp
领域
embedded-iot

调研方向

从 include/cetl/pf17/variant.hpp:104 开始,其中 cetl::pf17::visit 用 lut.at(index) 对查找表进行索引。阅读周围的 detail 失败辅助函数,或配套 issue 中的终止 hook,以选择边界检查路径,然后把 .at(index) 替换为该检查加 operator[]。更新 chronomorphize 的 doc 注释,并添加一个 CI job,用 -fno-exceptions 编译并验证没有引用任何 std::__throw_* 符号;当复现程序在不需要 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 小时
30 天内合并 PR
7

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

OpenCyphal/CETL 的其他 Issue

查看 OpenCyphal/CETL 的全部 Issue

相似的 Issue

更多 C++ Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。