Skip to content

Commit dc3e097

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 bb79e05 commit dc3e097

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
@@ -1524,10 +1524,11 @@ impl Readable for ErrorMessage {
15241524
Ok(Self {
15251525
channel_id: Readable::read(r)?,
15261526
data: {
1527-
let mut sz: usize = <u16 as Readable>::read(r)? as usize;
1528-
let data = read_to_end(r)?;
1529-
sz = cmp::min(data.len(), sz);
1530-
match String::from_utf8(data[..sz as usize].to_vec()) {
1527+
let sz: usize = <u16 as Readable>::read(r)? as usize;
1528+
let mut data = Vec::with_capacity(sz);
1529+
data.resize(sz, 0);
1530+
r.read_exact(&mut data)?;
1531+
match String::from_utf8(data) {
15311532
Ok(s) => s,
15321533
Err(_) => return Err(DecodeError::InvalidValue),
15331534
}
@@ -1550,10 +1551,11 @@ impl Readable for WarningMessage {
15501551
Ok(Self {
15511552
channel_id: Readable::read(r)?,
15521553
data: {
1553-
let mut sz: usize = <u16 as Readable>::read(r)? as usize;
1554-
let data = read_to_end(r)?;
1555-
sz = cmp::min(data.len(), sz);
1556-
match String::from_utf8(data[..sz as usize].to_vec()) {
1554+
let sz: usize = <u16 as Readable>::read(r)? as usize;
1555+
let mut data = Vec::with_capacity(sz);
1556+
data.resize(sz, 0);
1557+
r.read_exact(&mut data)?;
1558+
match String::from_utf8(data) {
15571559
Ok(s) => s,
15581560
Err(_) => return Err(DecodeError::InvalidValue),
15591561
}

0 commit comments

Comments
 (0)