Skip to content

Commit d786bfa

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 2d7b06e commit d786bfa

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

lightning/src/ln/msgs.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ use bitcoin::hash_types::{Txid, BlockHash};
3333
use ln::features::{ChannelFeatures, ChannelTypeFeatures, InitFeatures, NodeFeatures};
3434

3535
use prelude::*;
36-
use core::{cmp, fmt};
36+
use core::fmt;
3737
use core::fmt::Debug;
3838
use io::{self, Read};
3939
use io_extras::read_to_end;
@@ -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)