Skip to content

Commit c218818

Browse files
authored
Merge pull request #4172 from TheBlueMatt/2025-10-duration-deser-panic
Fix panic when deserializing `Duration`
2 parents 6941b0a + 7b9bde1 commit c218818

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

lightning/src/util/ser.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1688,7 +1688,14 @@ impl Readable for Duration {
16881688
fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
16891689
let secs = Readable::read(r)?;
16901690
let nanos = Readable::read(r)?;
1691-
Ok(Duration::new(secs, nanos))
1691+
// Duration::new panics if the nanosecond part in excess of a second, added to the second
1692+
// part, overflows. To ensure this won't happen, we simply reject any case where there are
1693+
// nanoseconds in excess of a second, which is invalid anyway.
1694+
if nanos >= 1_000_000_000 {
1695+
Err(DecodeError::InvalidValue)
1696+
} else {
1697+
Ok(Duration::new(secs, nanos))
1698+
}
16921699
}
16931700
}
16941701

0 commit comments

Comments
 (0)