Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Truncated fat entries on error #44

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions src/internal/alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,12 @@ impl<F> Allocator<F> {
}

fn validate(&mut self, validation: Validation) -> io::Result<()> {
if self.fat.len() > self.sectors.num_sectors() as usize {
let num_sectors = self.sectors.num_sectors() as usize;
if self.fat.len() > num_sectors && validation.is_strict() {
malformed!(
"FAT has {} entries, but file has only {} sectors",
self.fat.len(),
self.sectors.num_sectors()
num_sectors
);
}
for &difat_sector in self.difat_sector_ids.iter() {
Expand Down Expand Up @@ -133,7 +134,9 @@ impl<F> Allocator<F> {
let mut pointees = FnvHashSet::default();
for (from_sector, &to_sector) in self.fat.iter().enumerate() {
if to_sector <= consts::MAX_REGULAR_SECTOR {
if to_sector as usize >= self.fat.len() {
if to_sector as usize >= self.fat.len()
&& validation.is_strict()
{
malformed!(
"FAT has {} entries, but sector {} points to {}",
self.fat.len(),
Expand Down Expand Up @@ -504,7 +507,7 @@ mod tests {
fn pointee_out_of_range() {
let difat = vec![0];
let fat = vec![consts::FAT_SECTOR, 2];
make_allocator(difat, fat, Validation::Permissive);
make_allocator(difat, fat, Validation::Strict);
}

#[test]
Expand Down
29 changes: 26 additions & 3 deletions src/internal/stream.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::internal::{consts, MiniAllocator, ObjType, SectorInit};
use std::io::{self, BufRead, Read, Seek, SeekFrom, Write};
use std::io::{self, BufRead, ErrorKind, Read, Seek, SeekFrom, Write};
use std::sync::{Arc, RwLock, Weak};

//===========================================================================//
Expand Down Expand Up @@ -274,6 +274,27 @@ impl<F: Read + Write + Seek> Flusher<F> for FlushBuffer {

//===========================================================================//

fn read_until_error<R: Read + ?Sized>(
this: &mut R,
mut buf: &mut [u8],
) -> io::Result<usize> {
let mut read_amount = 0;

while !buf.is_empty() {
match this.read(buf) {
Ok(0) => break,
Ok(n) => {
buf = &mut buf[n..];
read_amount += n;
}
Err(err) if err.kind() == ErrorKind::Interrupted => {}
Err(err) => return Err(err),
}
}

Ok(read_amount)
}

fn read_data_from_stream<F: Read + Seek>(
minialloc: &mut MiniAllocator<F>,
stream_id: u32,
Expand All @@ -299,12 +320,14 @@ fn read_data_from_stream<F: Read + Seek>(
if stream_len < consts::MINI_STREAM_CUTOFF as u64 {
let mut chain = minialloc.open_mini_chain(start_sector)?;
chain.seek(SeekFrom::Start(buf_offset_from_start))?;
chain.read_exact(&mut buf[..num_bytes])?;
return Ok(read_until_error(&mut chain, &mut buf[..num_bytes])
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a little confused what's going on with this change. Seems like it's changing to ignore IO errors? Why is that desired?

Copy link

@ideeockus ideeockus Sep 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mdsteele fat entries may be incomplete. read_exact emits Error { kind: UnexpectedEof, message: "failed to fill whole buffer" } in case of incomplete data

.unwrap_or(0));
} else {
let mut chain =
minialloc.open_chain(start_sector, SectorInit::Zero)?;
chain.seek(SeekFrom::Start(buf_offset_from_start))?;
chain.read_exact(&mut buf[..num_bytes])?;
return Ok(read_until_error(&mut chain, &mut buf[..num_bytes])
.unwrap_or(0));
}
}
Ok(num_bytes)
Expand Down
Loading