Use of throwing methods in CETL core
还没有人认领这个 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 内容生成。
描述
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 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
OpenCyphal/CETL 的其他 Issue
-
accepted
难度 3/5 1-2 天 新手友好度 70/100
OpenCyphal/CETL#270 ·
-
documentation
难度 3/5 1-2 天 新手友好度 45/100
OpenCyphal/CETL#262 ·
-
under consideration
难度 4/5 3-5 天 新手友好度 35/100
OpenCyphal/CETL#252 ·
-
tracking
难度 3/5 1-2 天 新手友好度 35/100
OpenCyphal/CETL#194 ·
-
under consideration
难度 5/5 一周以上 新手友好度 25/100
OpenCyphal/CETL#176 · 1 条评论 ·
相似的 Issue
-
good first issue hardware-report help wanted
难度 1/5 1 小时以内 新手友好度 66/100
-
难度 2/5 1-3 小时 新手友好度 75/100
-
launcher: settings are never saved on macOS — QSettings SystemScope points to /Library/Preferences未关闭
难度 1/5 1 小时以内 新手友好度 85/100
维护者通常 1 天内回复
-
documentation ready
难度 1/5 1 小时以内 新手友好度 80/100
MartinPTielemans/ilo#3806 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 80/100