Use of throwing methods in CETL core
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 70/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Active
- Tech stack
- cpp
- Domain
- embedded-iot
Research direction
Start at include/cetl/pf17/variant.hpp:104, where cetl::pf17::visit indexes the lookup table with lut.at(index). Read the surrounding detail failure helpers, or the termination hook from the companion issue, to choose the bounds-check path, then replace .at(index) with that check plus operator[]. Update the chronomorphize doc comment and add a CI job that compiles with -fno-exceptions and verifies no std::__throw_* symbols are referenced; done when the reproducer links without libstdc++ throw helpers and existing tests pass.
Written by the indexing model from the issue text.
Description
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.
- Dominant language
- C++
- Stars
- 18
- Forks
- 4
- Avg merge
- 7d 16h
- Merged PRs (30d)
- 7
Getting set up
- No Dockerfile or Docker Compose file
- No pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from OpenCyphal/CETL
-
accepted
Difficulty 3/5 1-2 days Newbie friendliness 70/100
OpenCyphal/CETL#270 ·
-
documentation
Difficulty 3/5 1-2 days Newbie friendliness 45/100
OpenCyphal/CETL#262 ·
-
under consideration
Difficulty 4/5 3-5 days Newbie friendliness 35/100
OpenCyphal/CETL#252 ·
-
tracking
Difficulty 3/5 1-2 days Newbie friendliness 35/100
OpenCyphal/CETL#194 ·
-
under consideration
Difficulty 5/5 Over a week Newbie friendliness 25/100
OpenCyphal/CETL#176 · 1 comment ·
Similar issues
-
good first issue hardware-report help wanted
Difficulty 1/5 Under an hour Newbie friendliness 66/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
-
launcher: settings are never saved on macOS — QSettings SystemScope points to /Library/PreferencesOpen
Difficulty 1/5 Under an hour Newbie friendliness 85/100
Maintainers usually reply within 1 day
-
documentation ready
Difficulty 1/5 Under an hour Newbie friendliness 80/100
MartinPTielemans/ilo#3806 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 80/100