devtools: Add amount access check to pre-commit.
What changed, and why it matters
This commit adds a new automated code-quality check that runs before code is committed. It scans C code for direct access to certain monetary fields (like amount_msat and amount_sat) that could lead to arithmetic overflow if handled incorrectly. It is purely a defensive development tool and does not change any runtime behavior or fix a specific bug.
No action required. This is a development tooling change. Ensure the regex correctly covers intended patterns and does not produce false positives that frustrate developers.
Security signals we found
Preventive coding-standard enforcement for monetary types
Aims to reduce risk of integer overflow in amount handling
Evidence from the diff
The change adds a local pre-commit hook in .pre-commit-config.yaml. The hook uses pygrep to detect C code patterns that directly access millistoshis/satoshis members or cast to struct amount_msat/amount_sat outside of tests and the common/amount module. This reimplements the existing make check-amount-access Makefile target as a pre-commit hook. It is a preventive measure, not a patch for a known vulnerability.
Changed components
.pre-commit-config.yamlInspect captured patch +11 / −0
diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml
index 7c8e13d8..b9a48260 100644
--- a/.pre-commit-config.yaml
+++ b/.pre-commit-config.yaml
@@ -17,3 +17,14 @@ repos:
hooks:
- id: shellcheck
args: [ -fgcc ]
+
+- repo: local
+ hooks:
+ # Reimplementation of `make check-amount-access` for pygrep.
+ - id: check-amount-access
+ name: Check amount_msat and amount_sat members are not accessed directly
+ description: "Don't access amount_msat and amount_sat members directly without a good reason since it risks overflow."
+ language: pygrep
+ entry: (->|\.)(milli)?satoshis(?!.*\/\*\ Raw:)|(?<!sizeof)\(struct\ amount_(m)?sat\)
+ types: [ c ]
+ exclude: common/amount|.*/test/.*
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.