fix panic if len is zero

This commit is contained in:
Numbers
2025-06-02 09:57:38 +02:00
parent d4b8ef032b
commit fa11367d84

View File

@@ -7,11 +7,11 @@ const SEP: &str = " | ";
pub struct HexDump<'s, T: Scannable + ?Sized, R: RangeBounds<usize>>(pub &'s T, pub R); pub struct HexDump<'s, T: Scannable + ?Sized, R: RangeBounds<usize>>(pub &'s T, pub R);
pub fn hex< pub fn hex<
's, 's,
T: Scannable + ?Sized, T: Scannable + ?Sized,
R: RangeBounds<usize> R: RangeBounds<usize>
>( >(
data: &'s T, data: &'s T,
range:R range:R
) -> HexDump<'s, T, R> { ) -> HexDump<'s, T, R> {
HexDump(data, range) HexDump(data, range)
@@ -45,6 +45,12 @@ impl<'s, T: Scannable + ?Sized, R: RangeBounds<usize>> Display for HexDump<'s, T
(start, end) (start, end)
}; };
// if there is nothing to print then just return...
// this also prevents the ilog below from crashing :)
if end == 0 || start > end {
return Ok(())
}
// the number of digits the address column should have // the number of digits the address column should have
let digits = (end.ilog(16) as usize + 1).max(4); let digits = (end.ilog(16) as usize + 1).max(4);