Skip to content

Commit 037e9a3

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 f682d0c commit 037e9a3

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

lightning/src/ln/msgs.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1550,9 +1550,10 @@ impl Readable for ErrorMessage {
15501550
channel_id: Readable::read(r)?,
15511551
data: {
15521552
let mut sz: usize = <u16 as Readable>::read(r)? as usize;
1553-
let data = read_to_end(r)?;
1554-
sz = cmp::min(data.len(), sz);
1555-
match String::from_utf8(data[..sz as usize].to_vec()) {
1553+
let mut data = Vec::with_capacity(sz);
1554+
data.resize(sz, 0);
1555+
r.read_exact(&mut data)?;
1556+
match String::from_utf8(data) {
15561557
Ok(s) => s,
15571558
Err(_) => return Err(DecodeError::InvalidValue),
15581559
}
@@ -1576,9 +1577,10 @@ impl Readable for WarningMessage {
15761577
channel_id: Readable::read(r)?,
15771578
data: {
15781579
let mut sz: usize = <u16 as Readable>::read(r)? as usize;
1579-
let data = read_to_end(r)?;
1580-
sz = cmp::min(data.len(), sz);
1581-
match String::from_utf8(data[..sz as usize].to_vec()) {
1580+
let mut data = Vec::with_capacity(sz);
1581+
data.resize(sz, 0);
1582+
r.read_exact(&mut data)?;
1583+
match String::from_utf8(data) {
15821584
Ok(s) => s,
15831585
Err(_) => return Err(DecodeError::InvalidValue),
15841586
}

0 commit comments

Comments
 (0)