byte_buffer: fix case where length of string may be zero with no trailing null byte

This commit is contained in:
Matt Bilker
2018-12-08 21:07:00 +00:00
parent 378538ff13
commit a266030eaa

View File

@@ -13,8 +13,13 @@ pub use error::{KbinError, KbinErrorKind, Result};
/// Remove trailing null bytes, used for the `String` type
pub(crate) fn strip_trailing_null_bytes<'a>(data: &'a [u8]) -> &'a [u8] {
let mut index = data.len() - 1;
let len = data.len();
if len == 0 {
return data;
}
let mut index = len - 1;
while index > 0 && index < len && data[index] == 0x00 {
index -= 1;
}