Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use checked arithmetic for the entry index in Extents #314

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions src/iters/extents.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::checksum::Checksum;
use crate::error::{Corrupt, Ext4Error};
use crate::extent::Extent;
use crate::inode::{Inode, InodeIndex};
use crate::util::{read_u16le, read_u32le, u64_from_hilo};
use crate::util::{read_u16le, read_u32le, u64_from_hilo, usize_from_u32};
use crate::Ext4;
use alloc::vec;
use alloc::vec::Vec;
Expand Down Expand Up @@ -88,7 +88,7 @@ struct ToVisitItem {

// Current index within the node. 0 is the node header, 1 is the
// first entry, etc.
entry: u16,
entry: u32,

// Node depth, copied from the node header.
depth: u16,
Expand All @@ -115,7 +115,7 @@ impl ToVisitItem {
}

fn entry(&self) -> Option<&[u8]> {
let start = usize::from(self.entry) * ENTRY_SIZE_IN_BYTES;
let start = usize_from_u32(self.entry) * ENTRY_SIZE_IN_BYTES;
self.node.get(start..start + ENTRY_SIZE_IN_BYTES)
}
}
Expand Down Expand Up @@ -167,7 +167,11 @@ impl Extents {
// Increment at the start to ensure that early returns don't
// accidentally skip the increment. Since entry 0 is the node
// header, entry 1 is the first actual node entry.
item.entry += 1;
//
// OK to unwrap: there are at most `2^16-1` entries, plus 1 for
// the header. Adding 1 here brings the maximum value to
// `2^16+1`. This fits in `item.entry` since it is a `u32`.
item.entry = item.entry.checked_add(1).unwrap();

let Some(entry) = &item.entry() else {
// Reached end of this node.
Expand Down