From 3410b54793c3a1808e58d0fae94fb2ebd5f81015 Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Wed, 11 Feb 2026 21:21:32 +0000 Subject: feat: добавить тесты для проверки структурных инвариантов и корректности сортировки в RsLi MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crates/rsli/src/parse.rs | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) (limited to 'crates/rsli/src/parse.rs') diff --git a/crates/rsli/src/parse.rs b/crates/rsli/src/parse.rs index 272e076..9a916dc 100644 --- a/crates/rsli/src/parse.rs +++ b/crates/rsli/src/parse.rs @@ -149,13 +149,31 @@ pub fn parse_library(bytes: Arc<[u8]>, opts: OpenOptions) -> Result { let presorted_flag = u16::from_le_bytes([bytes[14], bytes[15]]); if presorted_flag == 0xABBA { + let mut seen = vec![false; count]; for entry in &entries { let idx = i32::from(entry.sort_to_original); - if idx < 0 || usize::try_from(idx).map_err(|_| Error::IntegerOverflow)? >= count { + if idx < 0 { return Err(Error::CorruptEntryTable( "sort_to_original is not a valid permutation index", )); } + let idx = usize::try_from(idx).map_err(|_| Error::IntegerOverflow)?; + if idx >= count { + return Err(Error::CorruptEntryTable( + "sort_to_original is not a valid permutation index", + )); + } + if seen[idx] { + return Err(Error::CorruptEntryTable( + "sort_to_original is not a permutation", + )); + } + seen[idx] = true; + } + if seen.iter().any(|value| !*value) { + return Err(Error::CorruptEntryTable( + "sort_to_original is not a permutation", + )); } } else { let mut sorted: Vec = (0..count).collect(); -- cgit v1.2.3