Skip to content

Commit 6bbeeee

Browse files
committed
Update error deserialization in compliance with BOLT #1
1 parent 3d588da commit 6bbeeee

File tree

1 file changed

+4
-6
lines changed

1 file changed

+4
-6
lines changed

src/ln/msgs.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use bitcoin::network::serialize::{deserialize,serialize};
55
use bitcoin::blockdata::script::Script;
66

77
use std::error::Error;
8-
use std::fmt;
8+
use std::{cmp, fmt};
99
use std::result::Result;
1010

1111
use util::{byte_utils, internal_traits, events};
@@ -1626,11 +1626,9 @@ impl MsgDecodable for ErrorMessage {
16261626
if v.len() < 34 {
16271627
return Err(DecodeError::ShortRead);
16281628
}
1629-
let len = byte_utils::slice_to_be16(&v[32..34]);
1630-
if v.len() < 34 + len as usize {
1631-
return Err(DecodeError::ShortRead);
1632-
}
1633-
let data = match String::from_utf8(v[34..34 + len as usize].to_vec()) {
1629+
// Unlike most messages, BOLT 1 requires we truncate our read if the value is out of range
1630+
let mut len = cmp::min(byte_utils::slice_to_be16(&v[32..34]) as usize, v.len() - 34);
1631+
let data = match String::from_utf8(v[34..34 + len].to_vec()) {
16341632
Ok(s) => s,
16351633
Err(_) => return Err(DecodeError::BadText),
16361634
};

0 commit comments

Comments
 (0)