From 36a930d2a2fee88b35db731bfa89db16ec7d3328 Mon Sep 17 00:00:00 2001 From: avivdavid23 Date: Wed, 11 Nov 2026 11:50:48 +0200 Subject: [PATCH 1/2] MOD-20160 Retag emptied typed arrays before adding non-numeric items --- src/array.rs | 131 ++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 93 insertions(+), 38 deletions(-) diff --git a/src/array.rs b/src/array.rs index 03ec693..21fb63e 100644 --- a/src/array.rs +++ b/src/array.rs @@ -1026,25 +1026,7 @@ impl IArray { /// a large number of items. pub fn insert(&mut self, index: usize, item: impl Into) -> Result<(), IJsonError> { let item = item.into(); - let current_tag = self.header().type_tag(); - let len = self.len(); - - if len == 0 { - let desired_tag = ArrayTag::from_ivalue(&item); - if desired_tag != ArrayTag::Heterogeneous { - if self.is_static() { - *self = IArray::with_capacity_and_tag(4, desired_tag)?; - } else { - self.promote_to_type(desired_tag)?; - } - } - } else if !(current_tag == ArrayTag::Heterogeneous - || self.can_push_directly(&item, current_tag)) - { - self.promote_to_type(current_tag.promote_with(ArrayTag::from_ivalue(&item)))?; - } - - self.reserve(1)?; + self.prepare_for_item(&item)?; unsafe { // Safety: cannot be static after calling `reserve` @@ -1193,31 +1175,36 @@ impl IArray { /// Pushes a new item onto the back of the array. pub fn push(&mut self, item: impl Into) -> Result<(), IJsonError> { let item = item.into(); - let current_tag = self.header().type_tag(); - let len = self.len(); - - if len == 0 { - let desired_tag = ArrayTag::from_ivalue(&item); - if desired_tag != ArrayTag::Heterogeneous { - if self.is_static() { - *self = IArray::with_capacity_and_tag(4, desired_tag)?; - } else { - self.promote_to_type(desired_tag)?; - } - } - } else if !(current_tag == ArrayTag::Heterogeneous - || self.can_push_directly(&item, current_tag)) - { - self.promote_to_type(current_tag.promote_with(ArrayTag::from_ivalue(&item)))?; - } - - self.reserve(1)?; + self.prepare_for_item(&item)?; unsafe { self.header_mut().push(item); } Ok(()) } + /// Retags or promotes the array so `item` can be stored in it, then reserves + /// space for one more item. + fn prepare_for_item(&mut self, item: &IValue) -> Result<(), IJsonError> { + if self.len() == 0 { + // An emptied typed array keeps its tag, so retag it to fit the item, + // including to heterogeneous for non-numeric items. + let desired_tag = ArrayTag::from_ivalue(item); + if !self.is_static() { + self.promote_to_type(desired_tag)?; + } else if desired_tag != ArrayTag::Heterogeneous { + *self = IArray::with_capacity_and_tag(4, desired_tag)?; + } + } else { + let current_tag = self.header().type_tag(); + if !(current_tag == ArrayTag::Heterogeneous + || self.can_push_directly(item, current_tag)) + { + self.promote_to_type(current_tag.promote_with(ArrayTag::from_ivalue(item)))?; + } + } + self.reserve(1) + } + /// Check if an item can be pushed directly to a typed array without promotion /// This is a simple check: can the value fit in the array type? fn can_push_directly(&self, item: &IValue, array_tag: ArrayTag) -> bool { @@ -2158,6 +2145,7 @@ impl Default for IArray { #[cfg(test)] mod tests { use super::*; + use crate::IObject; use serde::Deserialize; #[test] @@ -2523,6 +2511,73 @@ mod tests { } } + /// One single-element array per typed tag. + fn singleton_typed_arrays() -> Vec { + vec![ + IArray::try_from(vec![1i8]).unwrap(), + IArray::try_from(vec![1u8]).unwrap(), + IArray::try_from(vec![1i16]).unwrap(), + IArray::try_from(vec![1u16]).unwrap(), + IArray::try_from(vec![f16::from_f32(1.5)]).unwrap(), + IArray::try_from(vec![bf16::from_f32(1.5)]).unwrap(), + IArray::try_from(vec![1i32]).unwrap(), + IArray::try_from(vec![1u32]).unwrap(), + IArray::try_from(vec![1.5f32]).unwrap(), + IArray::try_from(vec![1i64]).unwrap(), + IArray::try_from(vec![1u64]).unwrap(), + IArray::try_from(vec![1.5f64]).unwrap(), + ] + } + + #[test] + fn test_emptied_typed_array_accepts_non_numeric_items() { + let non_numeric = [ + IValue::NULL, + IValue::TRUE, + IValue::from("s"), + IValue::from(IArray::new()), + IValue::from(IObject::new()), + ]; + let empty_ways: [fn(&mut IArray); 3] = + [|a| drop(a.remove(0)), |a| drop(a.pop()), |a| a.clear()]; + for empty in empty_ways { + for item in &non_numeric { + for mut array in singleton_typed_arrays() { + let tag = array.header().type_tag(); + assert_ne!(tag, ArrayTag::Heterogeneous); + empty(&mut array); + array.insert(0, item.clone()).unwrap(); + assert_eq!( + array.header().type_tag(), + ArrayTag::Heterogeneous, + "{tag:?}" + ); + assert_eq!(array.len(), 1); + assert_eq!(array[0], *item); + } + for mut array in singleton_typed_arrays() { + empty(&mut array); + array.push(item.clone()).unwrap(); + array.push(1).unwrap(); + assert_eq!(array.len(), 2); + assert_eq!(array[0], *item); + assert_eq!(array[1], IValue::from(1)); + } + } + } + } + + #[test] + fn test_emptied_typed_array_retags_to_numeric_item() { + for mut array in singleton_typed_arrays() { + array.remove(0).unwrap(); + array.insert(0, 7).unwrap(); + assert_eq!(array.header().type_tag(), ArrayTag::I8); + let items: Vec = array.iter().map(|v| v.into_owned()).collect(); + assert_eq!(items, vec![IValue::from(7)]); + } + } + #[test] fn test_typed_array_type_promotion() { let mut i8_array = IArray::try_from(vec![-5i8, 10i8]).unwrap(); From 0bc8c856607d89ffecd5364f6348d38f982beae7 Mon Sep 17 00:00:00 2001 From: avivdavid23 Date: Thu, 12 Nov 2026 10:28:20 +0200 Subject: [PATCH 2/2] MOD-20160 Document empty-array retag and test heterogeneous to numeric --- src/array.rs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/array.rs b/src/array.rs index 21fb63e..b852094 100644 --- a/src/array.rs +++ b/src/array.rs @@ -1186,8 +1186,8 @@ impl IArray { /// space for one more item. fn prepare_for_item(&mut self, item: &IValue) -> Result<(), IJsonError> { if self.len() == 0 { - // An emptied typed array keeps its tag, so retag it to fit the item, - // including to heterogeneous for non-numeric items. + // Empty: tag follows the item only (may narrow, drops any FPHA type); + // non-numeric item -> Heterogeneous. let desired_tag = ArrayTag::from_ivalue(item); if !self.is_static() { self.promote_to_type(desired_tag)?; @@ -2578,6 +2578,16 @@ mod tests { } } + #[test] + fn test_emptied_heterogeneous_array_retags_to_numeric_item() { + let mut array = IArray::new(); + array.push(IValue::from("s")).unwrap(); + array.pop().unwrap(); + assert_eq!(array.header().type_tag(), ArrayTag::Heterogeneous); + array.push(7).unwrap(); + assert_eq!(array.header().type_tag(), ArrayTag::I8); + } + #[test] fn test_typed_array_type_promotion() { let mut i8_array = IArray::try_from(vec![-5i8, 10i8]).unwrap();