fixup! pyln-client: don't crash Millisatoshi.to_approx_str() on zero
What changed, and why it matters
This is a small bug fix in a Python helper library used by Core Lightning. The Millisatoshi.to_approx_str() method previously crashed with a math error when converting a zero amount to a human-readable string. The patch makes zero return "0btc" instead of raising an exception. It is a correctness fix, not a security vulnerability.
No security action required. Treat as a normal bug fix and include in regular release testing.
Security signals we found
No security-relevant signals present
Fix is a routine correctness/robustness improvement
Evidence from the diff
The change moves the zero check from inside round_to_n() to the start of to_approx_str() and returns “0btc” directly. Previously, calling to_approx_str() on a zero Millisatoshi object would cause log10(0), which is undefined, raising a ValueError. The fix prevents the crash in string formatting of zero amounts.
Changed components
contrib/pyln-client/pyln/client/lightning.pyMillisatoshi.to_approx_str() methodInspect captured patch +4 / −3
### contrib/pyln-client/pyln/client/lightning.py
@@ -126,10 +126,11 @@ def to_approx_str(self, digits: int = 3) -> str:
Rounds to significant `digits`. Default: 3
"""
+ # log10(0) is undefined, and zero has nothing to round anyway.
+ if self.millisatoshis == 0:
+ return "0btc"
+
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()
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.