Skip to content

Commit

Permalink
Collect Backtrace on insufficient source / destination buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
kellerkindt committed Jun 11, 2021
1 parent 96df6b2 commit 4a2358e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
33 changes: 26 additions & 7 deletions src/io/per/err.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use crate::model::Charset;
use backtrace::Backtrace;
use std::string::FromUtf8Error;

#[derive(Debug, PartialEq)]
#[derive(Debug)]
pub enum Error {
FromUtf8Error(FromUtf8Error),
InvalidString(Charset, char, usize),
UnsupportedOperation(String),
InsufficientSpaceInDestinationBuffer,
InsufficientDataInSourceBuffer,
InsufficientSpaceInDestinationBuffer(Backtrace),
InsufficientDataInSourceBuffer(Backtrace),
InvalidChoiceIndex(u64, u64),
ExtensionFieldsInconsistent(String),
ValueNotInRange(i64, i64, i64),
Expand All @@ -25,6 +26,14 @@ impl Error {
Some((index, char)) => Err(Self::InvalidString(charset, char, index)),
}
}

pub fn insufficient_space_in_destination_buffer() -> Self {
Error::InsufficientSpaceInDestinationBuffer(Backtrace::new_unresolved())
}

pub fn insufficient_data_in_source_buffer() -> Self {
Error::InsufficientDataInSourceBuffer(Backtrace::new_unresolved())
}
}

impl std::fmt::Display for Error {
Expand All @@ -42,13 +51,23 @@ impl std::fmt::Display for Error {
)
}
Error::UnsupportedOperation(o) => write!(f, "The operation is not supported: {}", o),
Error::InsufficientSpaceInDestinationBuffer => write!(
Error::InsufficientSpaceInDestinationBuffer(backtrace) => write!(
f,
"There is insufficient space in the destination buffer for this operation"
"There is insufficient space in the destination buffer for this operation:\n{:?}",
{
let mut b = backtrace.clone();
b.resolve();
b
}
),
Error::InsufficientDataInSourceBuffer => write!(
Error::InsufficientDataInSourceBuffer(backtrace) => write!(
f,
"There is insufficient data in the source buffer for this operation"
"There is insufficient data in the source buffer for this operation:\n{:?}",
{
let mut b = backtrace.clone();
b.resolve();
b
}
),
Error::InvalidChoiceIndex(index, variant_count) => write!(
f,
Expand Down
8 changes: 4 additions & 4 deletions src/io/per/unaligned/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,10 @@ fn bit_string_copy(
len: usize,
) -> Result<(), Error> {
if dst.len() * BYTE_LEN < dst_bit_position + len {
return Err(Error::InsufficientSpaceInDestinationBuffer);
return Err(Error::insufficient_space_in_destination_buffer());
}
if src.len() * BYTE_LEN < src_bit_position + len {
return Err(Error::InsufficientDataInSourceBuffer);
return Err(Error::insufficient_data_in_source_buffer());
}
for bit in 0..len {
let dst_byte_pos = (dst_bit_position + bit) / BYTE_LEN;
Expand Down Expand Up @@ -141,10 +141,10 @@ pub(crate) fn bit_string_copy_bulked(
}

if dst.len() * BYTE_LEN < dst_bit_position + len {
return Err(Error::InsufficientSpaceInDestinationBuffer);
return Err(Error::insufficient_space_in_destination_buffer());
}
if src.len() * BYTE_LEN < src_bit_position + len {
return Err(Error::InsufficientDataInSourceBuffer);
return Err(Error::insufficient_data_in_source_buffer());
}

let bits_till_full_byte_src = (BYTE_LEN - (src_bit_position % BYTE_LEN)) % BYTE_LEN;
Expand Down

0 comments on commit 4a2358e

Please sign in to comment.