From a266030eaac01b1b46afb5910527adb2fc943acb Mon Sep 17 00:00:00 2001 From: Matt Bilker Date: Sat, 8 Dec 2018 21:07:00 +0000 Subject: [PATCH] byte_buffer: fix case where length of string may be zero with no trailing null byte --- src/byte_buffer.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/byte_buffer.rs b/src/byte_buffer.rs index c7afb81..bd6e356 100644 --- a/src/byte_buffer.rs +++ b/src/byte_buffer.rs @@ -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; }