Use of throwing methods in CETL core

Open Beginner friendly
#271 0 comments 0 reactions 0 assignees View on GitHub

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

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.

Dominant language
C++
Stars
18
Forks
4
Avg merge
7d 16h
Merged PRs (30d)
7

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from OpenCyphal/CETL

All issues in OpenCyphal/CETL

Similar issues

More C++ issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.