Skip to content

Commit

Permalink
Fix an overflow when rounding up the size of a file
Browse files Browse the repository at this point in the history
Closes #265
  • Loading branch information
alexcrichton committed Oct 4, 2021
1 parent 60c6bd8 commit de08ca1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/archive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,8 +298,10 @@ impl<'a> EntriesFields<'a> {

// Store where the next entry is, rounding up by 512 bytes (the size of
// a header);
let size = (size + 511) & !(512 - 1);
self.next += size;
let size = size
.checked_add(511)
.ok_or_else(|| other("size overflow"))?;
self.next += size & !(512 - 1);

Ok(Some(ret.into_entry()))
}
Expand Down
13 changes: 13 additions & 0 deletions tests/all.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1245,3 +1245,16 @@ fn tar_directory_containing_special_files() {
t!(ar.append_path("null"));
t!(ar.finish());
}

#[test]
fn header_size_overflow() {
let mut ar = Builder::new(Vec::new());
let mut header = Header::new_gnu();
header.set_size(0xffffffffffffffff);
header.set_cksum();
ar.append(&mut header, "x".as_bytes()).unwrap();
let result = t!(ar.into_inner());
let mut ar = Archive::new(&result[..]);
let mut e = ar.entries().unwrap();
assert!(e.next().unwrap().is_err());
}

0 comments on commit de08ca1

Please sign in to comment.