Use of throwing methods in CETL core

Aperta Adatta ai principianti
#271 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
70/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
cpp
Ambito
embedded-iot

Direzione di ricerca

Inizia da include/cetl/pf17/variant.hpp:104, dove cetl::pf17::visit indicizza la tabella di lookup con lut.at(index). Leggi gli helper di errore detail circostanti, o l’hook di terminazione dell’issue correlata, per scegliere il percorso di controllo dei limiti, poi sostituisci .at(index) con quel controllo più operator[]. Aggiorna il commento di documentazione di chronomorphize e aggiungi un job CI che compila con -fno-exceptions e verifica che nessun simbolo std::__throw_* sia referenziato; fatto quando il reproducer linka senza helper di throw di libstdc++ e i test esistenti passano.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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.

Lingua principale
C++
Stelle
18
Fork
4
Merge medio
7g 16h
PR unite (30g)
7

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di OpenCyphal/CETL

Tutte le issue di OpenCyphal/CETL

Issue simili

Altre issue su C++

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.