Skip to content

Commit 06b0140

Browse files
author
Ben Leadbetter
committed
fix: panic in empty flex-data text iterator
1 parent cdf503b commit 06b0140

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

src/flex_data/text.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,14 +123,14 @@ pub struct TextBytesIterator<'a> {
123123
impl<'a> core::iter::Iterator for TextBytesIterator<'a> {
124124
type Item = u8;
125125
fn next(&mut self) -> Option<Self::Item> {
126+
while !self.finished() && self.value() == 0 {
127+
self.advance();
128+
}
126129
if self.finished() {
127130
return None;
128131
}
129132
let ret = Some(self.value());
130133
self.advance();
131-
while !self.finished() && self.value() == 0 {
132-
self.advance();
133-
}
134134
ret
135135
}
136136

@@ -143,7 +143,7 @@ impl<'a> core::iter::FusedIterator for TextBytesIterator<'a> {}
143143

144144
impl<'a> TextBytesIterator<'a> {
145145
fn finished(&self) -> bool {
146-
self.buffer.len() / 4 <= self.packet_index
146+
self.packet_index == self.buffer.len() / 4 - 1 && self.byte_index == 11
147147
}
148148
fn advance(&mut self) {
149149
self.byte_index += 1;

src/flex_data/unknown_metadata_text.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,16 @@ mod tests {
389389
)
390390
}
391391

392+
#[test]
393+
fn read_empty_text_bytes() {
394+
assert_eq!(
395+
UnknownMetadataText::<std::vec::Vec<u32>>::new()
396+
.text_bytes()
397+
.collect::<std::vec::Vec<u8>>(),
398+
std::vec![],
399+
)
400+
}
401+
392402
#[test]
393403
#[cfg(feature = "std")]
394404
fn read_string() {

0 commit comments

Comments
 (0)