Skip to content

Commit 2b7d632

Browse files
committed
Rely on Error/Warning message data lengths being correct
In lightning/bolts#950, the (somewhat strange) requirement that error messages be handled even if the length field is set larger than the size of the package was removed. Here we change the code to drop the special handling for this, opting to just fail to read the message if the length is incorrect.
1 parent 96af817 commit 2b7d632

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

lightning/src/ln/msgs.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1529,10 +1529,11 @@ impl Readable for ErrorMessage {
15291529
Ok(Self {
15301530
channel_id: Readable::read(r)?,
15311531
data: {
1532-
let mut sz: usize = <u16 as Readable>::read(r)? as usize;
1533-
let data = read_to_end(r)?;
1534-
sz = cmp::min(data.len(), sz);
1535-
match String::from_utf8(data[..sz as usize].to_vec()) {
1532+
let sz: usize = <u16 as Readable>::read(r)? as usize;
1533+
let mut data = Vec::with_capacity(sz);
1534+
data.resize(sz, 0);
1535+
r.read_exact(&mut data)?;
1536+
match String::from_utf8(data) {
15361537
Ok(s) => s,
15371538
Err(_) => return Err(DecodeError::InvalidValue),
15381539
}
@@ -1555,10 +1556,11 @@ impl Readable for WarningMessage {
15551556
Ok(Self {
15561557
channel_id: Readable::read(r)?,
15571558
data: {
1558-
let mut sz: usize = <u16 as Readable>::read(r)? as usize;
1559-
let data = read_to_end(r)?;
1560-
sz = cmp::min(data.len(), sz);
1561-
match String::from_utf8(data[..sz as usize].to_vec()) {
1559+
let sz: usize = <u16 as Readable>::read(r)? as usize;
1560+
let mut data = Vec::with_capacity(sz);
1561+
data.resize(sz, 0);
1562+
r.read_exact(&mut data)?;
1563+
match String::from_utf8(data) {
15621564
Ok(s) => s,
15631565
Err(_) => return Err(DecodeError::InvalidValue),
15641566
}

0 commit comments

Comments
 (0)