Validate GDSII record sizes through one fallible path

Route every record read through size validation and report malformed payloads
as parse errors instead of panicking. Apply metadata size constraints through
the Record trait, hide Arrow reader internals, and remove unused wrappers and
scratch code.

Add coverage for direct, skipped, and payload reads, truncated buffers, and
FFI error offsets.
This commit is contained in:
Jan Petykiewicz 2026-11-16 20:44:48 -08:00
commit 54aa757678

View file

@ -4,16 +4,12 @@
use std::io::{self, Write};
use crate::basic::{pack_datetime, pack_bitarray, pack_ascii, pack_int2, pack_int4, pack_real8}; #[warn(unused_imports)]
use crate::basic::{parse_datetime, parse_bitarray, parse_ascii, parse_int2, parse_int4, parse_real8}; #[warn(unused_imports)]
use crate::basic::{OResult, IResult, fail, parse_u16, take_bytes}; //ErrType,
use crate::basic::{pack_datetime, pack_bitarray, pack_ascii, pack_int2, pack_int4, pack_real8};
use crate::basic::{parse_datetime, parse_bitarray, parse_ascii, parse_int2, parse_int4, parse_real8};
use crate::basic::{OResult, IResult, fail, parse_u16, take_bytes};
use crate::records;
//#[no_mangle]
//pub extern "C" fn write_record_header(
#[repr(C)]
#[derive(Debug, Clone)]
pub struct RecordHeader {
@ -53,7 +49,6 @@ pub trait RecordData {
fn read(input: &[u8], size: u16) -> IResult<'_, Self::BareData<'_>>;
fn pack_into(buf: &mut [u8], data: &Self::InData);
//fn size(data: &Self::BareData<'_>) -> u16;
fn pack(data: &Self::InData) -> Self::ByteData;
}
@ -73,22 +68,17 @@ pub trait Record<RData: RecordData> {
}
}
fn read_header(input: &[u8]) -> IResult<'_, RecordHeader> {
RecordHeader::read(input)
}
fn write_header<W: Write>(ww: &mut W, data_size: u16) -> OResult {
RecordHeader{tag: Self::tag(), data_size}.write(ww)
}
fn read_data(input: &[u8], size: u16) -> IResult<'_, RData::BareData<'_>> {
if let Err(message) = Self::check_size(size) {
return fail(input, message);
}
RData::read(input, size)
}
fn pack_data(buf: &mut [u8], data: &RData::InData) {
RData::pack_into(buf, data)
}
///
/// Skip to the end of the next occurence of this record.
///
@ -116,8 +106,7 @@ pub trait Record<RData: RecordData> {
(input, _) = take_bytes(input, header.data_size)?;
(input, header) = RecordHeader::read(input)?;
}
let (input, data) = Self::read_data(input, header.data_size)?;
Ok((input, data))
Self::read_data(input, header.data_size)
}
fn expect_header(input: &[u8]) -> IResult<'_, u16> {
@ -131,9 +120,7 @@ pub trait Record<RData: RecordData> {
fn read(input: &[u8]) -> IResult<'_, RData::BareData<'_>> {
let (input, size) = Self::expect_header(input)?;
Self::check_size(size).unwrap();
let (input, data) = Self::read_data(input, size)?;
Ok((input, data))
Self::read_data(input, size)
}
fn write<W: Write>(ww: &mut W, data: &RData::InData) -> OResult {
@ -155,7 +142,9 @@ impl RecordData for BitArray {
type ByteData = [u8; 2];
fn read(input: &[u8], size: u16) -> IResult<'_, Self::BareData<'_>> {
assert!(size == 2);
if size != 2 {
return fail(input, format!("Expected record size {}, got {size}", 2));
}
parse_bitarray(input)
}
@ -178,7 +167,9 @@ impl RecordData for Int2 {
type ByteData = [u8; 2];
fn read(input: &[u8], size: u16) -> IResult<'_, Self::BareData<'_>> {
assert!(size == 2);
if size != 2 {
return fail(input, format!("Expected record size {}, got {size}", 2));
}
parse_int2(input)
}
@ -200,7 +191,9 @@ impl RecordData for Int4 {
type ByteData = [u8; 4];
fn read(input: &[u8], size: u16) -> IResult<'_, Self::BareData<'_>> {
assert!(size == 4);
if size != 4 {
return fail(input, format!("Expected record size {}, got {size}", 4));
}
parse_int4(input)
}
@ -223,8 +216,9 @@ impl RecordData for Int2Array {
type ByteData = Vec<u8>;
fn read(input: &[u8], size: u16) -> IResult<'_, Self::BareData<'_>> {
assert!(size.is_multiple_of(2), "Record must contain an integer quantity of integers");
//let mut input = input;
if !size.is_multiple_of(2) {
return fail(input, format!("Record size must be divisible by 2, got {size}"));
}
let (input, bytes) = take_bytes(input, size)?;
Ok((input, Int2ArrayReader{bytes}))
}
@ -267,8 +261,9 @@ impl RecordData for Int4Array {
type ByteData = Vec<u8>;
fn read(input: &[u8], size: u16) -> IResult<'_, Self::BareData<'_>> {
assert!(size.is_multiple_of(4), "Record must contain an integer quantity of integers");
//let mut input = input;
if !size.is_multiple_of(4) {
return fail(input, format!("Record size must be divisible by 4, got {size}"));
}
let (input, bytes) = take_bytes(input, size)?;
Ok((input, Int4ArrayReader{bytes}))
}
@ -311,7 +306,9 @@ impl RecordData for Real8 {
type ByteData = [u8; 8];
fn read(input: &[u8], size: u16) -> IResult<'_, Self::BareData<'_>> {
assert!(size == 8);
if size != 8 {
return fail(input, format!("Expected record size {}, got {size}", 8));
}
parse_real8(input)
}
@ -333,7 +330,9 @@ impl RecordData for Real8Pair {
type ByteData = [u8; 2 * 8];
fn read(input: &[u8], size: u16) -> IResult<'_, Self::BareData<'_>> {
assert!(size == 2 * 8);
if size != 2 * 8 {
return fail(input, format!("Expected record size {}, got {size}", 2 * 8));
}
let (input, data0) = parse_real8(input)?;
let (input, data1) = parse_real8(input)?;
Ok((input, (data0, data1)))
@ -350,10 +349,6 @@ impl RecordData for Real8Pair {
buf
}
//fn write<W: Write>(ww: &mut W, data: &Self::BareData) -> OResult {
// let mut buf = [u8; 2 * 6 * 2];
// Self::pack_into(&mut buf, data)
//}
}
pub struct ASCII;
@ -385,7 +380,9 @@ impl RecordData for DateTimePair {
type ByteData = [u8; 2 * 6 * 2];
fn read(input: &[u8], size: u16) -> IResult<'_, Self::BareData<'_>> {
assert!(size == 2 * 6 * 2);
if size != 2 * 6 * 2 {
return fail(input, format!("Expected record size {}, got {size}", 2 * 6 * 2));
}
let (input, data0) = parse_datetime(input)?;
let (input, data1) = parse_datetime(input)?;
Ok((input, [data0, data1]))
@ -402,10 +399,6 @@ impl RecordData for DateTimePair {
buf
}
//fn write<W: Write>(ww: &mut W, data: &Self::BareData) -> OResult {
// let mut buf = [u8; 2 * 6 * 2];
// Self::pack_into(&mut buf, data)
//}
}
pub struct Empty;
@ -415,7 +408,9 @@ impl RecordData for Empty {
type ByteData = [u8; 0];
fn read(input: &[u8], size: u16) -> IResult<'_, Self::BareData<'_>> {
assert!(size == 0);
if size != 0 {
return fail(input, format!("Expected record size {}, got {size}", 0));
}
Ok((input, ()))
}
@ -425,6 +420,4 @@ impl RecordData for Empty {
fn pack(_data: &Self::InData) -> Self::ByteData {
[]
}
//fn write<W: Write>(ww: &mut W, data: &Self::BareData) {
//}
}