use std::alloc::{GlobalAlloc, Layout, System};
use hadris_cpio::read::CpioArchiveReader;
const CAP: usize = 512 * 1024 * 1024;
struct Capped;
unsafe impl GlobalAlloc for Capped {
unsafe fn alloc(&self, l: Layout) -> *mut u8 {
if l.size() > CAP {
return std::ptr::null_mut();
}
unsafe { System.alloc(l) }
}
unsafe fn dealloc(&self, p: *mut u8, l: Layout) {
unsafe { System.dealloc(p, l) }
}
unsafe fn realloc(&self, p: *mut u8, l: Layout, new: usize) -> *mut u8 {
if new > CAP {
return std::ptr::null_mut();
}
unsafe { System.realloc(p, l, new) }
}
}
#[global_allocator]
static ALLOC: Capped = Capped;
fn header_with(field_offset: usize, hex8: &str) -> Vec<u8> {
let mut h = Vec::new();
h.extend_from_slice(b"070701"); for _ in 0..13 {
h.extend_from_slice(b"00000000"); }
assert_eq!(h.len(), 110);
h[field_offset..field_offset + 8].copy_from_slice(hex8.as_bytes());
h
}
#[test]
fn oversized_namesize_does_not_preallocate() {
let mut archive = header_with(94, "FFFFFFFF");
archive.extend_from_slice(b"AAAA");
let mut reader = CpioArchiveReader::new(archive.as_slice());
assert!(reader.next_entry_alloc().is_err());
}
#[test]
fn oversized_filesize_does_not_preallocate() {
let mut archive = header_with(54, "FFFFFFFF"); archive[94..102].copy_from_slice(b"00000002"); archive.extend_from_slice(b"x\0"); archive.extend_from_slice(b"\0\0");
let mut reader = CpioArchiveReader::new(archive.as_slice());
let entry = reader
.next_entry_alloc()
.expect("header parses")
.expect("not trailer");
assert_eq!(entry.file_size(), 0xFFFF_FFFF);
assert!(reader.read_entry_data_alloc(&entry).is_err());
}