fix unsafe keywords

This commit is contained in:
Szymon Walter 2018-03-19 19:36:30 +01:00
parent 959572eca1
commit fb1b5bdef5
3 changed files with 15 additions and 12 deletions

View file

@ -29,7 +29,7 @@ where
Error: From<B::Error>, Error: From<B::Error>,
{ {
pub fn new(buffer: B) -> Result<Ext2<B>, Error> { pub fn new(buffer: B) -> Result<Ext2<B>, Error> {
let superblock = Struct::from(Superblock::find(&buffer)?); let superblock = unsafe { Struct::from(Superblock::find(&buffer)?) };
let block_size = superblock.inner.block_size(); let block_size = superblock.inner.block_size();
let block_groups_offset = let block_groups_offset =
(superblock.inner.first_data_block as usize + 1) * block_size; (superblock.inner.first_data_block as usize + 1) * block_size;
@ -38,11 +38,13 @@ where
.block_group_count() .block_group_count()
.map(|count| count as usize) .map(|count| count as usize)
.map_err(|(a, b)| Error::BadBlockGroupCount(a, b))?; .map_err(|(a, b)| Error::BadBlockGroupCount(a, b))?;
let block_groups = BlockGroupDescriptor::find_descriptor_table( let block_groups = unsafe {
&buffer, BlockGroupDescriptor::find_descriptor_table(
block_groups_offset, &buffer,
block_groups_count, block_groups_offset,
)?; block_groups_count,
)?
};
let block_groups = Struct::from(block_groups); let block_groups = Struct::from(block_groups);
Ok(Ext2 { Ok(Ext2 {
buffer, buffer,

View file

@ -72,7 +72,7 @@ impl BlockGroupDescriptor {
let mut vec = Vec::with_capacity(count); let mut vec = Vec::with_capacity(count);
for i in 0..count { for i in 0..count {
let offset = offset + i * mem::size_of::<BlockGroupDescriptor>(); let offset = offset + i * mem::size_of::<BlockGroupDescriptor>();
vec.push(unsafe { vec.push({
BlockGroupDescriptor::find_descriptor(haystack, offset)?.0 BlockGroupDescriptor::find_descriptor(haystack, offset)?.0
}); });
} }
@ -88,8 +88,9 @@ mod tests {
#[test] #[test]
fn find() { fn find() {
let buffer = vec![0_u8; 4096]; let buffer = vec![0_u8; 4096];
let table = let table = unsafe {
BlockGroupDescriptor::find_descriptor_table(&buffer, 2048, 8); BlockGroupDescriptor::find_descriptor_table(&buffer, 2048, 8)
};
assert!( assert!(
table.is_ok(), table.is_ok(),
"Err({:?})", "Err({:?})",

View file

@ -157,7 +157,7 @@ impl Superblock {
return Err(Error::OutOfBounds(end)); return Err(Error::OutOfBounds(end));
} }
let superblock = unsafe { let superblock = {
haystack haystack
.slice_unchecked(offset..end) .slice_unchecked(offset..end)
.dynamic_cast::<Superblock>() .dynamic_cast::<Superblock>()
@ -250,7 +250,7 @@ mod tests {
// magic // magic
buffer[1024 + 56] = EXT2_MAGIC as u8; buffer[1024 + 56] = EXT2_MAGIC as u8;
buffer[1024 + 57] = (EXT2_MAGIC >> 8) as u8; buffer[1024 + 57] = (EXT2_MAGIC >> 8) as u8;
let superblock = Superblock::find(&buffer); let superblock = unsafe { Superblock::find(&buffer) };
assert!( assert!(
superblock.is_ok(), superblock.is_ok(),
"Err({:?})", "Err({:?})",
@ -265,7 +265,7 @@ mod tests {
use std::fs::File; use std::fs::File;
let file = RefCell::new(File::open("ext2.bin").unwrap()); let file = RefCell::new(File::open("ext2.bin").unwrap());
let superblock = Superblock::find(&file); let superblock = unsafe { Superblock::find(&file) };
assert!( assert!(
superblock.is_ok(), superblock.is_ok(),
"Err({:?})", "Err({:?})",