Skip to content

fix: skip trailing padding in HII database parsing #9

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

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
20 changes: 20 additions & 0 deletions src/lib/hii/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use std::fmt;
use std::fs;
use std::io::Seek;
use std::rc::Rc;
use std::io::Read;

use anyhow::Context;
use anyhow::Result;
Expand Down Expand Up @@ -93,6 +94,25 @@ fn get_package_lists(source: &[u8]) -> Result<Vec<PackageList>> {
.context("failed to find current position of db_cursor")?;

while used_bytes < db_size {
// Stop parsing if fewer than 20 bytes remain — not enough for a package list header.
// Also handle trailing padding (all 0x00 or 0xFF), which isn't a valid package list.
let remaining = (db_size - used_bytes) as usize;

if remaining < 20 {
debug!("Less than 20 bytes remaining, stopping parse.");
break;
}

let mut header_buf = [0u8; 20];
db_cursor.read_exact(&mut header_buf)?;
db_cursor.seek(SeekFrom::Current(-20))?; // rewind

let looks_like_padding = header_buf.iter().all(|&b| b == 0x00 || b == 0xFF);
if looks_like_padding {
debug!("Padding detected at offset {}, stopping parse.", used_bytes);
break;
}

let package_list: PackageList = match db_cursor.read_ne() {
Err(why) => {
error!("Can't parse more package lists: {}", why);
Expand Down