pyln-client: don't crash Millisatoshi.to_approx_str() on zero
What changed, and why it matters
This commit fixes a minor bug in Core Lightning's Python client library where calling a pretty-printing method on a zero-amount payment would crash with a ValueError. It is a routine correctness fix, not a security vulnerability. There is no way to exploit it to steal funds, bypass security, or attack a node.
No security action required. Treat as a normal bug-fix release note for the Python client library.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch adds an early return in Millisatoshi.to_approx_str()’s helper round_to_n() to handle x == 0, because log10(0) is undefined and previously raised ValueError. The test test_to_approx_str_zero is also changed from an expected failure (xfail) to a normal passing test. The change is purely defensive against a crash in a formatting helper and does not alter any consensus, networking, or cryptographic code.
Changed components
contrib/pyln-client/pyln/client/lightning.pyMillisatoshi.to_approx_str()round_to_n() helperInspect captured patch +3 / −1
### contrib/pyln-client/pyln/client/lightning.py
@@ -127,6 +127,9 @@ def to_approx_str(self, digits: int = 3) -> str:
Rounds to significant `digits`. Default: 3
"""
def round_to_n(x: int, n: int) -> float:
+ # log10(0) is undefined, and zero has nothing to round anyway.
+ if x == 0:
+ return 0
return round(x, -int(floor(log10(x))) + (n - 1))
result = self.to_satoshi_str()
### contrib/pyln-client/tests/test_units.py
@@ -38,7 +38,6 @@ def test_to_approx_str():
-@pytest.mark.xfail(strict=True)
def test_to_approx_str_zero():
assert Millisatoshi(0).to_approx_str() == "0btc"
assert Millisatoshi('0sat').to_approx_str(1) == "0btc"Why this scored 20/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.