Skip to content

Commit 5ad7d4e

Browse files
committed
consensus_encoding: fix zero element SliceEncoder
Ensure that the compact size is removed on the first call to advance so that even if the slice is empty, current_chunk fulfills its contract and returns None.
1 parent 544d979 commit 5ad7d4e

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

consensus_encoding/src/encode/encoders.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -111,24 +111,24 @@ impl<'e, T: Encodable> Encoder for SliceEncoder<'e, T> {
111111
}
112112

113113
fn advance(&mut self) -> bool {
114+
// Handle compact_size first, regardless of whether we have elements.
115+
if self.compact_size.is_some() {
116+
self.compact_size = None;
117+
return self.cur_enc.is_some();
118+
}
119+
114120
let Some(cur) = self.cur_enc.as_mut() else {
115121
return false;
116122
};
117123

118124
loop {
119-
if self.compact_size.is_some() {
120-
// On the first call to advance(), just mark the compact_size as already
121-
// yielded and leave self.sl alone.
122-
self.compact_size = None;
123-
} else {
124-
// On subsequent calls, attempt to advance the current encoder and return
125-
// success if this succeeds.
126-
if cur.advance() {
127-
return true;
128-
}
129-
// self.sl guaranteed to be non-empty if cur is non-None.
130-
self.sl = &self.sl[1..];
125+
// On subsequent calls, attempt to advance the current encoder and return
126+
// success if this succeeds.
127+
if cur.advance() {
128+
return true;
131129
}
130+
// self.sl guaranteed to be non-empty if cur is non-None.
131+
self.sl = &self.sl[1..];
132132

133133
// If advancing the current encoder failed, attempt to move to the next encoder.
134134
if let Some(x) = self.sl.first() {

0 commit comments

Comments
 (0)