Skip to content

crypto: declare the backends' canaries constinit in place of four clang-tidy suppressions - #15

Merged
pinhopro merged 1 commit into
mainfrom
crypto/canary-lint
Nov 22, 2026
Merged

pinhopro merged 1 commit into
mainfrom
crypto/canary-lint

Conversation

@pinhopro

Copy link
Copy Markdown
Contributor

What

The four link-time canaries of webcpp::crypto's backends (BearSSL, libsodium, ctaes, libsecp256k1) are declared constinit in their headers, in place of the four NOLINTNEXTLINE(bugprone-dynamic-static-initializers) that suppressed the finding. The two MSVC tests that define a canary in C++ (test/msvc/canary_two_files, test/msvc/canary_secp256k1_two_files) repeat constinit on that initialising declaration.

Why

trystero's port reported bugprone-dynamic-static-initializers at backends/secp256k1.hpp (extern "C" const char WEBCPP_CRYPTO_DETAIL_SECP256K1_CANARY;) in its Emscripten units: its pinned crypto predates the suppression main gained in 9c9bbc2. The cause is the declaration itself: a namespace-scope variable of static storage in a header, with no initialiser in any C++ translation unit, which the check cannot tell from one that is dynamically initialised (it fires on every global-storage variable whose initialiser it cannot evaluate, a missing one included). Its definition is src/canary_<name>.c's = 0, a C constant: its initialisation is static, never dynamic.

Rulings

  • Fix at the cause, not a suppression. constinit on the declaration states the fact the check asks about (the variable is constant-initialised), and the check honours it. It removes all four suppressions, not only libsecp256k1's: the four canaries share the one declaration form, and leaving three suppressed beside one stated would make them differ for no reason.
  • constinit across languages. [dcl.constinit] asks that the initialising declaration also carry constinit. Where that declaration is C++ (the two MSVC tests), it does. Where it is src/canary_<name>.c, C has no constinit and no dynamic initialisation at all, so the promise holds by construction.
  • Considered and rejected: keeping the NOLINT (the finding is a false positive, but a suppression is the last resort when the code cannot say why, and here constinit says it); turning the canaries into functions (would change the C side, the link-time contract and the guide, for no gain over a volatile byte load).
  • Scope. webcpp::hash's canaries carry the same suppression form; they belong to another stream and are left as they are here.

Verified

  • Reproduced: clang-tidy (wasi-sdk 23.1) on test/secp256k1_keys_test.cpp with em++'s --cflags reports the finding at the declaration once the suppression is removed; with constinit, it is clean.
  • crypto's tests and examples, b2 -a: native clang (411 tests passed), Emscripten (289) and wasip2 with wasmtime (369), every secp256k1 test and every canary test among them.
  • The full macOS lint: clean (1266 translation units). The Linux lint (the CI's Lint job in Docker, changed mode): clean, 154 units analysed.
  • MSVC 14.3 (real cl.exe under Wine), libs/crypto/test: every canary test passes, the two that define a canary with constinit among them; the only failures are the lane's known Boost.JSON silent stops of translation units without exceptions (no diagnostic), one of which, async_p256_test without exceptions, the lane now lists.

…-dynamic-static-initializers reads, in place of four suppressions

Each canary's declaration (BearSSL, libsodium, ctaes, libsecp256k1) is a namespace-scope
variable without an initialiser, which bugprone-dynamic-static-initializers reports in a
header as one that may be dynamically initialised. Its definition, src/canary_<name>.c, is
a C constant: its initialisation is static. constinit on the declaration states that fact,
which the check reads, so the four NOLINTNEXTLINE are gone. The two MSVC tests that define a
canary in C++ repeat constinit on that initialising declaration, as the standard asks.
@pinhopro
pinhopro merged commit 171e091 into main Nov 22, 2026
1 of 3 checks passed
@pinhopro
pinhopro deleted the crypto/canary-lint branch November 22, 2026 17:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant