Rely on Error/Warning message data lengths being correct 2021-07-warning-msgs
authorMatt Corallo <git@bluematt.me>
Fri, 7 Jan 2022 20:11:31 +0000 (20:11 +0000)
committerMatt Corallo <git@bluematt.me>
Tue, 11 Jan 2022 20:25:24 +0000 (20:25 +0000)
In https://github.com/lightning/bolts/pull/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.

lightning/src/ln/msgs.rs

index 6c04636a4355c96fb0f5158887f855a93977e7b7..9007f3f2ea7d59aaa820c0999e5c069be3718935 100644 (file)
@@ -33,7 +33,7 @@ use bitcoin::hash_types::{Txid, BlockHash};
 use ln::features::{ChannelFeatures, ChannelTypeFeatures, InitFeatures, NodeFeatures};
 
 use prelude::*;
-use core::{cmp, fmt};
+use core::fmt;
 use core::fmt::Debug;
 use io::{self, Read};
 use io_extras::read_to_end;
@@ -1529,10 +1529,11 @@ impl Readable for ErrorMessage {
                Ok(Self {
                        channel_id: Readable::read(r)?,
                        data: {
-                               let mut sz: usize = <u16 as Readable>::read(r)? as usize;
-                               let data = read_to_end(r)?;
-                               sz = cmp::min(data.len(), sz);
-                               match String::from_utf8(data[..sz as usize].to_vec()) {
+                               let sz: usize = <u16 as Readable>::read(r)? as usize;
+                               let mut data = Vec::with_capacity(sz);
+                               data.resize(sz, 0);
+                               r.read_exact(&mut data)?;
+                               match String::from_utf8(data) {
                                        Ok(s) => s,
                                        Err(_) => return Err(DecodeError::InvalidValue),
                                }
@@ -1555,10 +1556,11 @@ impl Readable for WarningMessage {
                Ok(Self {
                        channel_id: Readable::read(r)?,
                        data: {
-                               let mut sz: usize = <u16 as Readable>::read(r)? as usize;
-                               let data = read_to_end(r)?;
-                               sz = cmp::min(data.len(), sz);
-                               match String::from_utf8(data[..sz as usize].to_vec()) {
+                               let sz: usize = <u16 as Readable>::read(r)? as usize;
+                               let mut data = Vec::with_capacity(sz);
+                               data.resize(sz, 0);
+                               r.read_exact(&mut data)?;
+                               match String::from_utf8(data) {
                                        Ok(s) => s,
                                        Err(_) => return Err(DecodeError::InvalidValue),
                                }