Use of throwing methods in CETL core

Abierto Apto para principiantes
#271 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
2/5
Tiempo estimado
1-3 horas
Aptitud para principiantes
70/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
cpp
Área
embedded-iot

Línea de trabajo

Empieza en include/cetl/pf17/variant.hpp:104, donde cetl::pf17::visit indexa la tabla de búsqueda con lut.at(index). Lee los ayudantes de fallo de detail circundantes, o el hook de terminación del issue complementario, para elegir la ruta de comprobación de límites, y luego reemplaza .at(index) por esa comprobación más operator[]. Actualiza el comentario de documentación de chronomorphize y añade un job de CI que compile con -fno-exceptions y verifique que no se referencien símbolos std::__throw_*; está hecho cuando el reproductor enlace sin los ayudantes de throw de libstdc++ y pasen las pruebas existentes.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

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.

Lenguaje dominante
C++
Estrellas
18
Forks
4
Merge medio
7 d 16 h
PR fusionados (30 d)
7

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de OpenCyphal/CETL

Todos los issues de OpenCyphal/CETL

Issues similares

Más issues de C++

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.