[thin_metadata_unpack] Fix truncated output file on invalid input
Check the input header before creating or truncating the output file
This commit is contained in:
parent
6cecf0f673
commit
a50c9d97e2
@ -39,7 +39,7 @@ fn main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if let Err(reason) = thinp::pack::toplevel::unpack(&input_file, &output_file) {
|
if let Err(reason) = thinp::pack::toplevel::unpack(&input_file, &output_file) {
|
||||||
println!("Application error: {}", reason);
|
eprintln!("Application error: {}", reason);
|
||||||
process::exit(1);
|
process::exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -163,24 +163,28 @@ fn read_header<R>(mut r: R) -> io::Result<u64>
|
|||||||
where
|
where
|
||||||
R: byteorder::ReadBytesExt,
|
R: byteorder::ReadBytesExt,
|
||||||
{
|
{
|
||||||
use std::process::exit;
|
|
||||||
|
|
||||||
let magic = r.read_u64::<LittleEndian>()?;
|
let magic = r.read_u64::<LittleEndian>()?;
|
||||||
if magic != MAGIC {
|
if magic != MAGIC {
|
||||||
eprintln!("Not a pack file.");
|
return Err(io::Error::new(
|
||||||
exit(1);
|
io::ErrorKind::InvalidData,
|
||||||
|
"Not a pack file",
|
||||||
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
let version = r.read_u64::<LittleEndian>()?;
|
let version = r.read_u64::<LittleEndian>()?;
|
||||||
if version != PACK_VERSION {
|
if version != PACK_VERSION {
|
||||||
eprintln!("unsupported pack file version ({}).", PACK_VERSION);
|
return Err(io::Error::new(
|
||||||
exit(1);
|
io::ErrorKind::InvalidData,
|
||||||
|
format!("unsupported pack file version ({}).", PACK_VERSION),
|
||||||
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
let block_size = r.read_u64::<LittleEndian>()?;
|
let block_size = r.read_u64::<LittleEndian>()?;
|
||||||
if block_size != BLOCK_SIZE {
|
if block_size != BLOCK_SIZE {
|
||||||
eprintln!("block size is not {}", BLOCK_SIZE);
|
return Err(io::Error::new(
|
||||||
exit(1);
|
io::ErrorKind::InvalidData,
|
||||||
|
format!("block size is not {}", BLOCK_SIZE),
|
||||||
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
r.read_u64::<LittleEndian>()
|
r.read_u64::<LittleEndian>()
|
||||||
@ -270,6 +274,8 @@ pub fn unpack(input_file: &Path, output_file: &Path) -> Result<(), Box<dyn Error
|
|||||||
.write(false)
|
.write(false)
|
||||||
.open(input_file)?;
|
.open(input_file)?;
|
||||||
|
|
||||||
|
let nr_blocks = read_header(&input)?;
|
||||||
|
|
||||||
let mut output = OpenOptions::new()
|
let mut output = OpenOptions::new()
|
||||||
.read(false)
|
.read(false)
|
||||||
.write(true)
|
.write(true)
|
||||||
@ -277,8 +283,6 @@ pub fn unpack(input_file: &Path, output_file: &Path) -> Result<(), Box<dyn Error
|
|||||||
.truncate(true)
|
.truncate(true)
|
||||||
.open(output_file)?;
|
.open(output_file)?;
|
||||||
|
|
||||||
let nr_blocks = read_header(&input)?;
|
|
||||||
|
|
||||||
// zero the last block to size the file
|
// zero the last block to size the file
|
||||||
write_zero_block(&mut output, nr_blocks - 1)?;
|
write_zero_block(&mut output, nr_blocks - 1)?;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user