CATCH_TRANSLATE_EXCEPTION violates clang-tidy's cert-err58-cpp check
- Dominant language
- C++
- Stars
- 21.5k
- Forks
- 3.5k
- Avg merge
- 3d 16h
- Merged PRs (30d)
- 2
Description
Thanks very much for all work on Catch2.
Checking this code:
~~~cpp
#include
#include
#define CATCH_CONFIG_MAIN
#include
CATCH_TRANSLATE_EXCEPTION( const ::std::invalid_argument & ) {
return std::string{ "Some invalid_argument was caught here" };
}
~~~
…with `clang-tidy -checks=cert-err58-cpp -- -isystem -fmacro-backtrace-limit=0` gives:
~~~no-highlight
23 warnings generated.
:7:1: warning: initialization of 'catch_internal_ExceptionRegistrar1' with static storage duration may throw an exception that cannot be caught [cert-err58-cpp]
CATCH_TRANSLATE_EXCEPTION( const ::std::invalid_argument & ) {
^
/catch.hpp:17396:48: note: expanded from macro 'CATCH_TRANSLATE_EXCEPTION'
#define CATCH_TRANSLATE_EXCEPTION( signature ) INTERNAL_CATCH_TRANSLATE_EXCEPTION( signature )
^
/catch.hpp:3025:57: note: expanded from macro 'INTERNAL_CATCH_TRANSLATE_EXCEPTION'
#define INTERNAL_CATCH_TRANSLATE_EXCEPTION( signature ) INTERNAL_CATCH_TRANSLATE_EXCEPTION2( INTERNAL_CATCH_UNIQUE_NAME( catch_internal_ExceptionTranslator ), signature )
^
/catch.hpp:3021:52: note: expanded from macro 'INTERNAL_CATCH_TRANSLATE_EXCEPTION2'
namespace{ Catch::ExceptionTranslatorRegistrar INTERNAL_CATCH_UNIQUE_NAME( catch_internal_ExceptionRegistrar )( &translatorName ); } \
^
note: (skipping 1 expansions in backtrace; use -fmacro-backtrace-limit=0 to see all)
/catch.hpp:449:55: note: expanded from macro 'INTERNAL_CATCH_UNIQUE_NAME_LINE'
#define INTERNAL_CATCH_UNIQUE_NAME_LINE( name, line ) INTERNAL_CATCH_UNIQUE_NAME_LINE2( name, line )
^
/catch.hpp:448:56: note: expanded from macro 'INTERNAL_CATCH_UNIQUE_NAME_LINE2'
#define INTERNAL_CATCH_UNIQUE_NAME_LINE2( name, line ) name##line
^
note: expanded from here
/catch.hpp:3009:9: note: possibly throwing constructor declared here
ExceptionTranslatorRegistrar( std::string(*translateFunction)( T& ) ) {
^
Suppressed 22 warnings (22 in non-user code).
Use -header-filter=.* to display errors from all non-system headers. Use -system-headers to display errors from system headers as well.
~~~
(it seems my `-fmacro-backtrace-limit=0` didn't work :unamused:)
This can be seen on Godbolt : https://godbolt.org/z/38M3i4
The check is documented [here](https://clang.llvm.org/extra/clang-tidy/checks/cert-err58-cpp.html) and it refers to [this CERT rule](https://wiki.sei.cmu.edu/confluence/display/cplusplus/ERR58-CPP.+Handle+all+exceptions+thrown+before+main()+begins+executing)
Perhaps you consider this unavoidable or just not worth the effort to address. But I think it would be good to analyse whether judicious use of `catch` and `noexcept` can avoid this security-based warning.
Thanks again.
Contributor guide
Research direction
Start in catch.hpp at the CATCH_TRANSLATE_EXCEPTION and INTERNAL_CATCH_TRANSLATE_EXCEPTION macros, then read ExceptionTranslatorRegistrar around the constructor cited by clang-tidy. Reproduce the warning with the provided sample and clang-tidy cert-err58-cpp. Done means the exception translator remains usable without triggering this warning.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp
- Domain
- security, testing-qa
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100