Skip to content

Commit

Permalink
Merge pull request #452 from tafia/issue444
Browse files Browse the repository at this point in the history
skip minifat construction if length is zero
  • Loading branch information
tafia authored Jul 20, 2024
2 parents e71f737 + ee68b24 commit a90e877
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 13 deletions.
30 changes: 17 additions & 13 deletions src/cfb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ impl Cfb {
let mut sectors = Sectors::new(h.sector_size, Vec::with_capacity(len));

// load fat and dif sectors
debug!("load difat");
debug!("load difat {h:?}");
let mut sector_id = h.difat_start;
while sector_id < RESERVED_SECTORS {
difat.extend(to_u32(sectors.get(sector_id, reader)?));
Expand All @@ -107,24 +107,28 @@ impl Cfb {
if dirs.is_empty() || (h.version != 3 && dirs[0].start == ENDOFCHAIN) {
return Err(CfbError::EmptyRootDir);
}
debug!("{:?}", dirs);

// load the mini streams
debug!("load minis");
let ministream = sectors.get_chain(dirs[0].start, &fats, reader, dirs[0].len)?;
let minifat = sectors.get_chain(
h.mini_fat_start,
&fats,
reader,
h.mini_fat_len * h.sector_size,
)?;
let minifat = to_u32(&minifat).collect();
debug!("load minis {dirs:?}");
let (mini_fats, ministream) = if h.mini_fat_len > 0 {
let ministream = sectors.get_chain(dirs[0].start, &fats, reader, dirs[0].len)?;
let minifat = sectors.get_chain(
h.mini_fat_start,
&fats,
reader,
h.mini_fat_len * h.sector_size,
)?;
let minifat = to_u32(&minifat).collect();
(minifat, ministream)
} else {
(Vec::new(), Vec::new())
};
Ok(Cfb {
directories: dirs,
sectors,
fats,
mini_sectors: Sectors::new(64, ministream),
mini_fats: minifat,
mini_fats,
})
}

Expand Down Expand Up @@ -183,7 +187,7 @@ impl Header {
0x000C => {
// sector size is 4096 bytes, but header is 512 bytes,
// so the remaining sector bytes have to be read
let mut buf_end = [0u8; 3584];
let mut buf_end = [0u8; 4096 - 512];
f.read_exact(&mut buf_end).map_err(CfbError::Io)?;
4096
}
Expand Down
Binary file added tests/issue444.xls
Binary file not shown.
9 changes: 9 additions & 0 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1700,6 +1700,15 @@ fn issue_438_charts() {
.expect("could not open worksheet range");
}

#[test]
fn isssue_444_memory_allocation() {
let mut excel: Xls<_> = wb("issue444.xls"); // should not fail
let range = excel
.worksheet_range("Sheet1")
.expect("could not open worksheet range");
assert_eq!(range.get_size(), (10, 8));
}

#[test]
fn isssue_446_formulas() {
let mut excel: Xlsx<_> = wb("issue446.xlsx");
Expand Down

0 comments on commit a90e877

Please sign in to comment.