AI-generated analysisPublished automatically and not human-verified. Validated context appears in community notes below.
← Watch feed
Low 47 Bitcoin

Merge bitcoin-core/secp256k1#2007: Add a self-check for miscompiled `memcmp` and remove `secp256k1_memcmp_var`

Public commit record

What the developer wrote

Authored by merge-script

98/100 · Strong
Merge bitcoin-core/secp256k1#2007: Add a self-check for miscompiled `memcmp` and remove `secp256k1_memcmp_var`

947bd145c13e674f00a7c614313efb0d3e902d71 refactor: Remove unused `secp256k1_memcmp_var()` (Hennadii Stepanov)
8e3e9d772229e094f6957c78e7cad67c7b960d5c Replace `secp256k1_memcmp_var()` with `memcmp()` (Hennadii Stepanov)
c7edddcde829d3d1cd051e37c5d1b75cde8fd4a6 selftest: Add check for miscompiled `memcmp` (Hennadii Stepanov)

Pull request description:

`secp256k1_memcmp_var` was introduced in 6173839c90553385171d560be8a17cbe167e3bef to avoid [GCC bug 95189](https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95189), which may miscompile [`memcmp`](https://en.cppreference.com/c/string/byte/memcmp). This PR follows the suggestion in https://github.com/bitcoin-core/secp256k1/issues/823#issuecomment-4485072400 to get rid of the function and to guard against affected compilers with a self-check instead.

Closes #823.

ACKs for top commit:
Yudis-bit:
ACK 947bd145c13e674f00a7c614313efb0d3e902d71
real-or-random:
ACK 947bd145c13e674f00a7c614313efb0d3e902d71
theStack:
ACK 947bd145c13e674f00a7c614313efb0d3e902d71

Tree-SHA512: 2bea33d503abcc94d747a96485ced0a80588a93b1688880737916d98653d02a6f083f41f4ec4a5ede58fe5addab3fb18de2fe491eaf7e6c55a27be2853a7a988
✓ Specific, descriptive subject✓ Names a concrete action or component✓ Provides detailed explanatory context✓ Explains rationale or failure mode✓ Links an issue, advisory, or supporting reference✓ Names security-relevant behavior explicitly
The short version

What changed, and why it matters

This commit removes a custom safe-comparison helper called secp256k1_memcmp_var and switches the codebase back to the standard C memcmp. It also adds a startup self-test that detects compilers affected by a known GCC bug that could miscompile memcmp when comparing arrays that contain zero bytes. The change is a cleanup and hardening measure: it relies on the compiler's memcmp being correct, but adds a guard so the library refuses to run on a compiler that mishandles it.

Recommended action

Ensure builds use a compiler not affected by GCC bug 95189, or build with -fno-builtin-memcmp if using an affected GCC version. Run the library's self-tests (they now include a memcmp miscompilation check) before deployment. Reviewers should verify that all replaced comparisons are safe to expose to standard memcmp semantics and that the self-test reliably fails on the affected compiler versions.

Security signals we found

01

Removal of a workaround for a known compiler bug (GCC 95189)

02

Addition of a runtime self-test to detect the same compiler bug

03

Wide replacement of custom comparison helper with standard memcmp across cryptographic code paths

04

Magic-number and public-key comparison paths now depend on correct memcmp behavior

05

No direct vulnerability fix; this is a defensive refactor and build-time guard

Risk score

Why this scored 47/100

Our methodology →
Potential impact 12/30
Exploitability 8/25
Stealth signal 6/15
Affected reach 10/15
Confidence 7/10
Evidence quality 4/5
Human-validated context

Community notes

Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.

No validated notes yet.

The AI analysis stands alone for now. Submit a note if you can add evidence or important context.