Use a ParseError::EndianError rather than panicking

This commit is contained in:
Christopher Cole 2022-10-02 23:02:43 -07:00
parent e604816c15
commit e1ba759253
2 changed files with 13 additions and 12 deletions

View File

@ -39,6 +39,7 @@ impl std::fmt::Display for File {
#[derive(Debug)]
pub enum ParseError {
EndianError,
IoError(io::Error),
InvalidMagic,
InvalidFormat(Option<std::string::FromUtf8Error>),

View File

@ -1,40 +1,40 @@
#[macro_export]
macro_rules! read_u16 {
($elf:ident, $io:ident) => ({
($elf:ident, $io:ident) => {{
use byteorder::{LittleEndian, BigEndian, ReadBytesExt};
match $elf.ehdr.data {
types::ELFDATA2LSB => { $io.read_u16::<LittleEndian>() }
types::ELFDATA2MSB => { $io.read_u16::<BigEndian>() }
types::ELFDATANONE => { panic!("Unable to resolve file endianness"); }
_ => { panic!("Unable to resolve file endianness"); }
types::ELFDATANONE => { return Err(ParseError::EndianError); }
_ => { return Err(ParseError::EndianError); }
}
});
}};
}
#[macro_export]
macro_rules! read_u32 {
($elf:ident, $io:ident) => ({
($elf:ident, $io:ident) => {{
use byteorder::{LittleEndian, BigEndian, ReadBytesExt};
match $elf.ehdr.data {
types::ELFDATA2LSB => { $io.read_u32::<LittleEndian>() }
types::ELFDATA2MSB => { $io.read_u32::<BigEndian>() }
types::ELFDATANONE => { panic!("Unable to resolve file endianness"); }
_ => { panic!("Unable to resolve file endianness"); }
types::ELFDATANONE => { return Err(ParseError::EndianError); }
_ => { return Err(ParseError::EndianError); }
}
});
}};
}
#[macro_export]
macro_rules! read_u64 {
($elf:ident, $io:ident) => ({
($elf:ident, $io:ident) => {{
use byteorder::{LittleEndian, BigEndian, ReadBytesExt};
match $elf.ehdr.data {
types::ELFDATA2LSB => { $io.read_u64::<LittleEndian>() }
types::ELFDATA2MSB => { $io.read_u64::<BigEndian>() }
types::ELFDATANONE => { panic!("Unable to resolve file endianness"); }
_ => { panic!("Unable to resolve file endianness"); }
types::ELFDATANONE => { return Err(ParseError::EndianError); }
_ => { return Err(ParseError::EndianError); }
}
});
}};
}
use std;