From ae33c4948b6363bdbb5d11e4647455e8d1128483 Mon Sep 17 00:00:00 2001 From: Jan Petykiewicz Date: Sat, 14 Nov 2026 14:39:01 -0800 Subject: [PATCH] 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. --- src/elements.rs | 24 +++++++++++++++--- tests/selective_reads.rs | 54 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 4 deletions(-) diff --git a/src/elements.rs b/src/elements.rs index 76988ba..9541b5d 100644 --- a/src/elements.rs +++ b/src/elements.rs @@ -886,6 +886,9 @@ fn read_ref<'a>( angle_rad = _angle_deg.to_radians(); }, records::RTAG_COLROW => { + if header.data_size != 4 { + return fail(input, "COLROW must contain two counts".to_string()); + } let mut _colrow; (input, _colrow) = COLROW::read_data(input, header.data_size)?; colrow = Some((_colrow.next().unwrap(), _colrow.next().unwrap())); @@ -899,6 +902,10 @@ fn read_ref<'a>( (input, header) = RecordHeader::read(input)?; } + let expected_xy_size = if is_aref { 24 } else { 8 }; + if header.data_size != expected_xy_size { + return fail(input, format!("Expected {expected_xy_size} bytes of reference coordinates, got {}", header.data_size)); + } let (input, mut xy_iter) = XY::read_data(input, header.data_size)?; let xx = xy_iter.next().unwrap(); let yy = xy_iter.next().unwrap(); @@ -910,11 +917,20 @@ fn read_ref<'a>( return fail(input, "AREF without COLROW before XY".to_string()) } let (count0, count1) = colrow.unwrap(); + if count0 <= 0 || count1 <= 0 { + return fail(input, "AREF column and row counts must be positive".to_string()); + } - let x0a = (xy_iter.next().unwrap() - xx) / (count0 as i32); - let y0a = (xy_iter.next().unwrap() - yy) / (count0 as i32); - let x1a = (xy_iter.next().unwrap() - xx) / (count1 as i32); - let y1a = (xy_iter.next().unwrap() - yy) / (count1 as i32); + let pitch = |end: i32, origin: i32, count: i16| { + let value = (i64::from(end) - i64::from(origin)) / i64::from(count); + i32::try_from(value).map_err(|_| (input, crate::basic::ErrType::Failed( + "AREF pitch exceeds signed 32-bit coordinate range".to_string() + ))) + }; + let x0a = pitch(xy_iter.next().unwrap(), xx, count0)?; + let y0a = pitch(xy_iter.next().unwrap(), yy, count0)?; + let x1a = pitch(xy_iter.next().unwrap(), xx, count1)?; + let y1a = pitch(xy_iter.next().unwrap(), yy, count1)?; let xy0a = ((x0a as u32 as u64) << 32) | (y0a as u32 as u64); let xy1a = ((x1a as u32 as u64) << 32) | (y1a as u32 as u64); diff --git a/tests/selective_reads.rs b/tests/selective_reads.rs index de9e34c..3265491 100644 --- a/tests/selective_reads.rs +++ b/tests/selective_reads.rs @@ -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 { + 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 = 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)