Skip to content

Commit

Permalink
refactor: return entries when read a zero byte instead of returning e…
Browse files Browse the repository at this point in the history
…rror

Signed-off-by: bsbds <69835502+bsbds@users.noreply.github.com>
  • Loading branch information
bsbds committed Apr 19, 2024
1 parent 1f22570 commit 3dab4a8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
2 changes: 1 addition & 1 deletion crates/curp/src/server/storage/wal/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub(crate) enum WALError {
/// The WAL segment might reach on end
///
/// NOTE: This exists because we cannot tell the difference between a corrupted WAL
/// and a normally ended WAL, as the segment files are all preallocated with zeros
/// and a normally ended WAL, as the segment files are all preallocated with zeros
#[error("WAL ended")]
MaybeEnded,
/// The WAL corrupt error
Expand Down
13 changes: 12 additions & 1 deletion crates/curp/src/server/storage/wal/segment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,18 @@ impl WALSegment {
let mut pos = 0;
let mut entries = Vec::new();
while pos < buf.len() {
let (item, n) = decoder.decode(&buf[pos..])?;
let (item, n) = match decoder.decode(&buf[pos..]) {
Ok(d) => d,
Err(WALError::MaybeEnded) => {
if !buf[pos..].iter().all(|b| *b == 0) {
return Err(WALError::Corrupted(CorruptType::Codec(
"Read zero".to_owned(),
)));
}
return Ok(entries);
}
Err(e) => return Err(e),
};
entries.push(item);
pos += n;
}
Expand Down

0 comments on commit 3dab4a8

Please sign in to comment.