fix(arrow): reject fractional array pitches and avoid rectangle overflow
This commit is contained in:
parent
0cbb2c0e2f
commit
f38d88e3cf
1 changed files with 184 additions and 6 deletions
|
|
@ -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, °enerate] {
|
||||
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], °enerate[..8]].concat()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn paths_preserve_missing_explicit_zero_and_negative_widths() {
|
||||
let mut bytes = library_header();
|
||||
|
|
|
|||
Loading…
Reference in a new issue