fix(ffi): report compact parse errors with file offsets
Format the parser error reason and absolute byte offset without dumping the remaining input bytes. Calculate offsets correctly for errors inside bounded selected-cell ranges.
This commit is contained in:
parent
e858ec83c9
commit
4108baf0a1
2 changed files with 87 additions and 4 deletions
18
src/lib.rs
18
src/lib.rs
|
|
@ -54,6 +54,18 @@ fn describe_panic(payload: Box<dyn Any + Send>) -> String {
|
|||
}
|
||||
|
||||
|
||||
fn describe_parse_error(input: &[u8], (remaining, reason): (&[u8], crate::basic::ErrType)) -> String {
|
||||
// Selected-cell errors may contain a bounded range rather than a file suffix.
|
||||
let offset = remaining.as_ptr() as usize - input.as_ptr() as usize;
|
||||
let message = match reason {
|
||||
crate::basic::ErrType::Failed(message) => message,
|
||||
crate::basic::ErrType::Incomplete(Some(size)) => format!("Unexpected end of input; need {size} bytes"),
|
||||
crate::basic::ErrType::Incomplete(None) => "Unexpected end of input".to_string(),
|
||||
};
|
||||
format!("at byte {offset}: {message}")
|
||||
}
|
||||
|
||||
|
||||
fn export_struct_array(
|
||||
op_name: &str,
|
||||
arr: *mut FFI_ArrowArray,
|
||||
|
|
@ -128,7 +140,7 @@ pub unsafe extern "C" fn read_path(
|
|||
let input = fs::read(Path::new(&path))
|
||||
.map_err(|err| format!("file read failed for {}: {err}", path.display()))?;
|
||||
let (_input, struct_arr) = read_library(&input)
|
||||
.map_err(|err| format!("read failed: {err:?}"))?;
|
||||
.map_err(|err| format!("read failed {}", describe_parse_error(&input, err)))?;
|
||||
Ok(struct_arr)
|
||||
})
|
||||
}
|
||||
|
|
@ -166,7 +178,7 @@ pub unsafe extern "C" fn scan_bytes(
|
|||
export_struct_array("scan_bytes", arr, schema, || {
|
||||
let input = unsafe { input_slice(data, size, "data") }?;
|
||||
let (_input, struct_arr) = scan_library(input)
|
||||
.map_err(|err| format!("scan failed: {err:?}"))?;
|
||||
.map_err(|err| format!("scan failed {}", describe_parse_error(input, err)))?;
|
||||
Ok(struct_arr)
|
||||
})
|
||||
}
|
||||
|
|
@ -198,7 +210,7 @@ pub unsafe extern "C" fn read_cells_bytes(
|
|||
.map(|chunk| (chunk[0], chunk[1]))
|
||||
.collect();
|
||||
let (_input, struct_arr) = read_selected_library(input, &cell_ranges)
|
||||
.map_err(|err| format!("selected-cell read failed: {err:?}"))?;
|
||||
.map_err(|err| format!("selected-cell read failed {}", describe_parse_error(input, err)))?;
|
||||
Ok(struct_arr)
|
||||
})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
mod common;
|
||||
|
||||
use arrow::ffi::{FFI_ArrowArray, FFI_ArrowSchema};
|
||||
use klamath_rs_ext::{last_error_message, read_path, records};
|
||||
use klamath_rs_ext::{last_error_message, read_path, read_cells_bytes, scan_bytes, records};
|
||||
use std::{ffi::{CStr, CString}, fs, path::PathBuf, ptr};
|
||||
|
||||
struct TestFile(PathBuf);
|
||||
|
|
@ -84,3 +84,74 @@ fn null_arrow_outputs_return_an_error() {
|
|||
assert_eq!(unsafe { read_path(ptr::null(), ptr::null_mut(), ptr::null_mut()) }, -1);
|
||||
assert!(error_message().contains("Arrow output pointer was null"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn large_malformed_inputs_report_offsets_without_dumping_contents() {
|
||||
let mut bytes = vec![0; 1024 * 1024 + 4];
|
||||
bytes[..4].copy_from_slice(&[0, 4, 0xff, 0xff]);
|
||||
let mut array = FFI_ArrowArray::empty();
|
||||
let mut schema = FFI_ArrowSchema::empty();
|
||||
let file = TestFile::new("large-malformed", &bytes);
|
||||
assert_eq!(
|
||||
unsafe { scan_bytes(bytes.as_ptr(), bytes.len(), &mut array, &mut schema) },
|
||||
-1
|
||||
);
|
||||
let scan_message = error_message();
|
||||
assert_eq!(
|
||||
unsafe { read_path(file.cpath().as_ptr(), &mut array, &mut schema) },
|
||||
-1
|
||||
);
|
||||
let read_message = error_message();
|
||||
assert_eq!(
|
||||
unsafe {
|
||||
read_cells_bytes(
|
||||
bytes.as_ptr(),
|
||||
bytes.len(),
|
||||
ptr::null(),
|
||||
0,
|
||||
&mut array,
|
||||
&mut schema,
|
||||
)
|
||||
},
|
||||
-1
|
||||
);
|
||||
let selected_message = error_message();
|
||||
for message in [scan_message, read_message, selected_message] {
|
||||
assert!(message.contains("at byte 4"), "{message}");
|
||||
assert!(message.contains("Unexpected record"), "{message}");
|
||||
assert!(message.len() < 256);
|
||||
}
|
||||
assert!(array.is_released());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn selected_cell_errors_use_file_offsets_for_bounded_ranges() {
|
||||
let mut bytes = common::library_header();
|
||||
let start = bytes.len() as u64;
|
||||
common::record(&mut bytes, records::RTAG_BGNSTR, &[0; 24]);
|
||||
common::record(&mut bytes, records::RTAG_STRNAME, b"top\0");
|
||||
common::record(&mut bytes, records::RTAG_ENDSTR, &[]);
|
||||
let cell_end = bytes.len();
|
||||
common::record(&mut bytes, records::RTAG_ENDLIB, &[]);
|
||||
let range_end = bytes.len() as u64;
|
||||
bytes.resize(bytes.len() + 1024, 0);
|
||||
let ranges = [start, range_end];
|
||||
let mut array = FFI_ArrowArray::empty();
|
||||
let mut schema = FFI_ArrowSchema::empty();
|
||||
assert_eq!(
|
||||
unsafe {
|
||||
read_cells_bytes(
|
||||
bytes.as_ptr(),
|
||||
bytes.len(),
|
||||
ranges.as_ptr(),
|
||||
1,
|
||||
&mut array,
|
||||
&mut schema,
|
||||
)
|
||||
},
|
||||
-1
|
||||
);
|
||||
assert!(error_message().contains(&format!(
|
||||
"at byte {cell_end}: Cell byte range extends past ENDSTR"
|
||||
)));
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue