fix(arrow): reject fractional array pitches and avoid rectangle overflow

This commit is contained in:
Jan Petykiewicz 2026-11-15 19:50:49 -08:00
commit f38d88e3cf
3 changed files with 184 additions and 6 deletions

View file

@ -93,6 +93,12 @@ References point to other cells in the library.
- `xy1`: `UInt64` (Packed row basis vector increment)
- `counts`: `UInt32` (Packed columns and rows)
Array increments must be exact signed 32-bit integer database-unit values. Full
reads, hierarchy scans, selected reads, and their FFI entry points reject arrays
whose endpoint/count division is fractional or outside that range. They never
round or truncate placements. The review CLI retains original endpoints and can
compare fractional arrays independently of this Arrow representation.
**SRefProp / ARefProp**:
- Same as above, plus a `properties` field.

View file

@ -822,9 +822,9 @@ fn boundary_to_rect(vertices: &[i32]) -> Option<[i32; 4]> {
let mut seen_corners: u8 = 0;
for ii in 0..4 {
let jj = (ii + 1) % 4;
let dx = xs[jj] - xs[ii];
let dy = ys[jj] - ys[ii];
if (dx == 0) == (dy == 0) {
let same_x = xs[jj] == xs[ii];
let same_y = ys[jj] == ys[ii];
if same_x == same_y {
return None;
}
@ -922,7 +922,14 @@ fn read_ref<'a>(
}
let pitch = |end: i32, origin: i32, count: i16| {
let value = (i64::from(end) - i64::from(origin)) / i64::from(count);
let delta = i64::from(end) - i64::from(origin);
let divisor = i64::from(count);
if delta % divisor != 0 {
return Err((input, crate::basic::ErrType::Failed(
"AREF pitch is not an integer database-unit increment; the Arrow schema cannot represent it exactly".to_string()
)));
}
let value = delta / divisor;
i32::try_from(value).map_err(|_| (input, crate::basic::ErrType::Failed(
"AREF pitch exceeds signed 32-bit coordinate range".to_string()
)))

View file

@ -10,9 +10,12 @@ use klamath_rs_ext::{
basic::encode_real8,
last_error_message,
library::{read_library, read_selected_library, scan_library},
read_cells_bytes, records, scan_bytes,
read_cells_bytes, read_path, records, scan_bytes,
};
use std::{
ffi::{CStr, CString},
ptr,
};
use std::{ffi::CStr, ptr};
fn xy(values: &[i32]) -> Vec<u8> {
values.iter().flat_map(|v| v.to_be_bytes()).collect()
@ -129,6 +132,168 @@ fn aref_invalid_counts_and_unrepresentable_pitches_return_errors() {
assert!(scan_library(&bytes).is_err());
}
#[test]
fn fractional_aref_pitches_are_rejected_by_all_arrow_readers() {
for (column, row, counts) in [
([10, 0], [0, 20], [3, 2]),
([-10, 0], [0, 20], [3, 2]),
([0, 10], [20, 0], [3, 2]),
([20, 0], [0, 10], [2, 3]),
([20, 0], [-10, 0], [2, 3]),
] {
let bytes = aref_fixture([0, 0], column, row, counts);
let range = [(library_header().len() as u64, bytes.len() as u64 - 4)];
for error in [
read_library(&bytes).unwrap_err().1,
scan_library(&bytes).unwrap_err().1,
read_selected_library(&bytes, &range).unwrap_err().1,
] {
assert!(format!("{error:?}").contains("not an integer database-unit increment"));
}
}
let bytes = aref_fixture([0, 0], [10, 0], [0, 20], [3, 2]);
let mut array = FFI_ArrowArray::empty();
let mut schema = FFI_ArrowSchema::empty();
assert_eq!(
unsafe { scan_bytes(bytes.as_ptr(), bytes.len(), &mut array, &mut schema) },
-1
);
let range = [library_header().len() as u64, bytes.len() as u64 - 4];
assert_eq!(
unsafe {
read_cells_bytes(
bytes.as_ptr(),
bytes.len(),
range.as_ptr(),
1,
&mut array,
&mut schema,
)
},
-1
);
let path = std::env::temp_dir().join(format!(
"klamath-fractional-aref-{}.gds",
std::process::id()
));
struct Cleanup(std::path::PathBuf);
impl Drop for Cleanup {
fn drop(&mut self) {
let _ = std::fs::remove_file(&self.0);
}
}
let _cleanup = Cleanup(path.clone());
std::fs::write(&path, &bytes).unwrap();
let path = CString::new(path.to_str().unwrap()).unwrap();
assert_eq!(
unsafe { read_path(path.as_ptr(), &mut array, &mut schema) },
-1
);
let message = unsafe { CStr::from_ptr(last_error_message()) }
.to_str()
.unwrap();
assert!(message.contains("not an integer database-unit increment"));
assert!(array.is_released());
// The object reviewer keeps the exact endpoints, including fractional pitches.
let mut bytes = bytes;
bytes.truncate(bytes.len() - 4);
record(&mut bytes, records::RTAG_BGNSTR, &[0; 24]);
record(&mut bytes, records::RTAG_STRNAME, b"leaf");
record(&mut bytes, records::RTAG_ENDSTR, &[]);
record(&mut bytes, records::RTAG_ENDLIB, &[]);
let doc = klamath_rs_ext::review::Document::parse(&bytes).unwrap();
assert_eq!(
klamath_rs_ext::review::Comparison::between(&doc, &doc).exit_code(),
0
);
}
#[test]
fn rectangle_detection_handles_extreme_coordinates_and_preserves_other_polygons() {
let mut bytes = library_header();
record(&mut bytes, records::RTAG_BGNSTR, &[0; 24]);
record(&mut bytes, records::RTAG_STRNAME, b"top\0");
let wide = [
i32::MIN,
i32::MIN,
i32::MAX,
i32::MIN,
i32::MAX,
i32::MAX,
i32::MIN,
i32::MAX,
i32::MIN,
i32::MIN,
];
let reversed = [
i32::MIN,
i32::MIN,
i32::MIN,
i32::MAX,
i32::MAX,
i32::MAX,
i32::MAX,
i32::MIN,
i32::MIN,
i32::MIN,
];
let diagonal = [0, 0, 10, 10, 0, 10, 10, 0, 0, 0];
let degenerate = [0, 0, 0, 0, 10, 0, 10, 0, 0, 0];
for points in [&wide, &reversed, &diagonal, &degenerate] {
record(&mut bytes, records::RTAG_BOUNDARY, &[]);
record(&mut bytes, records::RTAG_LAYER, &[0, 1]);
record(&mut bytes, records::RTAG_DATATYPE, &[0, 0]);
record(&mut bytes, records::RTAG_XY, &xy(points));
record(&mut bytes, records::RTAG_ENDEL, &[]);
}
record(&mut bytes, records::RTAG_ENDSTR, &[]);
record(&mut bytes, records::RTAG_ENDLIB, &[]);
let full = read_library(&bytes).unwrap().1;
let scan = scan_library(&bytes).unwrap().1;
assert_eq!(
full,
read_selected_library(&bytes, &ranges(&scan)).unwrap().1
);
let cells = list_struct(&full, "cells", 0);
let rects = list_struct(&cells, "rect_batches", 0);
let values = rects
.column_by_name("rects")
.unwrap()
.as_any()
.downcast_ref::<ListArray>()
.unwrap()
.value(0);
let values = values.as_any().downcast_ref::<Int32Array>().unwrap();
assert_eq!(
values.values().as_ref(),
&[
i32::MIN,
i32::MIN,
i32::MAX,
i32::MAX,
i32::MIN,
i32::MIN,
i32::MAX,
i32::MAX
]
);
let boundaries = list_struct(&cells, "boundary_batches", 0);
let values = boundaries
.column_by_name("vertices")
.unwrap()
.as_any()
.downcast_ref::<ListArray>()
.unwrap()
.value(0);
let values = values.as_any().downcast_ref::<Int32Array>().unwrap();
assert_eq!(
values.values().as_ref(),
[&diagonal[..8], &degenerate[..8]].concat()
);
}
#[test]
fn paths_preserve_missing_explicit_zero_and_negative_widths() {
let mut bytes = library_header();