fix(parser): calculate array reference pitches without overflow

Use i64 coordinate differences and checked i32 pitches in both scans and full reads. Reject nonpositive counts, malformed coordinate records, and pitches that cannot fit the packed schema.
This commit is contained in:
Jan Petykiewicz 2026-11-14 14:39:01 -08:00
commit ae33c4948b

View file

@ -75,6 +75,60 @@ fn list_struct(array: &StructArray, name: &str, row: usize) -> StructArray {
.clone()
}
fn aref_fixture(origin: [i32; 2], column: [i32; 2], row: [i32; 2], counts: [i16; 2]) -> Vec<u8> {
let mut bytes = library_header();
record(&mut bytes, records::RTAG_BGNSTR, &[0; 24]);
record(&mut bytes, records::RTAG_STRNAME, b"top\0");
record(&mut bytes, records::RTAG_AREF, &[]);
record(&mut bytes, records::RTAG_SNAME, b"leaf");
let counts: Vec<u8> = counts.into_iter().flat_map(i16::to_be_bytes).collect();
record(&mut bytes, records::RTAG_COLROW, &counts);
record(
&mut bytes,
records::RTAG_XY,
&xy(&[origin[0], origin[1], column[0], column[1], row[0], row[1]]),
);
record(&mut bytes, records::RTAG_ENDEL, &[]);
record(&mut bytes, records::RTAG_ENDSTR, &[]);
record(&mut bytes, records::RTAG_ENDLIB, &[]);
bytes
}
#[test]
fn aref_pitches_use_wide_coordinate_differences_in_full_reads_and_scans() {
let bytes = aref_fixture(
[i32::MIN, i32::MAX],
[i32::MAX - 1, i32::MIN + 1],
[i32::MIN, i32::MIN + 1],
[2, 2],
);
let full_cells = list_struct(&read_library(&bytes).unwrap().1, "cells", 0);
let scan_cells = list_struct(&scan_library(&bytes).unwrap().1, "cells", 0);
for refs in [
list_struct(&full_cells, "arefs", 0),
list_struct(&scan_cells, "refs", 0),
] {
let column = u64_value(&refs, "xy0", 0);
let row = u64_value(&refs, "xy1", 0);
assert_eq!((column >> 32) as i32, i32::MAX);
assert_eq!(column as i32, -i32::MAX);
assert_eq!((row >> 32) as i32, 0);
assert_eq!(row as i32, -i32::MAX);
}
}
#[test]
fn aref_invalid_counts_and_unrepresentable_pitches_return_errors() {
for counts in [[0, 1], [-1, 1], [1, 0], [1, -1]] {
let bytes = aref_fixture([0, 0], [10, 0], [0, 10], counts);
assert!(read_library(&bytes).is_err());
assert!(scan_library(&bytes).is_err());
}
let bytes = aref_fixture([i32::MIN, 0], [i32::MAX, 0], [i32::MIN, 10], [1, 2]);
assert!(read_library(&bytes).is_err());
assert!(scan_library(&bytes).is_err());
}
fn u64_value(array: &StructArray, name: &str, row: usize) -> u64 {
array
.column_by_name(name)