Skip to content

Commit

Permalink
properly handle too long topic (should only happen with a bitcoind bu…
Browse files Browse the repository at this point in the history
…g) + add tests for it
  • Loading branch information
antonilol committed Nov 14, 2023
1 parent 4345783 commit 44815d6
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 4 deletions.
18 changes: 16 additions & 2 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::message::{DATA_MAX_LEN, SEQUENCE_LEN, TOPIC_MAX_LEN};
use bitcoin::consensus;
use core::fmt;
use core::{cmp::min, fmt};

pub type Result<T> = core::result::Result<T, Error>;

Expand All @@ -17,6 +17,20 @@ pub enum Error {
Zmq(zmq::Error),
}

impl Error {
/// Returns the (invalid) topic as a byte slice (as this might not always be valid UTF-8). If
/// this error is not an [`Error::InvalidTopic`], [`None`] is returned. The real length is also
/// returned, if this is higher that the length of the slice, the data was truncated to fit
/// directly in the object, instead of with a heap allocation.
pub fn invalid_topic_data(&self) -> Option<(&[u8], usize)> {
if let Self::InvalidTopic(len, buf) = self {
Some((&buf[..min(*len, buf.len())], *len))
} else {
None
}
}
}

impl From<zmq::Error> for Error {
#[inline]
fn from(value: zmq::Error) -> Self {
Expand Down Expand Up @@ -66,7 +80,7 @@ impl fmt::Display for Error {
write!(
f,
"invalid message topic '{}'{}",
String::from_utf8_lossy(&topic[0..*len]),
String::from_utf8_lossy(&topic[..min(*len, topic.len())]),
if *len > TOPIC_MAX_LEN {
" (truncated)"
} else {
Expand Down
40 changes: 38 additions & 2 deletions src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use bitcoin::{
hashes::Hash,
Block, BlockHash, Transaction, Txid,
};
use core::fmt;
use core::{cmp::min, fmt};

pub const TOPIC_MAX_LEN: usize = 9;
pub const DATA_MAX_LEN: usize = MAX_BLOCK_WEIGHT as usize;
Expand Down Expand Up @@ -134,7 +134,8 @@ impl Message {
_ => {
let mut buf = [0; TOPIC_MAX_LEN];

buf[0..topic.len()].copy_from_slice(topic);
buf[..min(TOPIC_MAX_LEN, topic.len())]
.copy_from_slice(&topic[..min(TOPIC_MAX_LEN, topic.len())]);

return Err(Error::InvalidTopic(topic.len(), buf));
}
Expand Down Expand Up @@ -257,4 +258,39 @@ mod tests {

assert_eq!(msg.serialize_to_vecs(), to_deserialize);
}

#[test]
fn test_deserialization_errors() {
let to_deserialize = [b"abc" as &[u8], &[], &[0x05, 0x00, 0x00, 0x00], b"garbage"];

assert!(matches!(
Message::from_multipart(&to_deserialize[..0]),
Err(Error::InvalidMutlipartLength(0))
));
assert!(matches!(
Message::from_multipart(&to_deserialize[..1]),
Err(Error::InvalidMutlipartLength(1))
));
assert!(matches!(
Message::from_multipart(&to_deserialize[..2]),
Err(Error::InvalidMutlipartLength(2))
));
assert_eq!(
Message::from_multipart(&to_deserialize[..3])
.expect_err("expected invalid topic")
.invalid_topic_data(),
Some((b"abc" as &[u8], 3))
);
assert!(matches!(
Message::from_multipart(&to_deserialize[..4]),
Err(Error::InvalidMutlipartLength(4))
));

assert_eq!(
Message::from_multipart(&[b"hashblock!" as &[u8], &[], &[0x06, 0x00, 0x00, 0x00]])
.expect_err("expected invalid topic")
.invalid_topic_data(),
Some((b"hashblock" as &[u8], 10))
)
}
}

0 comments on commit 44815d6

Please sign in to comment.