Reject invalid record sizes and return malformed-input errors

Validate payload sizes before record/header writes and make header packing
return io::Result when the total size overflows the format limit.

Return located parse errors for invalid UTF-8 and timestamp year overflow.
Validate cell timestamps consistently in full reads, selected reads, and
hierarchy scans while retaining scan behavior for skipped text geometry.

Add regression coverage for preserved writer outputs, padded metadata sizes,
malformed strings, timestamp boundaries, bounded errors, and Python bindings.
Document the fallible header packing API.

Validation: all 97 Rust tests pass in debug and release; all 16 Python tests
pass against the rebuilt native library.
This commit is contained in:
Jan Petykiewicz 2026-11-16 21:28:54 -08:00
commit 31afef0529

View file

@ -27,15 +27,16 @@ impl RecordHeader {
Ok((input, RecordHeader{tag, data_size}))
}
pub fn pack_into(&self) -> [u8; 4] {
let size = self.data_size.checked_add(4).expect("Record too big!");
pub fn pack_into(&self) -> io::Result<[u8; 4]> {
let size = self.data_size.checked_add(4)
.ok_or_else(|| io::Error::new(io::ErrorKind::InvalidInput, "Record longer than max size (u16)"))?;
let [size_hi, size_lo] = size.to_be_bytes();
let [tag_hi, tag_lo] = self.tag.to_be_bytes();
[size_hi, size_lo, tag_hi, tag_lo]
Ok([size_hi, size_lo, tag_hi, tag_lo])
}
pub fn write<W: Write>(&self, ww: &mut W) -> OResult {
let bytes = self.pack_into();
let bytes = self.pack_into()?;
ww.write_all(&bytes)?;
Ok(bytes.len())
}
@ -69,6 +70,8 @@ pub trait Record<RData: RecordData> {
}
fn write_header<W: Write>(ww: &mut W, data_size: u16) -> OResult {
Self::check_size(data_size)
.map_err(|message| io::Error::new(io::ErrorKind::InvalidInput, message))?;
RecordHeader{tag: Self::tag(), data_size}.write(ww)
}
@ -126,9 +129,8 @@ pub trait Record<RData: RecordData> {
fn write<W: Write>(ww: &mut W, data: &RData::InData) -> OResult {
let packed_data = RData::pack(data);
let data_bytes = packed_data.as_ref();
let len = u16::try_from(data_bytes.len()).ok()
.filter(|len| *len <= u16::MAX - 4)
.ok_or_else(|| io::Error::new(io::ErrorKind::InvalidInput, "Record longer than max size (u16)"))?;
let len = u16::try_from(data_bytes.len())
.map_err(|_| io::Error::new(io::ErrorKind::InvalidInput, "Record longer than max size (u16)"))?;
Self::write_header(ww, len)?;
ww.write_all(data_bytes)?;
Ok(usize::from(len) + 4)